How to Convert HTML to PDF in Java
How to Convert HTML to PDF in Java
Converting between the file format heavyweights of HTML and PDF just got a whole lot easier.
Join the DZone community and get the full member experience.Join For Free
There are many reasons why PDF is the most commonly used document format in the world. For one, its level of compatibility is unmatched — PDFs can be viewed with perfect fidelity on PC, Mac, Linux, web browsers, and mobile platforms with no problems whatsoever. Add to this its print quality and immutability, and you have a clear go-to choice when it comes to convenience.
Turning now to the matter of conversion, however, you start to run into some problems. There is no clear and simple means by which you can directly create a PDF from HTML code with Java. Instead, a whole process of parsing and rendering must first be performed, which is about as much fun as it sounds. So how can we achieve the high quality results that we require without wasting a ton of development hours on the problem?
Today, we will be looking at how to accomplish this quickly and easily through the use of an API. After just a few simple setup steps, we will be able to perform a variety of useful functions relating to easing the transition between HTML and PDF:
- HTML document to PDF.
- HTML string to PDF.
- URL to PDF.
- Editing PDFs.
Without further ado, let's dive straight in.
We begin with our library installation, which will require first a repository reference for our Maven POM file
That will allow Jitpack to dynamically compile our library. Second, we will also need our dependency reference in there as well:
Next, let us turn our attention to our controller. We will first need our imports to be added to the top of the file.
And now we can call our function, so let’s have a look at this example code below:
To make this work, we will need to ensure the following things:
- Provide a valid HTML document as an inputFile.
- Call the function convertDocumentHtmlToPdf using our API instance.
- Set the API key, which is available from the Cloudmersive website for free (forever), allowing up to 1,000 calls across all available APIs.
And just like that, you are already set up. Note that the above function is designed to work with HTML documents. So, what if we have an HTML string instead? The process is essentially the same, but we will be calling a different function, which is part of the ConvertWebApi. This means we will need to change/add to our imports to reflect this:
Now we can call
The key difference here is that instead of inputting an HTML file, we will add the HTML string as part of our
HtmlToPdfRequest object. Everything else is the same as before and just as simple to get off the ground. Within this API, there are also related functions that allow you to convert the input HTML into a PNG image, a DOCX document, or a plain text string.
Let us move on to look at creating PDFs from websites directly, using URLs. We will be using ConverWebApi again, so make sure it is on your list of imports. The function we need is called
Similar to the previous function, we create a request object, then pass it our desired URL, and some optional parameters, such as scale factor for the output. Pretty simple. There are also related functions that allow you to create a screenshot image PNG or a text string from a URL.
So what else can this API do with PDFs? If you would like to add some security, you can encrypt your PDF file with a password using this function below:
Notice that with the various parameters, you can achieve a high level of control over the various permissions, such as printing, editing, and content extraction. You can also set the length of the encryption key and the password itself. The reverse operation is also available through
editPdfDecrypt, allowing you to remove password protection and unlock your PDF files. Within this API, there also exist functions to get and set PDF metadata, transfer pages between PDF documents, and edit annotations as well as form fields.
Opinions expressed by DZone contributors are their own.