Skip to content

feat(api): render a DocumentSession directly to images#227

Merged
DemchaAV merged 2 commits into
developfrom
feat/render-to-image
Jun 22, 2026
Merged

feat(api): render a DocumentSession directly to images#227
DemchaAV merged 2 commits into
developfrom
feat/render-to-image

Conversation

@DemchaAV

Copy link
Copy Markdown
Owner

Why

Getting a raster image from a document meant calling toPdfBytes() and then reloading those bytes with Loader.loadPDF to hand a PDDocument to PDFBox's PDFRenderer — a serialize-then-reparse round-trip (and a second full document in memory). PDFBox can rasterize a PDDocument directly, so previews, thumbnails, and pixel diffs need no PDF byte detour.

What

  • DocumentSession.toImages(int dpi)List<BufferedImage> (one per page) and toImage(int pageIndex, int dpi)BufferedImage, plus transparent overloads returning ARGB instead of opaque white. The return type is the JDK java.awt.image.BufferedImage, so the public surface stays PDFBox-free.
  • The PDF backend's document build is extracted into buildDocument(...), shared by the existing save path and a new renderToImages(...); the PDFRenderer / ImageType calls stay in backend.fixed.pdf.
  • PdfVisualRegression gains direct renderPages(session) / assertMatchesBaseline(name, session) overloads; EmojiSvgVsPngExample rasterises through toImage(0, 96, true) instead of the round-trip.
  • dpi <= 0, an out-of-range page index, and an empty document are rejected; rendering failures surface as DocumentRenderingException.

Tests

./mvnw test -pl . green (1494 tests). New DocumentSessionImageTest (10): one image per page sized to the page at the DPI; dimensions scale with DPI; rendered pages contain painted pixels; transparent yields an ARGB image with a fully-transparent margin while the default is opaque RGB; toImage returns the requested page and is pixel-identical to the matching toImages entry; a watermark lands in the raster; and the direct render is pixel-identical to the toPdfBytes() round-trip (ImageDiff, budget 0). Examples compile and EmojiSvgVsPngExample regenerates.

Getting a raster from a document meant calling toPdfBytes() and then
reloading those bytes with Loader.loadPDF to hand a PDDocument to
PDFRenderer — a serialize-then-reparse round-trip. Add
DocumentSession.toImages(int dpi) and toImage(int pageIndex, int dpi),
plus transparent ARGB overloads, returning java.awt.image.BufferedImage
and rasterizing the in-memory document directly.

The PDF backend's document build is extracted into buildDocument(...),
shared by the existing save path and a new renderToImages(...); the
PDFRenderer/ImageType calls stay in the backend, so the public surface
stays PDFBox-free (BufferedImage is JDK). PdfVisualRegression gains
direct renderPages(session) / assertMatchesBaseline(name, session)
overloads, and EmojiSvgVsPngExample rasterises through toImage instead of
the round-trip.
@DemchaAV DemchaAV force-pushed the feat/render-to-image branch from 282a78f to 348be3b Compare June 22, 2026 21:55
@DemchaAV DemchaAV merged commit 23e8007 into develop Jun 22, 2026
11 checks passed
@DemchaAV DemchaAV deleted the feat/render-to-image branch June 22, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants