This tutorial and the sample source code explain converting a PDF document to HTML using PDF to HTML functionality from Web API. The source code uses Java programming language for this tutorial. The users can also convert scanned images into HTML, fully preserving the text, vectors, pictures, fonts, and format.

  1. Features of PDF to HTML API Endpoint
  2. PDF to HTML Endpoint Parameters
  3. How to Convert PDF to HTML in Java
  4. Step-by-Step Guide to Convert PDF to HTML

Features of PDF to HTML API Endpoint

The Web API provides tools and functionalities to convert any PDF document or scanned image to HTML. This process works efficiently to preserve the formatting, the text, the vectors, and the images after conversion. The API uses the method of automatic document classification. The users can utilize the document classifier endpoint to automatically detect the incoming document’s class based on their keywords-based rules. Moreover, the users can find any vendor or template required using the defined rules.

The Web API also provides a highly secure platform for its consumers. The platform has implemented a high-security process as the users must provide sensitive information. The API transmits the data via an encrypted connection. The users can go through the security protocols in detail here.

PDF to HTML Endpoint Parameters

Following are the PDF to HTML endpoint parameters:

  1. url: It is required to provide the URL to the source file. The platform supports URLs from Dropbox, Google Drive, and built-in file storage of The API takes the URL as input from this parameter and loads the PDF form to make changes whenever a user makes such a request.
  2. httpusername: An optional parameter provides an HTTP auth user name to access the source URL if required.
  3. httppassword: An optional parameter provides an HTTP auth password to access the source URL if required.
  4. pages: It is an optional parameter that must be a string. It helps in providing a comma-separated list of the pages required. The users can set a page range by using “ -.” Moreover, the users can leave the parameter empty to indicate selecting all the pages.
  5. unwrap: It is an optional parameter. It unwraps the lines and forms them into a single line in the table cells. It is done by enabling lineGrouping.
  6. rect: It is an optional parameter and must be a string. It provides coordinates for extraction.
  7. lang: An optional parameter sets the language for OCR to use for scanned JPG, PNG, and PDF document inputs to extract text from them.
  8. inline: It is an optional parameter. It is set to true to return data as inline or false to return the link to an output file.
  9. lineGrouping: It is an optional parameter and must be a string. It enables grouping within the table cells.
  10. async: It is an optional parameter. It helps in running the processes asynchronously. It returns the JobId to check the state of the background job.
  11. name: It is an optional parameter and must be a string. It provides the name of the generated output file.
  12. expiration: It is an optional parameter that provides the expiration time for the output link.
  13. profiles: It is an optional parameter and must be a string. This parameter helps in setting additional configurations and extra options.

How to Convert PDF to HTML in Java

The following source code explains how to convert any PDF document to HTML using the PDF to HTML API endpoint. The sample code in Java demonstrates converting a PDF document using the API. The below code takes the sample PDF file for classification and uses Artificial Intelligence (AI) to detect the data types like font, text, format, and others. It then writes all this data in HTML and returns it to the user as an HTML file.

Source Code in Java

Following is the sample code in Java to explain using PDF to HTML endpoint:

//                                                                                           //
// Download Free Evaluation Version From:       //
//                                                                                           //
// Also available as Web API! Get Your Free API Key:               //
//                                                                                           //
// Copyright © 2017-2020 ByteScout, Inc. All rights reserved.                                //
//                                                                 //
//                                                                            //
//                                                                                           //

import okhttp3.*;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main
   // Get your own API Key by registering at
   final static String API_KEY = "***************";
   // Direct URL of source PDF file.
   // You can also upload your own file into and use it as url. Check "Upload File" samples for code snippets:   
   final static String SourceFileUrl = "";
   // Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'.
   final static String Pages = "";
   // PDF document password. Leave empty for unprotected documents.
   final static String Password = "";
   // Destination HTML file name
   final static Path DestinationFile = Paths.get(".\\result.html");
   // Set to `true` to get simplified HTML without CSS. Default is the rich HTML keeping the document design.
   final static boolean PlainHtml = false;
   // Set to `true` if your document has the column layout like a newspaper.
   final static boolean ColumnLayout = false;
   public static void main(String[] args) throws IOException
       // Create HTTP client instance
       OkHttpClient webClient = new OkHttpClient();
       // Prepare URL for `PDF To HTML` API call
       String query = "";
       // Make correctly escaped (encoded) URL
       URL url = null;
           url = new URI(null, query, null).toURL();
       catch (URISyntaxException e)
       // Create JSON payload
      String jsonPayload = String.format("{\"name\": \"%s\", \"password\": \"%s\", \"pages\": \"%s\", \"simple\": \"%s\", \"columns\": \"%s\", \"url\": \"%s\"}",

       // Prepare request body
       RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload);

       // Prepare request
       Request request = new Request.Builder()

           .addHeader("x-api-key", API_KEY) // (!) Set API Key
           .addHeader("Content-Type", "application/json")
       // Execute request
       Response response = webClient.newCall(request).execute();
       if (response.code() == 200)
           // Parse JSON response
           JsonObject json = new JsonParser().parse(response.body().string()).getAsJsonObject();
           boolean error = json.get("error").getAsBoolean();
           if (!error)

               // Get URL of generated HTML file
               String resultFileUrl = json.get("url").getAsString();
               // Download HTML file
               downloadFile(webClient, resultFileUrl, DestinationFile.toFile());
               System.out.printf("Generated HTML file saved as \"%s\" file.", DestinationFile.toString());

               // Display service reported error
           // Display request error
           System.out.println(response.code() + " " + response.message());
   public static void downloadFile(OkHttpClient webClient, String url, File destinationFile) throws IOException
       // Prepare request
       Request request = new Request.Builder()
       // Execute request
       Response response = webClient.newCall(request).execute();
       byte[] fileBytes = response.body().bytes();
       // Save downloaded bytes to file
       OutputStream output = new FileOutputStream(destinationFile);

