Home > Article > Web Front-end > How to Export PDFs with Correct CSS Rendering and Images Using jspdf and HTML2Canvas?
Exporting PDFs with CSS Rendering Using jspdf and HTML2Canvas
Issue: When using jspdf.debug.js to export data from a website, users encounter issues with CSS not being rendered in the exported PDF, and images appearing blank.
Solution:
jspdf does not natively support CSS rendering. However, this can be resolved by incorporating HTML2Canvas into the process.
HTML2Canvas allows you to convert HTML elements into canvas elements, which can then be added to the PDF using jspdf's addHTML method.
Code:
var pdf = new jsPDF('p', 'pt', 'letter'); pdf.addHTML($('#ElementYouWantToConvertToPdf')[0], function() { pdf.save('Test.pdf'); });
Implementation:
After adding the HTML2Canvas JS, replace jspdf's fromHTML method with addHTML(). The ElementYouWantToConvertToPdf selector should point to the HTML element you wish to export.
Note: If you don't find the addHTML() method, refer to the documentation for further clarification.
Additional Considerations:
The above is the detailed content of How to Export PDFs with Correct CSS Rendering and Images Using jspdf and HTML2Canvas?. For more information, please follow other related articles on the PHP Chinese website!