The HTML is a leading file format for web development and a majority of the modern web browsers support HTML specifications. Also, the HTML is platform-independent, therefore it can be viewed on any operating system running over Desktop or Mobile devices. But 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, a PDF (Portable Document Format) can be a viable choice. In this article, we are going to discuss the steps on how to convert HTML to PDF using Java REST API.
HTML Conversion API
Aspose.HTML Cloud SDK for Java enables you to perform HTML manipulation operations within Java applications. It also provides the feature to convert an HTML to fixed-layout document formats i.e. PDF or XPS format. 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 get started, we need to download the SDK which is available for download over Maven and GitHub. 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>http://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 HTML 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 in 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
In this article, we have learned the steps on how to convert an HTML 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 convert HTML to JPEG, HTML to PNG, HTML to TIFF, HTML to BMP, and HTML to GIF. Similarly, you may consider converting a Markdown file into HTML as well as converting 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.
We recommend visiting the following links to learn more about