Source PDF File

Following is the source PDF file.

Source PDF File

Output File

The following is the generated output file in HTML:

<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
.page { background-color:white; position:relative; z-index:0; }
.vector { position:absolute; z-index:1; }
.image { position:absolute; z-index:2; }
.text { position:absolute; z-index:3; opacity:inherit; white-space:nowrap; }
.annotation { position:absolute; z-index:5; }
.control { position:absolute; z-index:10; }
.annotation2 { position:absolute; z-index:7; }
.dummyimg { vertical-align: top; border: none; }
<body style="background-color:#999999;color:#000000;">
<div id="canvas" align="center">
<!-- page 1 begin -->
<div class="page" style="width:1024.0px;height:1448.2px;">
<span style="color:#538DD3;font-size:41px;font-family:'Arial';font-weight:bold;">
<span class="text" style="left:61.9px;top:59.2px;">Your Company Name&nbsp;</span>
<span style="font-size:19px;font-family:'Arial';">
<span class="text" style="left:61.9px;top:132.3px;">Your Address&nbsp;</span>
<span class="text" style="left:61.9px;top:157.3px;">City, State Zip&nbsp;</span>
<span style="font-size:19px;font-family:'Arial';font-weight:bold;">
<span class="text" style="left:793.0px;top:199.4px;">Invoice No. 123456&nbsp;</span>
<span class="text" style="left:750.9px;top:224.4px;">Invoice Date 01/01/2016&nbsp;</span>
<span class="text" style="left:61.9px;top:266.5px;">Client Name</span>
<span style="font-size:19px;font-family:'Arial';">
<span class="text" style="left:61.9px;top:291.9px;">Address&nbsp;</span>
<span class="text" style="left:61.9px;top:316.9px;">City, State Zip&nbsp;</span>
<span class="text" style="left:61.9px;top:401.3px;">Notes&nbsp;</span>
<span style="font-size:19px;font-family:'Arial';font-weight:bold;">
<span class="text" style="left:61.9px;top:544.0px;">Item&nbsp;</span>
<span class="text" style="left:425.9px;top:544.0px;">Quantity&nbsp;</span>
<span class="text" style="left:686.2px;top:544.0px;">Price&nbsp;</span>
<span class="text" style="left:917.0px;top:544.0px;">Total&nbsp;</span>
<span style="font-size:19px;font-family:'Arial';">
<span class="text" style="left:61.9px;top:587.1px;">Item 1&nbsp;</span>
<span class="text" style="left:492.2px;top:587.1px;">1&nbsp;</span>
<span class="text" style="left:685.2px;top:587.1px;">40.00&nbsp;</span>
<span class="text" style="left:915.0px;top:587.1px;">40.00&nbsp;</span>
<span class="text" style="left:61.9px;top:623.4px;">Item 2&nbsp;</span>
<span class="text" style="left:492.2px;top:623.4px;">2&nbsp;</span>
<span class="text" style="left:685.2px;top:623.4px;">30.00&nbsp;</span>
<span class="text" style="left:915.0px;top:623.4px;">60.00&nbsp;</span>
<span class="text" style="left:61.9px;top:659.8px;">Item 3&nbsp;</span>
<span class="text" style="left:492.2px;top:659.8px;">3&nbsp;</span>
<span class="text" style="left:685.2px;top:659.8px;">20.00&nbsp;</span>
<span class="text" style="left:915.0px;top:659.8px;">60.00&nbsp;</span>
<span class="text" style="left:61.9px;top:696.5px;">Item 4&nbsp;</span>
<span class="text" style="left:492.2px;top:696.5px;">4&nbsp;</span>
<span class="text" style="left:685.2px;top:696.5px;">10.00&nbsp;</span>
<span class="text" style="left:915.0px;top:696.5px;">40.00&nbsp;</span>
<span style="font-size:19px;font-family:'Arial';font-weight:bold;">
<span class="text" style="left:669.3px;top:732.5px;">TOTAL&nbsp;</span>
<span class="text" style="left:904.5px;top:732.5px;">200.00&nbsp;</span>
<div class="vector" style="left:52.0px;top:472.0px;"><img width="921" height="292" src="data:image/webp;base64,UklGRggFAABXRUJQVlA4WAoAAAAQAAAAmAMAIwEAQUxQSG0BAAABgFvbtmrldEIptwQ01wJcwqcRg4zsS4S7O5w/xo/hhYTaiMfs7ysimLhtJMhBt3A+MHPU/hZ9tg17sfZl+A//4T/8h//wH/7Df/gP/+E//If//n1Ply6xQ5dYtewSW1lziT2bf2j8g7KPT4nVaxqne1RonG6B//Af/sN/+A//4T/8h//wXwextXqhsMd6IbHz00JiazeFxG7M+0xiLyax0cgkFgWTmOM//If/8B/+w3/4D/81GdZOXGI1l9jmlkustOMSO29jfuA//If/8B/+6wKD/wz/V8wft15XzG93+qyYx3/4D//hP/yHfPCf4f9RFv1BYndBYjN5kFg8FiS2beU4kthpJLHlVZHTzSOJbbUxn/Ef/sN/+A//dYHBf4b/tx+cuMRqLrHNLZdYaccldi7zKIv7ILHZpvIoi5025jP+w3/4D//hP/yH/7rAnI8Eid0Fic00lSvmr60cRxJbiiQ2PRVJbHIuktiWAQBWUDggdAMAAPBQAJ0BKpkDJAE+kUigSyWlIyGlf4gAsBIJaW7hdgEb7AAvzExAfNfDbqt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVioLSjQwpJkyZMmTJkyZMmTJkyZMmTJkyZMmTJjK5Ju+2QIECBAgQIECBAgNTB148ePHjx48ePHjx3L0rM2oolaq+7Pw3JFt/dn4fSw0oyG2DytVfdn4bv76Y2weVqr7q+ozDqxb3YhbyuxDqxb3L+MiIdWLe7DoZmHVi3uxC3EbyfDlGjRo0aNGjRo0aNDnzxmLFixYsWLFixYsWKbrQrQezp31evXr169evXr169KeQhIDBgwYMGDBgwYMF/FqZbK48ePHjx48ePHjx47r2Hjx48ePHjx48eO69h48ePHjx48ePHjx3YePHjx48ePHjx48euo26re7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb3Yh1Yt7sQ6sW92IdWLe7EOrFvdiHVi3uxDqxb0AAP7/mrcgJaAAABJ6uzy6HcPZge729fu46heCIutB71+xRwF+VOTgDD60/+14Nf9n+0fNvbwXe7iqb06zd35XKb7HvO3lDGCEu0WMprLepaKc5pwvDjg0e5i3mZCW7WzIaRUbcikHZRltynM01kA3GQR1CVZGj6MXKceu0XzDjcAACO6OPPiPnw5eYam8cmh5RL6cwQoRXJAhILwmvVx77PY1oZqBHhHwSEj1jc0jTXPJa56wwt8tO/2x4grbKkCGKJi4SiYR81ZKJz/4fAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"/></div>
<!-- page 1 end -->

Extract PDF to HTML in Java – Demo

Below is a demonstration of the working code.

Extract PDF to HTML in Java - Demo

Step-by-Step Guide to Convert PDF to HTML

Following is the step-by-step guide to explain to users the working of the above-mentioned source code for the tutorial:

  1. The code imports the required packages and libraries to make the API request and reads the file from the URL.
  2. It then declares and initializes the API_Key, which the users can get by signing up or logging into the account. The users require this API key to make requests to API endpoints. The users can not send a direct request, so the API key acts as an access token, and the users have to provide it in the header for authentication.
  3. After this, the user has to provide API’s body payload, which in this sample code is the destination and source file URL, file password, and file pages to convert the required data, column layout, and HTML. The users can provide their own required information here and customize the code. In this scenario, the code utilizes the sample code containing the source file.
  4. The sample code assembles variables for JSON payload and sends the API POST request.
  5. The output response will provide the error, status, file name, and others. The status 200 determines that the request was successful, and the code provides the URL for the resulting file. The successful request returns the HTML code that the file stream reads and stores on the local storage as the result.HTML file, i.e., the destination file.