Skip to content

Instantly share code, notes, and snippets.

@DavidMellul
Last active April 21, 2021 18:20
Show Gist options
  • Save DavidMellul/d01c720ce31aecf99be3a6441255381f to your computer and use it in GitHub Desktop.
Save DavidMellul/d01c720ce31aecf99be3a6441255381f 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