The HTML is a leading file format for web development, and a majority of modern web browsers support HTML specifications. Though it’s platform-independent and can be viewed on any operating system, they are also prone to malicious scripts and cannot be easily shared over the internet. So, in order to avoid such circumstances and for long-term archival, we save a webpage as PDF (Portable Document Format). So in this article, we are going to discuss the steps on how to convert HTML to PDF using Java REST API.
HTML to PDF Conversion API
Aspose.HTML Cloud SDK for Java enables you to perform HTML manipulation operations within Java applications. We can also convert HTML to fixed-layout document formats (PDF or XPS). So in order to perform the conversion, we can load the source HTML(XHTML, MHTML, EPUB, Markdown) from cloud storage or provide the URL of a web page. Now in order to use the SDK, please add the following dependency in pom.xml of your maven build project so that aspose-html.jar can be added to the project:
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html-cloud</artifactId>
<version>20.7.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Convert HTML to PDF in Java
Please follow the steps given below to convert the webpage to PDF in the Java application.
- First of all, we need to specify details against Configuration.setAPP_SID.
- Secondly, we specify the Configuration.setAPI_KEY information.
- Thirdly, we set the setBasePath(..) details.
- Then we need to specify the setAuthPath(..) details.
- Set setUserAgent(…) as WebKit.
- For our own assistance, we are going to set setDebug(..) as true.
- The API also allows us to specify the margin details for the resultant file.
- The next step is to read the input HTML file and load it into the File instance.
- Now we need to create an instance of RequestBody class and parse media as “multipart/form-data”.
- Create an object of ConversionApi class.
- Since we are going to upload the file to cloud storage, so we also need to create an instance of StorageApi.
- Now we need to call PostConvertDocumentInRequestToPdf(..) to initiate the conversion process. This method accepts input file name, resultant file name, and resultant file margin and dimensions details as arguments.
- After the conversion, the result is returned to the Stream object containing raw bytes of the response body.
- Now we need to retrieve the deserialized response body of the successful response into the ResponseBody object.
- Download the resultant file from cloud storage to the ResponseBody object.
- Then finally, we are going to call our custom method to save the resultant PDF document on a local system drive.
Conclusion
In this article, we have learned the steps on how to convert a webpage to PDF using Java SDK. We have observed that with fewer code lines, the complete HTML is rendered to PDF format with complete fidelity. Apart from PDF format, the API also enables you to perform HTML to JPG, HTML to PNG, HTML to TIFF, HTML to BMP, and HTML to GIF conversion operations. Similarly, we can convert a Markdown file to HTML or convert MHTML to HTML format using the same API.
In case you encounter any issues while using the API, please feel free to contact us via the free product support forum.
Related Links
We recommend visiting the following links to learn more about