Leon Atherton Leon has over 15 years’ Java experience and leads BuildVu, also contributing to cloud services and internal tooling. Wearing many hats across technical and growth roles as a core member, he enjoys motorsport, strategy games, and software side projects.

PDF to HTML in Java: Step-by-step Tutorial

8 min read

PDF to HTML using Java (Icon)
Converting PDF to HTML in Java means parsing the binary PDF format and producing browser-renderable HTML5, CSS, and font files. This tutorial covers a working example using BuildVu, plus the configuration options, output structure, and edge cases most developers run into.

What you’ll build

A Java program that takes a PDF file path and an output directory, then writes HTML5 with embedded fonts and images. The output renders in any modern browser without plugins or a PDF reader.

Requirements

The example uses BuildVu, a commercial PDF to HTML library from IDRsolutions. A 30-day trial JAR is available with no signup required for evaluation.
  • BuildVu (the example was tested on 2026.05)
  • Java 17 or later
  • Maven or Gradle, or the raw JAR on the classpath

Add BuildVu to your project

Maven

Add the IDRsolutions repository and the BuildVu dependency to your pom.xml:


<repositories>
    <repository>
        <id>IDRsolutions</id>
        <name>IDR Solutions</name>
        <url>https://maven.idrsolutions.com</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.idrsolutions</groupId>
        <artifactId>{artifactId}</artifactId>
        <version>{version}</version>
    </dependency>
</dependencies>

Replace {artifactId} and {version} with the trial or licensed artifact you need and the current release. You’ll also need to add your credentials — see the Maven dependency guide for full setup, including the manual JAR install option.

Gradle

Add the BuildVu JAR as a local file dependency in your build.gradle:


dependencies {
    implementation files("lib/buildvu.jar")
}

Using the Kotlin DSL (the default for new Gradle projects)? Use implementation(files("lib/buildvu.jar")) instead.

Or use the IDRsolutions remote Maven repository:


dependencies {
    implementation "com.idrsolutions:{artifactId}:{version}"
}

See the Gradle dependency guide for repository and credentials setup, including the recommended gradle.properties approach, and the Kotlin DSL equivalents for the remote-repository snippets.

No build tool

If you’re not using a build tool, add the JAR to your classpath manually.

Minimal working example

This is the smallest amount of code that produces a working conversion: That’s the full working program. Drop an input.pdf next to it, run it, and the output/ directory will contain the converted HTML.

Choose an output mode

The fourth argument to PDFtoHTML5Converter decides what kind of output you get. Swap the class and the rest of the program stays the same:

ContentOptions — raw fragments

What the example above uses. Each page is a standalone HTML fragment with no viewer wrapper, meant to be embedded in your own page or served as a static asset.

new PDFtoHTML5Converter(pdfFile, outputDir, conversionOptions, new ContentOptions());

IDRViewerOptions — the bundled viewer

BuildVu’s default. Wraps the pages in the IDRViewer, a JavaScript viewer with page navigation, zoom, search, and thumbnails. Use this when you want a ready-to-open document viewer rather than raw content to drop into your own UI.

new PDFtoHTML5Converter(pdfFile, outputDir, conversionOptions, new IDRViewerOptions());

SinglefileOptions — one self-contained file

Produces a single HTML file with the CSS, fonts, and images inlined. It’s optimised, not a naive merge — CSS is shared across pages with duplicates removed — so the result stays lean, easy to move around, and simple to parse. Not recommended for large or complex documents, where a single inlined file gets heavy.

new PDFtoHTML5Converter(pdfFile, outputDir, conversionOptions, new SinglefileOptions());

See the view mode documentation for the details of each mode.

Useful content-mode settings

Most of BuildVu’s defaults are already sensible, so there is little to change for a first conversion. When you are embedding fragments, a few ContentOptions settings do earn their place:

Emit standalone documents instead of fragments

Wraps each page in html, head, and body tags so it opens on its own, rather than as a fragment you splice into an existing page.

contentOptions.setCompleteDocument(true);

Avoid CSS collisions between documents

Appends a suffix to every generated CSS class and id, so multiple conversions shown in the same page don’t clash with each other’s styles. Give each document a different suffix.

contentOptions.setCssSuffix("doc1");

Generate a search index

Writes a search.json alongside the pages so you can build text search over the converted document.

