Skip to content

Instantly share code, notes, and snippets.

@moaoa
Last active March 3, 2024 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moaoa/89198dafc46c906c89fe898354cb2ee5 to your computer and use it in GitHub Desktop.
Save moaoa/89198dafc46c906c89fe898354cb2ee5 to your computer and use it in GitHub Desktop.
import jsPdf from 'jspdf';
domToPng(ticketElement).then(dataUrl => {
const pdf = new jsPdf();
const imgProps = pdf.getImageProperties(dataUrl);
const pdfWidth = pdf.internal.pageSize.getWidth();
// const pdfHeight = pdf.internal.pageSize.getHeight();
const imgWidth = (imgProps.width * (25.4 / 96))
const imgHeight = (imgProps.height * (25.4/ 96))
const x_axis = (pdfWidth * 0.5) - (imgWidth * 0.5)
const y_axis = 0
pdf.addImage(dataUrl, 'PNG', x_axis, y_axis, imgWidth, imgHeight);
pdf.save('ticket.pdf');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment