/ docker

PDF Generation - Pain And Solutions

I've worked in many projects in my life and I'd say that in 80% of the cases, the clients end up needing some sort of PDF report. So along the way, we had way too many different solutions for this requirement, but there's always an issue.

Solution 1
One pretty good solution we've found for one of our projects was using Kendo to generate the PDF. It's great because everything happens in the client side, no server side code is required. And with that comes a few issues, and the biggest of all was cross-browser compatibility. It would work slightly different on each browser, and some browsers and it would just not work, IE8 is an example. But it's there, the client is happy with their known issues.

Solution 2

Source code for this solution: https://github.com/thiagospassos/PDFGenerator

Another solution we've found, and the merit goes to JK, was running a .NET console application leveraging Pechkin to generating the PDF. Initially, I thought it was a crazy idea having a console app to do it. Alright, maybe having a console app deployed with every single web application was indeed crazy, so instead, we created a microservice that its only responsibility is to generate PDFs, you give it a HTML and it returns a PDF, as simple as that, and it worked beautifully. The whole source code for our solution is here.

Solution 3
If you have a Docker environment, another great option is to use this image to run a HTML to PDF service.

Use this command to run the service
docker run -d -P openlabs/docker-wkhtmltopdf-aas

Now run docker ps to confirm which port your service is running on
htmltopdf

For you to test it, you should pass a html converted to base64. You can use https://www.base64encode.org/ for this:
htmltopdf1

Then you can test it with Postman. As soon as you click on Send, you should see a popup to save the generated pdf.
htmltopdf2-1

And there you go, your PDF
htmltopdf3

Helper
Something I've used in order to generate elegantly the HTML itself before sending to the PDF Generator was using Handlebars.NET, this way we don't need to either concatenate the whole string nor levaring the whole MVC framework to use Razor in the server. And I loved it.

Now you tell me how you're doing it? Do you have any cool solution or pain to share?

Cheers

Thiago Passos

Thiago Passos

I'm Thiago Passos, a Solution Architect working for SSW sharing what I've been working with and learning. Love technology, dancing and I get unfriendly when I'm hungry.

Read More