How to print div as pdf


Working on a project we got the the requirement of printing a recipient from the website.

The solution we got is a simple as use:  https://github.com/jasonday/printThis
This will allow us to do something like this:

Most browsers can handle this with print, however chrome mobile version does not support this.

So the option was to create a pdf with the recipient by using: https://parall.ax/products/jspdf

However they don't support print with your css, so this could not be used as side solution, so there is this other tool:  https://html2canvas.hertzen.com/

Combine both and you have what you need, using code similar to this: https://github.com/MrRio/jsPDF/issues/434, I say similar because this code no longer work since onrendered is deprecated, so here is the version I used.

You can tell html2canvas to ignore compoents when creating the canvas for your div as follows:

I hope this helps you :)

Comments