The HTML is a leading file format for web development and a majority of the modern web browsers support HTML specifications and can easily render the web pages inside them. Also, the HTML is platform-independent, therefore it can be viewed on any operating system running over Desktop or Mobile devices. However, recently there have been scenarios where malicious scripts have been embedded inside the HTML content, and when these pages were rendered inside the browsers, the data breach occurred.
Therefore, in order to avoid such situations, the option is to convert the HTML files to a format that can avoid such circumstances as well as preserve the document layout so that all the users have the same experience when viewing the document. The PDF (Portable Document Format) is a viable choice because it ensures the same user experience when viewing the document on any platform using a supported application. In this article, we are going to discuss the steps on how to save HTML files to PDF format using Java REST API.
REST API vs Conventional API
A plethora of applications can be found in the market that promises the capabilities to render HTML files to PDF format but they incur cost and time. You need to spend time setting up the applications as well as you need to pay the licensing fee for the whole application, no matter which features you utilize or not. Furthermore, these applications allow you to perform the conversion/manipulation operations in a sequential manner and you can perform the processing one after another. Whereas, the programming APIs facilitate you to accomplish the same requirements in a much convenient and effective manner.
However, the REST APIs are one step further than conventional APIs because you do not need to install anything locally. All the processing and manipulation is performed over the Cloud. It saves you from the additional efforts of configuration and versioning, and you stay more focused on your business requirements. Also from savings perspectives, you only pay for what you use.
HTML manipulation Cloud API
We have developed a RESTful API specific for HTML file manipulation and conversion to one of the fixed-layout document formats including PDF or XPS format. The source document can be an HTML document from the cloud storage (also XHTML, MHTML, EPUB, Markdown), or a Web page by its URL.
Converting HTML to PDF using Java REST API
Our Aspose.HTML Cloud SDK for Java enables you to convert HTML files into PDF format. So in order to use the SDK, the first step is to install the SDK. The SDK is available for download on (Maven or GitHub).
Given below are instructions on how to use aspose-html.jar in the Maven build project. Add the following dependency to your project’s POM:
<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>
The following steps and code snippet shows the conversion of HTML file to PDF format.
- 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 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 about the capabilities of Aspose.HTML Cloud SDK for Java and have explored examples of how to save an HTML file to PDF format. Apart from PDF format, you may also render the output to raster image formats such as JPEG, PNG, TIFF, BMP, GIF. At the same time, the API is also capable of converting a Markdown file into HTML as well as converting MHTML to HTML format.