Using jsPDF and html2canvas with es6

2019-07-03 05:40发布

问题:

I'm trying to use jsPDF and html2canvas with es6.

I'm Importing html2canvas and jsPDF but getting an error on the addHTML when I comment out the addHTML the pdf is generated.

any clue what is the problem?

Thanks.

jspdf.debug.js:3754Uncaught Error: You need either https://github.com/niklasvh/html2canvas or https://github.com/cburgmer/rasterizeHTML.js(…)jsPDFAPI.addHTML @ jspdf.debug.js:3754(anonymous function) @ index.js:12(anonymous function) @ bundle.js?V1.27.2:31546(anonymous function) @ bundle.js?V1.27.2:31547__webpack_require__ @ bootstrap f7845b2…:555fn @ bootstrap f7845b2…:86(anonymous function) @ bootstrap f7845b2…:578__webpack_require__ @ bootstrap f7845b2…:555(anonymous function) @ bootstrap f7845b2…:578(anonymous function) @ bootstrap f7845b2…:578

import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';

doc.setFontSize(40);
doc.text(35, 25, "Paranyan loves jsPDF");

doc.addHTML(document.footer,function() {
     pdf.save('web.pdf');
});
<footer>
	<p id="to-pdf">HTML content...</p>
</footer>

回答1:

From https://github.com/MrRio/jsPDF/issues/1225:

jsPDF requires html2canvas to be on the window:

window.html2canvas = html2canvas



回答2:

Alternatively to calling window. you can also modify your import to this: import * as jsPDF from 'jspdf'



回答3:

Include the following script in index.html instead of the import in typescript file

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>