Jacob Collins Jacob is the JPedal Product Manager and specialises in PDF creation and manipulation. He also develops Salesforce backend systems and contributes to marketing and support. Outside of work, he’s a 1900‑rated chess player, guitarist, and French learner.

Java PDF to WebP conversion (Tutorial)

8 min read

pdf to webp

pdf to webp

TL;DR:

Java cannot read PDF files on its own. With JPedal you open the PDF, render each page to a BufferedImage, then write it out with JDeli.write(). WebP is usually the best choice for web delivery because it does both lossy and lossless compression and supports transparency, so it generally beats PNG and JPEG on size at the same visual quality.

Why convert PDF files to WebP images?

A PDF is a vector image (so it is rendered when you display it at whatever size you specify). A WebP is a bit mapped format invented by Google. So to convert a PDF to a WebP file we need to create a blank image and then draw the PDF onto this.

Our Java PDF library includes added functionality. There are lots of Open Source and commercial tools in most major languages. If you are using Java, it is not a function that is built into Java – you will need an external application to do this such as our JPedal PDF library. JPedal is the best Java PDF library for developers.

Add JPedal to your project

JPedal is published to the IDRsolutions Maven repository. Add the repository and the dependency to your build, or download the trial jar and put it on your class or module path.

Maven

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

<dependency>
    <groupId>com.idrsolutions</groupId>
    <artifactId>jpedal-trial</artifactId>
    <version>YYYY.MM</version>
</dependency>

Gradle

repositories {
    maven { url 'https://maven.idrsolutions.com' }
}

dependencies {
    implementation 'com.idrsolutions:jpedal-trial:YYYY.MM'
}

The repository needs your trial token or customer credentials in ~/.m2/settings.xml — see the Maven setup guide. Paid releases use the jpedal artifact in place of jpedal-trial.

How to convert PDF to WebP in Java

  1. Download a trial copy of JPedal and add it to your IDE.
  2. Create a File handle, InputStream or URL pointing to the PDF file
  3. Include a password if file password protected
  4. Open the PDF file
  5. Iterate over the pages

and the Java code to convert PDF to WebP…

Controlling the resolution and DPI of the output

This is the setting that matters most, and the one that is easiest to get wrong. Output resolution is controlled with setPageScaling(). The PDF to WebP documentation gives three reference points:

convert.setPageScaling(1.0f);  // 72 DPI - the default
convert.setPageScaling(1.33f); // which gives same size as Acrobat at 100% (approx 96 DPI)
convert.setPageScaling(2.0f);  // 144 DPI

Scaling and resolution are directly proportional, because a PDF page is defined in points at 72 points per inch. For any other target, the scaling factor is targetDpi / 72:

final int targetDpi = 300;   // 150 DPI -> 2.08f, 300 DPI -> 4.17f, 600 DPI -> 8.33f
convert.setPageScaling(targetDpi / 72f);

150 DPI is a reasonable default for screen and web output, and 300 DPI is the usual minimum for print.

If you need a fixed output size instead — for thumbnails, for example — use setFitToSize(). JPedal preserves the aspect ratio and fits the page within the given dimensions:

convert.setFitToSize(new int[] {300,400});

You can also restrict the conversion to particular pages rather than rendering the whole document:

convert.setPageRange(new PageRanges("1-5,8:10,15"));

Watch the memory cost. Scaling applies in both directions, so doubling the DPI quadruples the pixel count. An A4 page is about 595 × 842 pixels at 72 DPI, but roughly 2480 × 3508 at 300 DPI — around 35 MB as an uncompressed 32-bit BufferedImage. Render and write one page at a time rather than collecting a whole document in a list.

WebP can compress either way, and the choice matters more than it does for PNG or JPEG. Lossless keeps text crisp; lossy is much smaller for scanned or photographic pages. Quality defaults to 75, and 100 means no compression:

