Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an HTML file to PDF using wkhtmlpdf in Java

I want to convert an HTML file into a PDF file using wkhtmltopdf. wkhtmltopdf is the best option for me as it renders the HTML file using WebKit. The problem is that I want to do the same using Java but wkhtmltopdf does not provide any Java API.

I can use Runtime.exec() or ProcessBuilder to fork a new process from Java and create the PDF output using wkhtmtopdf in that process. But, as I am developing a web based application, I am not allowed to create so many new processes in the server.

Is there any other way so that I can use wkhtmltopdf? I really want to use it as it's giving me the exact output.

Or, is there any other open source browser engine that provides a Java API that can render my HTML page just like wkhtmltopdf?

like image 396
Surajeet Bharati Avatar asked Feb 10 '15 22:02

Surajeet Bharati


1 Answers

Give htmltopdf-java a try. It uses the native libraries generated by wkhtmltopdf, so you should expect the same result with more control over the flow.

(I am the author of this library)

like image 146
Ben Barkay Avatar answered Oct 17 '22 18:10

Ben Barkay