Skip to content

Instantly share code, notes, and snippets.

@gbili
Forked from DavidMellul/HTMLtoPDF_Simple.js
Created January 27, 2019 12:53
Show Gist options
  • Save gbili/5f422471042775be527f0ea3b5f4af92 to your computer and use it in GitHub Desktop.
Save gbili/5f422471042775be527f0ea3b5f4af92 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