2015-10-13 101 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); 

请让我知道,如果它工作或不工作。喜欢解决这个问题!谢谢!