jsPDF正在下载EMPTY PDF

时间:2015-10-13 20:05:27

标签: javascript jquery html pdf-generation jspdf

我有一个内容div,ID为" divDownload"。 在内容div中我有一个面板,它有一些中继器和一些表和标签等。  我想在用户点击下载按钮时将该div下载为pdf。为此,我使用jsPDF库。但它正在下载空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中搜索了很多并尝试了不同的方法,但没有运气。

3 个答案:

答案 0 :(得分:0)

我用过:

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

此外,我已将true用于#noprint

希望它可以帮到你!

答案 1 :(得分: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);

如果有效或无效,请告诉我。很高兴解决这个问题!谢谢!

答案 2 :(得分:0)

@Abhi,

jsPdf适用于html表。

因此,你必须提供一个html表的id而不是你正在尝试的div。尝试使用html表。

希望它有所帮助。