Skip to content

Instantly share code, notes, and snippets.

@Fahrek
Forked from DavidMellul/HTMLtoPDF_Simple.js
Created April 24, 2019 17:47
Show Gist options
  • Save Fahrek/fc3b069cb3e303d3ce843b8c3c74cb3e to your computer and use it in GitHub Desktop.
Save Fahrek/fc3b069cb3e303d3ce843b8c3c74cb3e to your computer and use it in GitHub Desktop.
function print() {
const filename = 'ThisIsYourPDFFilename.pdf';
html2canvas(document.querySelector('#nodeToRenderAsPDF')).then(canvas => {
let pdf = new jsPDF('p', 'mm', 'a4');
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 211, 298);
pdf.save(filename);
});
}
// Variant
// This one lets you improve the PDF sharpness by scaling up the HTML node tree to render as an image before getting pasted on the PDF.
function print(quality = 1) {
const filename = 'ThisIsYourPDFFilename.pdf';
html2canvas(document.querySelector('#nodeToRenderAsPDF'),
{scale: quality}
).then(canvas => {
let pdf = new jsPDF('p', 'mm', 'a4');
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 211, 298);
pdf.save(filename);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment