如何使用 html 和文本中的 jspdf 创建 pdf?

How can i create pdf with jspdf from html and text?

我需要将我的 html 内容和文本一起打印,我尝试使用以下代码,但添加的文本未打印在 PDF 中。它只打印 html 内容。请帮我解决这个问题....

    pdf = new jsPDF('l', 'mm', 'ledger'),
    specialElementHandlers = {
      '#editor': function( element, renderer ) {
          return true;
      }
};
pdf.fromHTML(
      $('#customers').get(0) // HTML element
    , 15  // x coord
    , 0.5  // y coord
    , {
          'width': 3000 // was 7.5, max width of content on PDF
        , elementHandlers: specialElementHandlers
    }
);
pdf.text(35, 25, "test");
pdf.save( filename ); 
}); 

我认为你错过了这里的语法 你在这里做了什么 pdf.text(35, 25, "test"); 而是使用这个: pdf.text("test",35, 25); (其中 35 代表 X 轴,25 代表 pdf 文档中的 Y 轴)。

您必须使用函数的完整语法:

doc.fromHTML(HTML, x, y, settings, callback, margins);

使用回调,您可以添加一个在 fromHtml complete 上执行的函数。

在您的代码中:

pdf = new jsPDF('l', 'mm', 'ledger'),
    specialElementHandlers = {
      '#editor': function( element, renderer ) {
          return true;
      }
};
pdf.fromHTML(
      $('#customers').get(0) // HTML element
    , 15  // x coord
    , 0.5  // y coord
    , {
          'width': 3000 // was 7.5, max width of content on PDF
        , elementHandlers: specialElementHandlers
    },
    myfunc,
    {
        top : 25,
        bottom : 25
    }
);

function myfunc(){
    pdf.text(35, 25, "test");
    pdf.save( filename ); 
}