Flying Saucer can convert html to PDF or Swing.
But FS's rendering pipeline is pluggable at its core.
Implement "OutputDevice", and you get a new render target - the full CSS layout engine stays the same. The PDF and Swing backends are just two OutputDevice implementations.
Set paper size and margins right in your CSS — no Java config:
@page {
size: A4 landscape;
margin: 2cm 1.5cm;
}
Flying Saucer honours @page rules fully. The template controls layout; the Java code just calls render(). #Java#PDF#CSS#FlyingSaucerPDF
Need a barcode or QR code inside a PDF?
Implement ReplacedElementFactory — map any HTML element to custom-drawn content. Flying Saucer calls your factory during layout and composites the result into the page.
Charts, signatures, stamps — all is possible. #Java#PDF
Flying Saucer supports "data:" URIs in HTML and CSS.
Like this:
<img src="data:image/png;base64,iVBOR..."/>
Embed images inline — no external files, no file-system access needed at render time. Great for self-contained report templates.
#Java#PDF
No PDF needed? Render XHTML to a BufferedImage instead:
BufferedImage img =
Graphics2DRenderer.renderToImage("<html>...</html>", 800);
Perfect to report thumbnails, email previews, or any pipeline that needs pixels rather than a PDF.
Want control access to resources?
Interface `UserAgentCallback` lets you intercept every resource Flying Saucer loads: images, stylesheets, XML (from classpath, DB, S3, CMS).
Or sandbox templates by blocking URLs they shouldn't reach.
One interface, total control.
What to create PDF from HTML5/CSS3 input?
Use new module "flying-saucer-chrome-pdf"!
1. Replace "org.xhtmlrenderer:flying-saucer-pdf" by "org.xhtmlrenderer:flying-saucer-chrome-pdf"
2. Generate PDF like a boss:
byte[] pdf =
Html2Pdf.fromHtml(<html>flex/grid/css3</html>");
Need to generate multi-page PDF?
Control exactly where your PDF pages break — right from CSS:
.section { page-break-before: always; }
.keep-together { page-break-inside: avoid; }
No Java code changes, no renderer configuration. Just CSS.
Need to use custom fonts in your PDF?
Use @font-face in CSS. @flyingsaucerpdf embeds the TTF/OTF directly:
@font-face {
font-family: 'Inter';
src: url('classpath:/fonts/Inter.ttf');
}
No system font installation required. Works in Docker too. #Java#PDF
SVG inside your XHTML?
Starting from June 2025, FlyingSaucer supports SVG images.
Embed inline <svg> or reference an external .svg file - FS will render it in PDF.
Vector graphics land in your PDFs at full resolution, sharp at any zoom level. #Java#PDF
Flying Saucer implements CSS 2.1.
What that means in practice?
✓ Box model (margin, padding, border)
✓ Floats & positioning
✓ Pseudo-classes (:first-child, :last-child…)
✓ Descendant, sibling & attribute selectors
What you style in your browser renders identically to PDF.
Did you know Flying Saucer ships a Swing component?
XHTMLPanel panel = new XHTMLPanel(); panel.setDocument("file:///report.xhtml");
Drop it in a JScrollPane and you get a live, CSS-styled document viewer - no browser needed.
#Java#Swing#FlyingSaucer
Generating a PDF from HTML doesn't have to be complicated.
The easiest way:
URL source = new URL("https://t.co/PhDXt9DmrZ");
byte[] pdf = Html2Pdf.fromUrl(source);
That's it. Flying Saucer handles CSS layout, custom fonts, pagination and the whole stack. #Java#PDF
Meet Flying Saucer, a pure-Java library that turns XHTML+CSS into PDFs, images, and Swing UIs.
Zero native dependencies.
Full CSS 2.1 layout engine.
In production since 2004.
One Java rendering tip per week. Follow along.
#Java#PDF#OpenSource