contentOptions.setGenerateSearchFile(true);
These are just a few of the available settings. The full list, covering every mode, is in the BuildVu settings documentation.

Common issues

Encrypted PDFs

If the PDF has a password, set it on the converter before calling convert():

converter.setPassword("yourPassword");
converter.convert();
For documents without a known password, conversion will throw a PdfException.

Memory usage

BuildVu reads the PDF from disk rather than loading it all into memory, so the heap you need depends on content, not file size: large pages, high-resolution images, and complex shadings are the main drivers. 1GB handles most documents; raise it for content-heavy ones:

java -Xmx1g -jar yourapp.jar
If you’re converting heavy documents repeatedly in a server, consider running BuildVu as a microservice rather than embedding it directly. The BuildVu microservice runs as a Docker container with a REST API.

Output directory must be writable

The output directory has to exist or be creatable by the running process. On Linux servers, the most common cause of a silent failure is a permission issue on the parent directory.

Scanned PDFs have no text to convert

BuildVu converts what’s actually in the PDF. A scanned document is just page images with no text layer, so the output is HTML wrapping those images — visually accurate, but with no selectable or searchable text. Recovering text requires OCR, which BuildVu doesn’t do; run the PDF through an OCR tool to add a text layer first, then convert.

How BuildVu compares to other Java options

The PDF to HTML space in Java has fewer options than developers usually expect, because most libraries marketed as “HTML conversion” actually go the other direction (HTML to PDF). The honest comparison:
Library License Notes
Apryse PDFTron Commercial Native library with Java bindings, not pure Java.
Aspose.PDF for Java Commercial Pure Java. Conversion is one feature in a broader PDF manipulation suite.
BuildVu Commercial Pure Java, focused specifically on PDF to HTML5 and SVG. Runs in-process or as a microservice.
Spire.PDF for Java Commercial (free tier with page limits) Pure Java. Free version restricts the number of pages converted per document.
All four are commercial. There isn’t a mature open-source Java library that converts PDF to HTML with reasonable fidelity that could be used in a commercial context.

Open source PDF to HTML options in Java

Open source options exist but come with practical problems. pdf2htmlEX, the most-cited example, is a C++ tool rather than a Java library, so using it from a JVM application means shelling out to a native binary and parsing its output. The project has been intermittently maintained since the original author stepped away in 2017, depends on a patched build of Poppler that complicates the build process, and produces single-file HTML output with base64-embedded assets that’s awkward to manipulate downstream. The license is GPLv3, which rules it out for most commercial Java products without legal review, and there’s no commercial support to fall back on when a customer PDF breaks the conversion. The pure Java characteristic matters because you’re deploying to environments where native dependencies are awkward (containerized JVMs, some PaaS providers, Jakarta EE servers).

Frequently Asked Questions

How do I convert PDF to HTML in Java without using a server?

Use an in-process Java library like BuildVu that runs inside your JVM. You add it as a dependency and call the converter from your application code — the conversion happens in the same process, with no network calls, no separate service, and no data leaving your environment.

Does the converted HTML rely on any external resources?

No. Everything the page needs is local: in content and IDRViewer modes the CSS, fonts, and images sit alongside the HTML and are referenced with relative paths, and single-file mode inlines them into the page itself. There are no CDN links or remote calls, so the output renders in air-gapped or offline environments.

How well does the original PDF layout survive conversion to HTML?

Good Java libraries preserve layout using absolute CSS positioning, keeping text, images, and vector graphics at sub-pixel coordinates that match the source. Fonts are extracted from the PDF and embedded with @font-face declarations so typography matches even on systems without the original font installed.

What happens to interactive PDF elements like forms, annotations, and hyperlinks?

BuildVu preserves interactive annotations such as hyperlinks, rich media, and markup annotations with popups. Form fields are the exception: they are rendered as static content rather than interactive HTML inputs. For fillable forms in the browser, FormVu converts PDF forms to interactive HTML in Java.

BuildVu allows you to

View PDF files in a Web app
Convert PDF documents to HTML5
Parse PDF documents as HTML
Leon Atherton Leon has over 15 years’ Java experience and leads BuildVu, also contributing to cloud services and internal tooling. Wearing many hats across technical and growth roles as a core member, he enjoys motorsport, strategy games, and software side projects.