PDF export with JxBrowser

This project demonstrates how to perform programmatic PDF export of HTML/CSS/JS content
with the help of JxBrowser.

PDF export

Problem 

When an organization wants to assemble and share some information, by far, the most common format for such reports would be PDF. The resulting files may contain a variety of data, including tables, charts, and other visualizations.

However, while the format is very popular, setting up an automated PDF generation system can often impose challenges. Depending on the solution, the organization may be required to lock into a specific reporting tool and/or the developers may need to use a program library with a steep learning curve requiring proficiency to use effectively. Also, the fidelity of the generated content may not be as high as expected.

In many situations, it’s preferable to use an alternative approach: render the content as HTML and print it to PDF via the built-in printing mechanism of a web browser.

Solution 

In this example, we use JxBrowser on a JVM web server to export table-like data to PDF upon request. As JxBrowser is built on top of Chromium, we use the HTML capabilities to create the table interface, and use CSS and JS libraries to establish a proper look&feel. At runtime, the page is filled with the actual data, and rendered to PDF via JxBrowser’s API. The resulting PDFs are the same as if they were printed by the Chromium browser for the desktop.

Another thing is that once the app is launched, the PDF export process requires no human intervention or any other interaction with the UI. This helps to run, say, scheduled reports every night, if needed.

The app can be hosted on a dedicated server or a Cloud VM, generating PDFs upon requests, such as a cron job, or a REST API request from another program.

To sum up, here is a short list of benefits:

  1. Fully automated PDF generation.
  2. Independence from the third-party report formats and specific reporting tools, high quality of the generated content.
  3. Reusing the same HTML/CSS/JS code for the PDF generation and on the web client if necessary, e.g. to generate a preview. The outcome is identical.

Licensing and reuse

The code of this project is provided under the MIT license. We encourage our clients to treat it as a source of inspiration, or even by directly using its parts in their applications.