How to Convert HTML to PDF in Java using REST API

Convert HTML to PDF in Java using REST API

As a Java developer, you can convert your HTML (HyperText Markup Language) files to PDF (Portable Document Format) documents programmatically on the cloud using GroupDocs.Conversion REST API. There are several reasons why you might want to convert HTML to PDF. PDFs are more secure, easier to print, long-term archiving, more portable, and compatible with a wider range of software than HTML files. Morerover, converting HTML to PDF can be a useful way to share documents and ensure that they are displayed consistently across different platforms. In this tutorial, I’ll show you how to convert HTML to PDF in Java using REST API.

This tutorial will go through the following topics:

Java HTML to PDF Converter REST API - Java SDK Installation

For converting HTML pages to a PDF document without losing formatting, I will be using the Java SDK of GroupDocs.Conversion Cloud API. It helps you to incorporate GroupDocs.Conversion Cloud services in your Java applications quickly and easily. This is the best HTML to PDF converter API that retains the original text and layouts of your web pages. Groupdocs.Conversion APIs allow you to convert your documents and images of any supported file format to any format you need. Easily convert between more than 50 types of documents and images like Word, PowerPoint, Excel, PDF, HTML, CAD, raster images, etc.

You can download the API’s JAR file or install using Maven configurations. Add repository and dependency to your project’s POM.xml. Below are the steps for Maven:

Maven Repository:

<repository>
    <id>groupdocs-artifact-repository</id>
    <name>GroupDocs Artifact Repository</name>
    <url>https://repository.groupdocs.cloud/repo</url>
</repository>

Maven Dependency:

 <dependency>
    <groupId>com.groupdocs</groupId
    <artifactId>groupdocs-conversion-cloud</artifactId>
    <version>22.10</version>
    <scope>compile</scope>
</dependency>

Please obtain your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, please add in the code as shown below:

Convert HTML Document to PDF File in Java using REST API

To convert HTML to PDF in Java using a REST API, you will need to follow these steps as mentioned below:

Upload the File

Firstly, upload the HTML document to the cloud storage using the code snippet as shown below:

As a result, the uploaded HTML file will be available in the files section of your dashboard on the cloud.

Convert HTML to PDF in Java

This section demonstrates how to convert HTML to PDF without losing formatting programmatically by following the steps given below:

  • Firstly, create an instance of ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Thirdly, provide the cloud storage name
  • Then, set the input HTML file path and the output file format as “pdf”
  • Now, set the output PDF file path
  • Next, create ConvertDocumentRequest with ConvertSettings
  • Finally, call the convert_document() method with ConvertDocumentRequest

The following code snippet shows how to transform HTML to PDF online using REST API:

Input HTML File:

How to transform HTML to PDF in java

Convert HTML to PDF in Java

Resultant PDF File:

How to change HTML into PDF in java

Convert HTML to PDF in Java

Download the Converted File

The above code sample will save the converted PDF file to the cloud. You can download it using the following code snippet:

This is how HTML to PDF converter library in java works. In the next section, let’s explore more advanced conversion settings using Java API.

Convert HTML to PDF Online in Java using Advanced Options

In this section, you can convert HTML file to PDF document using some advanced settings programmatically by following the steps given below:

  • Firstly, create an instance of ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Next, provide the cloud storage name
  • Then, set the input HTML file path and output file format as “pdf”
  • Now, create an instance of the PdfConvertOptions
  • Set various convert options like setFromPage, setPagesCount, setZoom, setImageQuality, setPassword, setHeight, setMarginTop, setDpi, etc.
  • Set convert options and the output file path
  • Next, create ConvertDocumentRequest with ConvertSettings
  • Last, perform the conversion process using the convert_document() class with ConvertDocumentRequest

The following code example shows how to convert HTML document to PDF file using advanced settings.

Output File Step1:

How to convert HTML file to PDF online free

Convert HTML to PDF Online in Java using Advanced Options

Output File Step2:

How to convert chrome HTML document to PDF in java

Convert HTML to PDF Online in Java using Advanced Options

Please follow the steps mentioned earlier to upload and download the files.

Online HTML to PDF Converter Free

How to convert HTML file to PDF online for free? Please try free HTML to PDF converter to generate PDF from HTML online. It was developed using the above API to convert HTML to PDF online for free.

Summing up

We have now reached the conclusion of this article. This article has taught us:

  • how to export HTML to PDF document using Java library programmatically;
  • how to convert HTML file into PDF using some advanced options in Java;
  • programmatically upload the HTML file to the cloud and then download the converted PDF file from the cloud;
  • online convert HTML to PDF free using HTML to PDF converter program;

Moreover, you can learn more about GroupDocs.Conversion file converter API using the documentation. We also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Java SDK complete source code is freely available on the Github. Please check the GroupDocs.Conversion Cloud SDK for Java Examples here.

Additionally, we advise you to refer to our Getting Started guide.

Finally, groupdocs.cloud is writing new blog articles on different file formats conversions using REST API. So, please stay in touch for regular updates.

Ask a question

Let us know if you have any questions or need further assistance with HTML to PDF Converter API, via our free support forum.

FAQs

How do I convert HTML to PDF in Java?

Please follow this link to learn the Java code snippet for how to change HTML to PDF file, quickly and easily.

Can we convert HTML to PDF in Java using REST API?

Yes, you can transform HTML to PDF in Java. Firstly, create an instance of ConvertApi, set the values of the convert settings, and invoke the convertDocument method with ConvertDocumentRequest to convert HTML to PDF without losing formatting.

How to convert HTML to PDF online for free?

HTML to PDF converter online free allows you to convert HTML to PDF free, quickly and easily. Once the online conversion of HTML to PDF is completed, you can instantly download the converted PDF file on your PC.

How do I online convert HTML to PDF?

  • Open free HTML to PDF converter online
  • Click inside the file drop area to upload HTML web page or drag & drop HTML file.
  • Click on Convert Now button, free online HTML to PDF converter will convert HTML file to PDF online free.
  • Download link of the resultant PDF file will be available instantly after converting the HTML file to PDF free.

How to install HTML to PDF Java library?

You can download and install Java HTML to PDF converter library to process, manipulate, and create PDF from HTML in Java programmatically.

How to convert HTML to PDF in windows?

Please visit this link to download HTML to PDF converter offline for windows. This HTML to PDF converter free download software can be used to export HTML to PDF in windows quickly, with a single click.

See Also

To learn more, we suggest reading the following articles: