2015-10-13 77 views
0

我有一個id爲「divDownload」的內容div。 在內容div我有一個面板有一些中繼器和一些表格和標籤等。 我想在用戶點擊下載按鈕時將該div下載爲pdf。爲此,我使用jsPDF庫。但它正在下載空的PDF。jsPDF正在下載EMPTY PDF

HTML頁面:

<asp:Button ID="btnDownload" runat="server" Text="Download" /> 

<div id="divDownload"> 
    <asp:Panel ID="pnldownload" runat="server"> 
     // repeaters 
     //tables 
     //labels 
    </Panel> 
</div> 

<div id="noprint"></div> 

JavaScript函數:

$(document).ready(function() { 
     $("#<%= btnDownload.ClientID %>").click(function (e) { 

      var pdf = new jsPDF('p', 'pt', 'a4'); 
      var source = $("#divDownload"); 

      alert(source); 
      specialElementHandlers = { 
       // element with id of "bypass" - jQuery style selector 
       '#noprint': function (element, renderer) { 
        // true = "handled elsewhere, bypass text extraction" 
        return false; 
       } 
      }; 
      pdf.fromHTML(
      source, 
      15, 
      15, { 
       'width': 150, 
       'elementHandlers': specialElementHandlers 
      }); 
      pdf.save('test.pdf'); 


     }); 
    }); 

能有人幫我如何解決這一個?搜索了很多,並嘗試了不同的方式在StackOverFlow中,但沒有運氣。

+0

當您在開發工具中檢查瀏覽器的JavaScript控制檯時,是否收到任何客戶端錯誤? – mason

回答

1

@Ahi,

jsPdf與html表一起使用。

因此,您必須在嘗試時提供html表格的id而不是div。嘗試使用html表格。

希望它有幫助。

0

我用:

var source = $("#divDownload").html; 

而且我已經使用true#noprint

希望它可以幫助你!

0

我又試了試,以下是完整代碼:

 var pdf = new jsPDF('p', 'pt', 'a4'); 
     var source = $("#divDownload").html(); 

     specialElementHandlers = { 
      // element with id of "bypass" - jQuery style selector 
      '#noprint': function (element, renderer) { 
       // true = "handled elsewhere, bypass text extraction" 
       return true; 
      } 
     }; 

     pdf.fromHTML(
     source, 20, 20, { 'width': 150, 
      'elementHandlers': specialElementHandlers 
     }, 

     function (dispose) { 
      // dispose: object with X, Y of the last line add to the PDF 
      //   this allow the insertion of new lines after html 
      pdf.save('Test.pdf'); 
     }, margins); 

請讓我知道,如果它工作或不工作。喜歡解決這個問題!謝謝!