· tutorials · 6 min read
How to Convert HTML to PDF using Flying Saucer
Step into the world of Java-based HTML to PDF conversions with Flying Saucer, complete with easy-to-follow code examples.
Introduction
Flying Saucer is a Java library that excels at converting XHTML/CSS content into PDFs, offering a high level of precision in rendering. This guide delves into the process of converting HTML to PDF using Flying Saucer, highlighting its ease of use and effectiveness with practical code examples.
Why Flying Saucer?
- CSS Support: Exceptional support for CSS 2.1, enabling sophisticated document styling.
- Ease of Use: Simple API for generating PDFs from HTML content.
- Integration: Seamless integration with Java applications and libraries.
Getting Started with Flying Saucer
Step 1: Add Flying Saucer Dependencies
To utilize Flying Saucer, you need to include its core library along with a PDF renderer, such as iText, in your project. For Maven users, add the following dependencies to your pom.xml
:
Step 2: Convert HTML to PDF with Flying Saucer
Flying Saucer requires well-formed XHTML to produce PDFs. If your HTML is not XHTML-compliant, you might need to preprocess it to conform to XHTML standards.
Here’s how you can convert XHTML to PDF using Flying Saucer:
In this example, ITextRenderer
is used to render the XHTML content as a PDF. The setDocument
method loads the XHTML file, layout
prepares the content for rendering, and createPDF
generates the PDF.
Enhancing PDFs with CSS
Flying Saucer’s strength lies in its CSS rendering capabilities. To leverage CSS for styling your PDF, include a <link>
to your CSS file within the <head>
of your XHTML document, just like in a regular HTML file.
A better approach: HTML to PDF using Templated
The examples above demonstrate how to convert HTML to PDF and web pages to PDF using libraries. However, when it comes to tasks like generating PDFs using templates or keeping track of generated PDFs, additional steps are required.
For instance, to keep track of generated PDFs, you’ll need to develop your own system for tracking the files created. Similarly, if you want to use custom templates, such as those for invoice or certificates generation, you must create and manage those templates manually.
An alternative solution is to utilize Templated, an API-based platform designed for PDF and Image generation, which is ideal for handling such use cases. Their PDF generation API is powered by a Chromium-based rendering engine that fully supports JavaScript, CSS, and HTML.
This approach simplifies the process and eliminates the need for extensive manual template management and tracking. To get started with PDF generation using Templated, follow the documentation and harness the power of this efficient solution.
Create PDFs with templates
Using Templated API to generate PDFs you can design your PDF template using a drag-and-drop editor.
Check the video below to see an example of the editor:
After logging in you will see your Dashboard where you can manage your templates or create new ones:
From your Dashboard, you can design your own templates or customize a existing one from our Template Gallery. Bellow is the Certificate of Achievement Template you can use.
There are 100+ free templates available that you can pick and customize to your needs.
To start using Templated API, you need to get your API key that can be found on the API Integration tab on your dashboard.
Now that you have your Templated account ready, let’s see how you can integrate your application with the API. In this example we will be using a certificate template to generate PDFs.
and If we check response
we have the following
In the code above, using Templated to convert HTML to PDF is pretty simple. No additional libraries need to be installed. You only need to make a single API call, providing your data as the request body. That’s all there is to it!
You can use the renderUrl
from the response to download or distribute the generated PDF.
Other Java libraries
There are other Java libraries capable of converting HTML to PDF and you can find more information about it in this article on How To Convert HTML to PDF with Java.
More ways to convert HTML to PDF
You you want to learn how to convert HTML to PDF in other languages here are other resources for you to explore:
- How To Convert HTML to PDF with Java
- How To Convert HTML to PDF with C#
- How To Convert HTML to PDF with PHP
- How To Convert HTML to PDF with Python
- How To Convert HTML to PDF with Node.js
Conclusion
Flying Saucer offers a Java-based solution for generating PDFs from HTML content, particularly excelling in rendering CSS-styled documents. Its straightforward API and powerful CSS rendering capabilities make it an ideal choice for applications requiring precise control over document layout and style in their PDF conversions.
Automate your content with Templated