feat(api): render a DocumentSession directly to images#227
Merged
Conversation
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.
282a78f to
348be3b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Getting a raster image from a document meant calling
toPdfBytes()and then reloading those bytes withLoader.loadPDFto hand aPDDocumentto PDFBox'sPDFRenderer— a serialize-then-reparse round-trip (and a second full document in memory). PDFBox can rasterize aPDDocumentdirectly, so previews, thumbnails, and pixel diffs need no PDF byte detour.What
DocumentSession.toImages(int dpi)→List<BufferedImage>(one per page) andtoImage(int pageIndex, int dpi)→BufferedImage, plustransparentoverloads returning ARGB instead of opaque white. The return type is the JDKjava.awt.image.BufferedImage, so the public surface stays PDFBox-free.buildDocument(...), shared by the existing save path and a newrenderToImages(...); thePDFRenderer/ImageTypecalls stay inbackend.fixed.pdf.PdfVisualRegressiongains directrenderPages(session)/assertMatchesBaseline(name, session)overloads;EmojiSvgVsPngExamplerasterises throughtoImage(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 asDocumentRenderingException.Tests
./mvnw test -pl .green (1494 tests). NewDocumentSessionImageTest(10): one image per page sized to the page at the DPI; dimensions scale with DPI; rendered pages contain painted pixels;transparentyields an ARGB image with a fully-transparent margin while the default is opaque RGB;toImagereturns the requested page and is pixel-identical to the matchingtoImagesentry; a watermark lands in the raster; and the direct render is pixel-identical to thetoPdfBytes()round-trip (ImageDiff, budget 0). Examples compile andEmojiSvgVsPngExampleregenerates.