是否可以使用jsPDF将多个HTML页面转换为PDF?

时间:2019-11-11 02:51:08

标签: javascript html css jspdf html2canvas

我正在尝试使用jsPDF将多个HTML页面转换为PDF。这是示例代码,

pageOne.html

<body>
<div id="pageOneContent">
<h1> Welcome to page one</h1>
  <p>
    Discussion of a war memorial for Northampton began shortly after 
    the armistice in 1918, and from July 1919 a temporary wooden cenotaph 
    stood on Abington Street in the town centre. The Northamptonshire War
    Memorial Committee commissioned Lutyens to design a permanent memorial.
    The monument's design was completed and approved quickly, but its 
    installation was delayed by six years until the site could be purchased
    from the Church of England, which required a faculty from the local 
    diocese. The memorial was finally unveiled on 11 November 1926 after 
    a service and a parade including local schoolchildren and civic 
    leaders.
  </p>
  <button onclick="secondPage.html">Next</button>
</div>

secondPage.html

<body>
<div id="pageTwoContent">
<h1> Welcome to second page</h1>
  <p>
    Northampton's memorial is one of the more elaborate town memorials in
    England. It uses three features characteristic of Lutyens's war 
    memorials: a pair of obelisks, the Stone of Remembrance (which Lutyens
    designed for the Imperial War Graves Commission), and painted stone 
    flags on the obelisks, which were rejected for his Cenotaph in London 
    but feature on several of his other memorials. Today it is a Grade I 
    listed building; it was upgraded from Grade II in 2015 when Lutyens's 
    war memorials were declared a "national collection" and all were 
    granted listed building status or had their listing renewed.
  </p>
  <button onclick="generatePDF();">Generate PDF</button>
</div>

脚本

function generatePDF(){
  var doc = new jsPDF();
  var specialElementHandlers = {
        '#editor': function (element, renderer) {
            return true;
        }
    };
    doc.fromHTML($("#pageOneContent").get(0), 15, 15, {
      "width":170,
      "elementHandlers": specialElementHandlers
    });
    doc.addPage()
    doc.fromHTML($("#pageTwoContent").get(0), 15, 15, {
      "width":170,
      "elementHandlers": specialElementHandlers
    });

    doc.save("multiPagePDF.pdf");
}

我尝试使用示例代码,但是当我尝试生成两页时,仅生成第二页,而第一页为空。有什么方法可以捕获两个页面并生成单个PDF。除了jsPDF之外,是否还有其他生成PDF的工具,因为我将处理40多个html页面以转换为PDF。

0 个答案:

没有答案