final WebpEncoderOptions options = new WebpEncoderOptions();
options.setQuality(90);
options.setCompressionFormat(WebpCompressionFormat.LOSSLESS);
JDeli.write(bi, options, out);

For page thumbnails on a website, pair a lower DPI with WebpCompressionFormat.LOSSY; for pages that will be zoomed or read, use LOSSLESS and let the resolution do the work.

Which image format should you use?

The conversion code is the same for every output format — only the OutputFormat constant and the file extension change. The trade-offs are not the same, so it is worth checking that WebP is the right choice:

Format Compression Transparency Multi-page Best for
PNG Lossless Yes No Text-heavy pages, thumbnails, screenshots, anything that will be zoomed
JPEG Lossy No No Photographic or scanned pages where file size matters more than sharp text
TIFF Lossless (LZW, Deflate) or JPEG Yes Yes Archival and document workflows, and whole documents in a single file
GIF Lossless, 256 colours only 1-bit only Yes (animation) Legacy pipelines only — the colour limit makes it a poor fit for PDF pages
BMP None (uncompressed) No No Windows interop and legacy tooling that cannot read anything else
WebP Lossy or lossless Yes No Web delivery — usually smaller than both PNG and JPEG
HEIC Lossy or lossless Yes Yes Apple ecosystems and the smallest lossy files

Key points to remember

The usual reason for doing this conversion is to display the content as an image (e.g. a thumbnail on a website). However, there are things so remember:

1. Bitmapped images do not scale very well (unlike Vector formats like PDF). So you need to get the size correct. If you make it too small you will not be able to zoom in without pixelation. If you make it too big you will make the download slower and need more memory.

2. Bitmapped images do not have some of the other advantages of PDF files (like text search).  You may need to add manual functionality to your application if you wish to have things like text highlighting.

3. If you are doing it to print the PDF you will need a very large image to get a quality print at 300 or 600 dpi (what looks good on screen at 72dpi will not appear as crisp on a printout).

So long as you remember these, Java PDF to WebP conversion is a straightforward process with a tool such as JPedal.

 

Frequently asked questions

Should I use lossy or lossless WebP for PDF pages?

Use lossless for pages that are mostly text, because lossy compression blurs the edges of glyphs. Use lossy for scanned or photographic pages, where it is dramatically smaller. Set it with WebpEncoderOptions.setCompressionFormat().

Is WebP smaller than PNG or JPEG?

Usually, yes, at the same visual quality — which is why it is the sensible default for serving PDF page images on the web. It also supports transparency, which JPEG does not. The trade-off is that some older desktop tooling still cannot open WebP files.

What DPI should I use when converting a PDF to WebP?

150 DPI is a good default for screen and web use, and 300 DPI is the usual minimum for print. Set it with convert.setPageScaling(targetDpi / 72f), because PDF pages are defined at 72 points per inch. Remember that the pixel count grows with the square of the scaling factor.

Can I convert only some of the pages?

Yes. Call convert.setPageRange(new PageRanges("1-5,8:10,15")) before you open the file. Without it, every page in the document is rendered.

How do I convert a password-protected PDF?

Call convert.setPassword("password") before openPDFFile(). The same applies to both the owner and the user password.

Can Java convert a PDF to an image without a third-party library?

No. The JDK has no PDF support at all — ImageIO can write WebP files, but nothing in the standard library can parse or render a PDF page. You need a PDF library such as JPedal, Apache PDFBox or a Ghostscript wrapper to produce the BufferedImage in the first place.

Other useful PDF to WebP resources

Converting to a different format, or not sure which one you need? Our Java PDF to Image conversion guide covers every output format JPedal supports, and we bring over two decades of hands-on experience to helping you work with PDF files in Java.



The JPedal PDF library allows you to solve these problems in Java


Jacob Collins Jacob is the JPedal Product Manager and specialises in PDF creation and manipulation. He also develops Salesforce backend systems and contributes to marketing and support. Outside of work, he’s a 1900‑rated chess player, guitarist, and French learner.