Convert HTML to PDF in Java using REST API

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. Your converted HTML files to PDF documents still retains the original text and layouts of html web page, even with compressed data. HTML to PDF converter online API is helpful for reading offline articles or sharing password protected PDF files on the internet. In this article, I will demonstrate how to convert HTML to PDF in Java using REST API.

The following topics shall be covered in this tutorial:

Java HTML to PDF Converter REST API – Java SDK Installation

For converting HTML to PDF 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 get 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

You can convert and export HTML to PDF file by following the simple 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

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

  • Create an instance of ConvertApi
  • Create an instance of the ConvertSettings
  • Provide the cloud storage name
  • Set the input HTML file path and the output file format as “pdf”
  • Now, set the output PDF file path
  • 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

You can also convert HTML document to PDF file using some advanced options programmatically by following the steps given below:

  • Create an instance of ConvertApi
  • Create an instance of the ConvertSettings
  • Next, provide the cloud storage name
  • Set the input HTML file path and output file format as “pdf”
  • Create an instance of the PdfConvertOptions
  • Set various convertOptions like setFromPage, setPagesCount, setZoom, setImageQuality, setPassword, setHeight, setMarginTop, setDpi, etc.
  • Set convertOptions and the the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Finally, perform conversion using the convert_document() method 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 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 free.

Conclusion

This brings us to the end of this article. In this article, we have learned:

  • how to export HTML to PDF document using Java library programmatically;
  • how to convert HTML file into PDF using some advance 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;

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.

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

For any queries/discussions about HTML to PDF Converter API, please feel free to contact us on 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 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 resultant PDF file will be available instantly after converting 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

We recommend you to visit the following articles to learn about: