angular - Angular2 - Generate pdf from HTML using jspdf -
for project i'm working on need able generate pdf of page user on, i'll use jspdf
. since have html
need generate pdf with, i'll need addhtml()
function. there lot of topic this, saying
you'll either need use
html2canvas
orrasterizehtml
.
i've chosen use html2canvas
. code looks @ moment:
import { injectable, elementref, viewchild } '@angular/core'; import * jspdf 'jspdf'; import * d3 'd3'; import * html2canvas 'html2canvas'; @injectable () export class pdfgeneratorservice { @viewchild('to-pdf') element: elementref; generatepdf () { html2canvas(this.element.nativeelement, <html2canvas.html2canvasoptions>{ onrendered: function(canvas: htmlcanvaselement) { var pdf = new jspdf('p','pt','a4'); pdf.addhtml(canvas, function() { pdf.save('web.pdf'); }); } }); } }
when function called, console error:
exception: error in ./appcomponent class appcomponent - inline template:3:4 caused by: need either https://github.com/niklasvh/html2canvas or https://github.com/cburgmer/rasterizehtml.js
why this? give canvas
parameter , still says need use html2canvas
.
what found worked adding:
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
to index.html file (it presumably elsewhere).
i used:
const elementtoprint = document.getelementbyid('foo'); //the html element become pdf const pdf = new jspdf('p', 'pt', 'a4'); pdf.addhtml(elementtoprint, () => { doc.save('web.pdf'); });
which no longer uses html2canvas in code.
can remove following import:
import * html2canvas 'html2canvas';
Comments
Post a Comment