Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate wkhtmltopdf into my java webapplication?

I am trying to generate PDF document from HTML using wkhtmltopdf. But I am not able to figure out how to integrate wkhtmltopdf into my webapplication as there are no jar files in the tar file provided by the author. Can someone help me to set up this?

like image 562
SRy Avatar asked Feb 18 '23 07:02

SRy


1 Answers

The wkhtmltopdf program is provided in a form of a stand-alone executable. It uses Qt Webkit module from Qt library for rendering the HTML and producing the PDF output. It is written in C/C++ thus you will be unable to find any JAR files.

You can save the HTML to a temporary file, use Runtime.exec() or ProcessBuilder to fork a new process from Java and when the process finishes, read the PDF file.

Qt Jambi might help you as well with its QWebView class might help you as well. Alternatively, you can implement your own wrapper over Qt Webkit in C and create Java bindings using JNI for calling it from Java in-process.

Depending on the target environment, you have to assess if you can afford forking processes from Java or implementing native calls as it might be a possible security risk.

like image 190
Matej Avatar answered Mar 05 '23 17:03

Matej