Skip to content

Instantly share code, notes, and snippets.

@haris-aqeel
Created March 12, 2024 18:41
Show Gist options
  • Save haris-aqeel/8b91e1cad5c4e5401c95c915d845a0c6 to your computer and use it in GitHub Desktop.
Save haris-aqeel/8b91e1cad5c4e5401c95c915d845a0c6 to your computer and use it in GitHub Desktop.
// Assuming you're using a modern import method
import { jsPDF } from "jspdf";
// Create a jsPDF instance
const doc = new jsPDF();
// Your HTML string
const htmlString = `
<h1>Hello World</h1>
<p>This is a basic example of HTML to PDF conversion.</p>
`;
// Convert the HTML string to a Blob object
const blob = new Blob([htmlString], { type: 'text/html' });
// Create a temporary URL for the blob
const url = URL.createObjectURL(blob);
// Use the html method to draw the HTML content in the PDF
// Note: The html method is asynchronous and returns a Promise
doc.html(document.body, {
callback: function (doc) {
doc.save("example.pdf"); // This triggers the download of the PDF
},
x: 10,
y: 10,
html2canvas: {
scale: 0.5 // Adjust scale according to your needs
}
});
// It's important to revoke the temporary URL after you're done to release memory
URL.revokeObjectURL(url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment