java.lang.Object
org.openpdf.html.HtmlToPdfBatchUtils
High-performance batch HTML-to-PDF conversion utilities for OpenPDF HTML (Flying Saucer),
powered by Java 21 virtual threads for scalable concurrent execution.
This class provides simple, high-level APIs for converting HTML sources to PDF files using
ITextRenderer, with support for:
- Raw HTML strings (with optional base URI for resolving relative resources)
- Local HTML files and their relative assets (CSS, images, fonts, etc.)
- Remote HTML content from HTTP/HTTPS URLs
- Optional injection of custom CSS before rendering
- Renderer customization hooks for advanced control (e.g., DPI, fonts, user agent)
Key Features
- Runs each rendering task on a virtual thread for massive concurrency without thread exhaustion.
- Provides convenient
batch*methods to process large collections of jobs in parallel. - Supports flexible output path handling (directories created automatically).
- Encapsulates results in a
PdfBatch.BatchResultcontaining separate lists of successes and failures.
Usage Examples
Convert a single HTML string to PDF
Path pdf = HtmlToPdfBatchUtils.renderHtmlString(
"<html><body>Hello World</body></html>",
"https://example.com/",
Path.of("out.pdf"),
HtmlToPdfBatchUtils.CSS_A4_20MM,
HtmlToPdfBatchUtils.setDpi(150)
);
Batch convert multiple HTML files
List<HtmlFileJob> jobs = List.of(
new HtmlFileJob(Path.of("file1.html"), Path.of("."), Path.of("file1.pdf"),
Optional.of(HtmlToPdfBatchUtils.CSS_LETTER_HALF_IN),
Optional.empty()),
new HtmlFileJob(Path.of("file2.html"), Path.of("."), Path.of("file2.pdf"),
Optional.empty(),
Optional.of(HtmlToPdfBatchUtils.registerFontDir(Path.of("fonts"))))
);
BatchResult<Path> result = HtmlToPdfBatchUtils.batchHtmlFiles(jobs,
path -> System.out.println("Created PDF: " + path),
error -> error.printStackTrace()
);
Notes invalid input: '&' Best Practices
- Set page size and margins via CSS
@pagerules in your HTML or via theinjectCssparameter. - Always provide a
baseUri(orbaseDir) if your HTML references relative resources. - Use
rendererCustomizerto adjust advanced rendering settings like DPI or font loading. - Batch methods allow optional success and failure callbacks for real-time feedback during processing.
- When using OutputStream-based batch jobs, every job must use a distinct
OutputStreaminstance. Sharing a stream between concurrent jobs will corrupt the output. The caller is responsible for closing streams after the batch completes.
- Since:
- 3.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRender an HTML file (and its relative assets) to PDF.static final recordRender an HTML file (and its relative assets) to a pre-openedOutputStream.static final recordRender raw HTML string to PDF.static final recordRender raw HTML string to a pre-openedOutputStream.static final recordRender a URL to PDF.static final recordRender a remote URL to a pre-openedOutputStream. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic org.openpdf.text.utils.PdfBatch.BatchResult<Path> batchHtmlFiles(List<HtmlToPdfBatchUtils.HtmlFileJob> jobs, Consumer<Path> onSuccess, Consumer<Throwable> onFailure) static org.openpdf.text.utils.PdfBatch.BatchResult<Void> batchHtmlFilesToStreams(List<HtmlToPdfBatchUtils.HtmlFileStreamJob> jobs, Consumer<Void> onSuccess, Consumer<Throwable> onFailure) static org.openpdf.text.utils.PdfBatch.BatchResult<Path> batchHtmlStrings(List<HtmlToPdfBatchUtils.HtmlStringJob> jobs, Consumer<Path> onSuccess, Consumer<Throwable> onFailure) static org.openpdf.text.utils.PdfBatch.BatchResult<Void> batchHtmlStringsToStreams(List<HtmlToPdfBatchUtils.HtmlStringStreamJob> jobs, Consumer<Void> onSuccess, Consumer<Throwable> onFailure) static org.openpdf.text.utils.PdfBatch.BatchResult<Path> batchUrls(List<HtmlToPdfBatchUtils.UrlJob> jobs, Consumer<Path> onSuccess, Consumer<Throwable> onFailure) static org.openpdf.text.utils.PdfBatch.BatchResult<Void> batchUrlsToStreams(List<HtmlToPdfBatchUtils.UrlStreamJob> jobs, Consumer<Void> onSuccess, Consumer<Throwable> onFailure) static UnaryOperator<String> injectCssBlock(String css) Wraps an HTML string with a block prepended to .static Consumer<ITextRenderer> registerFontDir(Path dir) Example customizer: register a font directory (TrueType/OpenType).static voidrenderHtmlFile(Path htmlFile, Path baseDir, OutputStream outputStream, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML file (and relatives).static PathrenderHtmlFile(Path htmlFile, Path baseDir, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML file (and relatives).static voidrenderHtmlString(String html, String baseUri, OutputStream outputStream, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML string.static PathrenderHtmlString(String html, String baseUri, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML string.static voidrenderUrl(String url, OutputStream outputStream, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render a URL to PDF.static PathRender a URL to PDF.static Consumer<ITextRenderer> setDpi(int dpi) Example customizer: set DPI for images/text.
-
Field Details
-
CSS_A4_20MM
Sample CSS for A4 portrait with 20mm margins.- See Also:
-
CSS_LETTER_HALF_IN
Sample CSS for US Letter portrait with 0.5in margins.- See Also:
-
-
Method Details
-
renderHtmlString
public static Path renderHtmlString(String html, String baseUri, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) throws IOException Render an HTML string.- Throws:
IOException
-
renderHtmlString
public static void renderHtmlString(String html, String baseUri, OutputStream outputStream, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML string. -
renderHtmlFile
public static Path renderHtmlFile(Path htmlFile, Path baseDir, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) throws IOException Render an HTML file (and relatives).- Throws:
IOException
-
renderHtmlFile
public static void renderHtmlFile(Path htmlFile, Path baseDir, OutputStream outputStream, String injectCss, Consumer<ITextRenderer> rendererCustomizer) throws IOException Render an HTML file (and relatives).- Throws:
IOException
-
renderUrl
public static Path renderUrl(String url, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) throws IOException Render a URL to PDF.- Throws:
IOException
-
renderUrl
public static void renderUrl(String url, OutputStream outputStream, String injectCss, Consumer<ITextRenderer> rendererCustomizer) throws IOException Render a URL to PDF.- Throws:
IOException
-
batchHtmlStrings
-
batchHtmlFiles
-
batchUrls
-
batchHtmlStringsToStreams
-
batchHtmlFilesToStreams
-
batchUrlsToStreams
-
injectCssBlock
Wraps an HTML string with a
-