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 = Html2PdfBatchUtils.renderHtmlString(
"<html><body>Hello World</body></html>",
"https://example.com/",
Path.of("out.pdf"),
Html2PdfBatchUtils.CSS_A4_20MM,
Html2PdfBatchUtils.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(Html2PdfBatchUtils.CSS_LETTER_HALF_IN),
Optional.empty()),
new HtmlFileJob(Path.of("file2.html"), Path.of("."), Path.of("file2.pdf"),
Optional.empty(),
Optional.of(Html2PdfBatchUtils.registerFontDir(Path.of("fonts"))))
);
BatchResult<Path> result = Html2PdfBatchUtils.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.
- 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 raw HTML string to PDF.static final recordRender a URL to PDF. -
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<Path> batchHtmlStrings(List<HtmlToPdfBatchUtils.HtmlStringJob> jobs, Consumer<Path> onSuccess, Consumer<Throwable> onFailure) static org.openpdf.text.utils.PdfBatch.BatchResult<Path> batchUrls(List<HtmlToPdfBatchUtils.UrlJob> jobs, Consumer<Path> 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 PathrenderHtmlFile(Path htmlFile, Path baseDir, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML file (and relatives).static PathrenderHtmlString(String html, String baseUri, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) Render an HTML string.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
-
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
-
renderUrl
public static Path renderUrl(String url, Path output, String injectCss, Consumer<ITextRenderer> rendererCustomizer) throws IOException Render a URL to PDF.- Throws:
IOException
-
batchHtmlStrings
-
batchHtmlFiles
-
batchUrls
-
injectCssBlock
Wraps an HTML string with a
-