How to Convert an HTML String to a PDF in Java
If you’re looking to create a PDF document out of a website page, you can accomplish your goal in a few ways. You can elect to use the website’s URL to render a screenshot, or you can use the URL to access the website’s underlying HTML code. Or, even more to the point, you can copy & use the HTML strings which formulate the website’s visuals in the first place. With our HTML to PDF API, you can easily create a PDF of a website with nothing more than this HTML code itself. Within this API’s input parameters, you can also specify the production loading time, the scale factor, and whether background graphics should be included or not.
It’s free to use this API — all you need to do is register a free account on our website, and you’ll receive a secure API key with a hard limit of 800 uses per month (and zero additional commitments). This is intended to be a simple & easy solution for smaller scale projects; below, you can follow steps to structure your API call in Java using ready-to-run code examples from the Cloudmersive API console.
First, let’s install the API client with Maven. We’ll start by adding a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
After that, let’s add one to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now let’s turn our attention to the controller. Include the following imports at the top of your file:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertWebApi;
Lastly, let’s call the function, and we’re all done. We can authenticate our API key where indicated in the comments, and we can then satisfy our input parameters. The input parameter format is included in a separate snippet at the bottom of this article:
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");ConvertWebApi apiInstance = new ConvertWebApi();
HtmlToPdfRequest input = new HtmlToPdfRequest(); // HtmlToPdfRequest | HTML to PDF request parameters
try {
byte[] result = apiInstance.convertWebHtmlToPdf(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertWebApi#convertWebHtmlToPdf");
e.printStackTrace();
}a
HTML to PDF request parameters:
{
"Html": "string",
"ExtraLoadingWait": 0,
"IncludeBackgroundGraphics": true,
"ScaleFactor": 0
}