#713: BatikImageRenderer.java

projectforge-business/src/main/java/org/projectforge/framework/renderer/BatikImageRenderer.java Renderer / SVG-to-Raster-PDF Conversion, projectforge-business/src/main/java/org/projectforge/framework/renderer/BatikImageRenderer.java 114 lines · 69 code · 37 comments · 8 blank
Converts SVG DOM documents to raster images (PNG, JPEG), PDF, or raw SVG byte arrays using Apache Batik and Apache FOP transcoders. Acts as a stateless utility for multi-format SVG rendering.

Architecture & Design

BatikImageRenderer is a stateless utility class exposing a single public entry point getByteArray(Document, int, ImageFormat). Internally, it delegates to one of three private methods depending on the requested ImageFormat. The class uses Apache Batik's transcoder API (PNGTranscoder, JPEGTranscoder) for raster output and Apache FOP's PDFTranscoder for vector PDF output. SVG bytes are obtained by serializing the DOM via XmlHelper.toString() without any transcoding step.

Public API

MethodDescription
getByteArray(Document, int width, ImageFormat)Main entry point. Routes to format-specific renderers based on ImageFormat. Throws UnsupportedOperationException for unknown formats.

Private Rendering Methods

MethodFormatDetails
getRasterImageByteArray(Document, int, ImageFormat)PNG / JPEGCreates an ImageTranscoder instance (PNG or JPEG). JPEG uses JPEGTranscoder.KEY_QUALITY hint set to 0.8. Both apply KEY_WIDTH for output width. TIFF support is commented out.
getPDFByteArray(Document, int)PDFUses PDFTranscoder with KEY_AUTO_FONTS disabled and KEY_WIDTH set. Outputs to ByteArrayOutputStream via TranscoderOutput.
getSVGByteArray(Document, int)SVGSerializes the DOM to an XML string using XmlHelper.toString(document, true), then returns UTF-8 bytes. The width parameter is ignored.

Error Handling

All transcoder invocations are wrapped in try-catch blocks catching TranscoderException. Errors are logged via SLF4J at error level, but the exception is not re-thrown — the partial ByteArrayOutputStream content is returned. This means callers receive potentially incomplete or empty data on failure without any indication.

Unsupported Format Guard

The dispatch logic uses ImageFormat.isIn(ImageFormat.PNG, ImageFormat.JPEG) for the raster branch, then equality checks against ImageFormat.PDF and ImageFormat.SVG. Any unrecognized format triggers an UnsupportedOperationException. Note the redundant identity check pattern: imageFormat == ImageFormat.PDF == true, which is semantically identical to imageFormat == ImageFormat.PDF.

Dependencies

Git History

868d6abb7 2026-01-01 2025 -> 2026
63081666f 2025-01-01 Source file headers: 2024-> 2025.
b6092df09 2024-01-09 Copyright 2023 -> 2024
ab45d51fa 2023-01-01 Copyright 2001-2022 -> 2001-2023.
5f7ef41b8 2022-02-18 Copyright 2021 -> 2022
cd27dd997 2021-04-11 package xstream -> xmlstream. (should be replaced by xstream later).
ceb63e8a1 2021-03-08 Source code header: (C) 2001-2021.
7c79f1922 2020-01-03 Copyright of source header -> 2020.
73a9755df 2019-10-11 More code cleanup: - Collapsed catch blocks that did the exact same things - Replaced ArrayList<Class> with ArrayList<> - Replaced StringBuffer with StringBuilder - Removed a few usages of deprecated classes (mainly HSSFColors in Excel) - Replace Collections.sort with List.sort
32f634b88 2019-10-09 Optimize imports
000ca723d 2019-10-07 Remove pointless boolean expressions (business)
dd5ca38ac 2019-06-07 CopyRight of all java file-header updated or created.
a5bbdca6a 2017-12-04 Change logger to slf4j
9ebb88522 2016-07-18 Initial commit