How to pass HTML Code to JSPDF or similar?

490    Asked by ClaudineTippins in Salesforce , Asked on May 4, 2023

 I need some advice with the following use case:

We have a custom object "Invoice" and want to generate the Invoice PDF using a JavaScript Library that is able to convert an HTML Code of the Invoice Document into a PDF.

In another custom object called "Document Template" we store the full HTML code similar to this one:

https://github.com/anvilco/html-pdf-invoice-template

The HTML Code in the Document Template Object is prepared for Visualforce and contains the logic to populate the fields from the Invoice record and its related line items.

So far we are able to pass this in a way to have a proper looking Visualforce Page as in the above sample.

Now our idea was to use the VF generated HTML Code and pass it to any PDF-JavaScript Library to get it rendered as a PDF and save it to the ContentDocument Object.

So far we tried with JSPDF but actually we don't understand if that's really possible how we imagine and we have not been able to find any similar solution. We made it work to pass single text strings or something to get it rendered as a PDF but we are not able to pass the whole HTML Code.

So I am asking for some advice to understand if someone else had such a use case already, knows some documentation on it or has some better idea on how to get this done WITHOUT any external API services that renders the PDF based on the HTML Code?

The issue with renderas=PDF and blob.toPDF is that it is not supporting custom fonts.

Answered by Claudine Tippins

I've been in your situation and there are no alternatives to VF or Blob.toPdf when generating a PDF file from HTML code. The jsPDF library has some compatibility issues with SF's LWC framework when trying to convert HTML to PDF. We ended up using the app SDocs. The free version lets you set up 1 document template with HTML code.



Your Answer

Interviews

Parent Categories