DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • How To Convert ODF Files to PDF in Java
  • How to Convert Excel and CSV Documents to HTML in Java

Trending

  • Segmentation Violation and How Rust Helps Overcome It
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Enhancing Avro With Semantic Metadata Using Logical Types
  1. DZone
  2. Coding
  3. Languages
  4. 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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Updated Jul. 29, 20 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
35.0K Views

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.

One particularly important goal for these operations will be to maintain a high level of accuracy when making the transition between the two formats. Advanced design elements including CSS, Javascript, and images will all be preserved post-conversion. One detail to bear in mind, images should be included as absolute URLs or in base 64 inline form.

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

XML
xxxxxxxxxx
1
 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>


That will allow Jitpack to dynamically compile our library. Second, we will also need our dependency reference in there as well:

XML
xxxxxxxxxx
1
 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>


Next, let us turn our attention to our controller. We will first need our imports to be added to the top of the file.

Java
xxxxxxxxxx
1
 
1
// Import classes:
2
import com.cloudmersive.client.invoker.ApiClient;
3
import com.cloudmersive.client.invoker.ApiException;
4
import com.cloudmersive.client.invoker.Configuration;
5
import com.cloudmersive.client.invoker.auth.*;
6
import com.cloudmersive.client.ConvertDocumentApi;


And now we can call our function, so let’s have a look at this example code below:

Java
xxxxxxxxxx
1
17
 
1
ApiClient defaultClient = Configuration.getDefaultApiClient();
2
 
3
// Configure API key authorization: Apikey
4
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
5
Apikey.setApiKey("YOUR API KEY");
6
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
7
//Apikey.setApiKeyPrefix("Token");
8
 
9
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
10
File inputFile = new File("/path/to/file"); // File | Input file to perform the operation on.
11
try {
12
    byte[] result = apiInstance.convertDocumentHtmlToPdf(inputFile);
13
    System.out.println(result);
14
} catch (ApiException e) {
15
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentHtmlToPdf");
16
    e.printStackTrace();
17
}


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:

Java
 
xxxxxxxxxx
1
 
1
// Import classes:
2
import com.cloudmersive.client.invoker.ApiClient;
3
import com.cloudmersive.client.invoker.ApiException;
4
import com.cloudmersive.client.invoker.Configuration;
5
import com.cloudmersive.client.invoker.auth.*;
6
import com.cloudmersive.client.ConvertWebApi;


Now we can call convertWebHtmlToPdf:

Java
 
xxxxxxxxxx
1
17
 
1
ApiClient defaultClient = Configuration.getDefaultApiClient();
2
 
3
// Configure API key authorization: Apikey
4
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
5
Apikey.setApiKey("YOUR API KEY");
6
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
7
//Apikey.setApiKeyPrefix("Token");
8
 
9
ConvertWebApi apiInstance = new ConvertWebApi();
10
HtmlToPdfRequest input = new HtmlToPdfRequest(); // HtmlToPdfRequest | HTML to PDF request parameters
11
try {
12
    byte[] result = apiInstance.convertWebHtmlToPdf(input);
13
    System.out.println(result);
14
} catch (ApiException e) {
15
    System.err.println("Exception when calling ConvertWebApi#convertWebHtmlToPdf");
16
    e.printStackTrace();
17
}


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 convertWebUrlToPdf:

Java
xxxxxxxxxx
1
17
 
1
ApiClient defaultClient = Configuration.getDefaultApiClient();
2
 
3
// Configure API key authorization: Apikey
4
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
5
Apikey.setApiKey("YOUR API KEY");
6
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
7
//Apikey.setApiKeyPrefix("Token");
8
 
9
ConvertWebApi apiInstance = new ConvertWebApi();
10
UrlToPdfRequest input = new UrlToPdfRequest(); // UrlToPdfRequest | URL to PDF request parameters
11
try {
12
    byte[] result = apiInstance.convertWebUrlToPdf(input);
13
    System.out.println(result);
14
} catch (ApiException e) {
15
    System.err.println("Exception when calling ConvertWebApi#convertWebUrlToPdf");
16
    e.printStackTrace();
17
}


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:

Java
xxxxxxxxxx
1
34
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.EditPdfApi;
7
 
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
 
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
 
16
EditPdfApi apiInstance = new EditPdfApi();
17
String ownerPassword = "ownerPassword_example"; // String | Password of a owner (creator/editor) of the PDF file (required)
18
String userPassword = "userPassword_example"; // String | Password of a user (reader) of the PDF file (optional)
19
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
20
String encryptionKeyLength = "encryptionKeyLength_example"; // String | Possible values are \"128\" (128-bit RC4 encryption) and \"256\" (256-bit AES encryption).  Default is 256.
21
Boolean allowPrinting = true; // Boolean | Set to false to disable printing through DRM.  Default is true.
22
Boolean allowDocumentAssembly = true; // Boolean | Set to false to disable document assembly through DRM.  Default is true.
23
Boolean allowContentExtraction = true; // Boolean | Set to false to disable copying/extracting content out of the PDF through DRM.  Default is true.
24
Boolean allowFormFilling = true; // Boolean | Set to false to disable filling out form fields in the PDF through DRM.  Default is true.
25
Boolean allowEditing = true; // Boolean | Set to false to disable editing in the PDF through DRM (making the PDF read-only).  Default is true.
26
Boolean allowAnnotations = true; // Boolean | Set to false to disable annotations and editing of annotations in the PDF through DRM.  Default is true.
27
Boolean allowDegradedPrinting = true; // Boolean | Set to false to disable degraded printing of the PDF through DRM.  Default is true.
28
try {
29
    byte[] result = apiInstance.editPdfSetPermissions(ownerPassword, userPassword, inputFile, encryptionKeyLength, allowPrinting, allowDocumentAssembly, allowContentExtraction, allowFormFilling, allowEditing, allowAnnotations, allowDegradedPrinting);
30
    System.out.println(result);
31
} catch (ApiException e) {
32
    System.err.println("Exception when calling EditPdfApi#editPdfSetPermissions");
33
    e.printStackTrace();
34
}


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.

HTML PDF Java (programming language) Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • How To Convert ODF Files to PDF in Java
  • How to Convert Excel and CSV Documents to HTML in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!