Convert Figma logo to code with AI

flyingsaucerproject logoflyingsaucer

XML/XHTML and CSS 2.1 renderer in pure Java

1,975
555
1,975
28

Top Related Projects

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!

Converts a pdf file into a text file while keeping the layout of the original pdf. Useful to extract the content from a table in a pdf file for instance. This is a subclass of PDFTextStripper class (from the Apache PDFBox library).

iText for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText can be a boon to nearly every workflow.

2,609

Mirror of Apache PDFBox

4,646

Fast, Nimble PDF Writer for Ruby

Convert HTML to PDF using Webkit (QtWebKit)

Quick Overview

Flying Saucer is an open-source, pure Java library for rendering XHTML with CSS 2.1 support. It's designed to generate PDF, images, and other formats from XML/XHTML sources, making it useful for creating printable documents from web content.

Pros

  • Pure Java implementation, ensuring cross-platform compatibility
  • Supports CSS 2.1, allowing for rich styling of documents
  • Can generate PDFs, images, and other formats from XHTML
  • Actively maintained with regular updates

Cons

  • Limited support for modern web technologies (e.g., HTML5, CSS3)
  • Performance can be slower compared to native rendering engines
  • Learning curve may be steep for developers new to XML/XHTML processing
  • Limited community support compared to more mainstream alternatives

Code Examples

  1. Creating a PDF from an XHTML file:
import org.xhtmlrenderer.pdf.ITextRenderer;

String inputFile = "input.xhtml";
String outputFile = "output.pdf";

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File(inputFile));
renderer.layout();

try (OutputStream os = new FileOutputStream(outputFile)) {
    renderer.createPDF(os);
}
  1. Rendering XHTML to an image:
import org.xhtmlrenderer.swing.Java2DRenderer;
import org.xhtmlrenderer.util.FSImageWriter;

String inputFile = "input.xhtml";
String outputFile = "output.png";

Java2DRenderer renderer = new Java2DRenderer(new File(inputFile), 800, 600);
BufferedImage image = renderer.getImage();

FSImageWriter imageWriter = new FSImageWriter();
imageWriter.write(image, outputFile);
  1. Applying custom CSS to XHTML:
import org.xhtmlrenderer.pdf.ITextRenderer;

String xhtml = "<html><body><h1>Hello, World!</h1></body></html>";
String css = "h1 { color: red; font-size: 24pt; }";

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(xhtml);
renderer.getSharedContext().getCss().parseStyleSheet(css);
renderer.layout();

try (OutputStream os = new FileOutputStream("output.pdf")) {
    renderer.createPDF(os);
}

Getting Started

To use Flying Saucer in your project, add the following dependency to your Maven pom.xml:

<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf-openpdf</artifactId>
    <version>9.1.22</version>
</dependency>

For Gradle, add this to your build.gradle:

implementation 'org.xhtmlrenderer:flying-saucer-pdf-openpdf:9.1.22'

After adding the dependency, you can start using Flying Saucer in your Java project to render XHTML to various formats.

Competitor Comparisons

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!

Pros of OpenHTMLtoPDF

  • More active development and maintenance
  • Better support for modern CSS features
  • Improved performance and rendering accuracy

Cons of OpenHTMLtoPDF

  • Potentially less stable due to ongoing changes
  • May require more frequent updates to keep up with new features
  • Smaller community and less widespread adoption

Code Comparison

OpenHTMLtoPDF:

PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withUri("https://example.com");
builder.toStream(outputStream);
builder.run();

Flying Saucer:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument("https://example.com");
renderer.layout();
renderer.createPDF(outputStream);

Both projects aim to convert HTML to PDF, but OpenHTMLtoPDF offers more modern features and active development. Flying Saucer, while more established, has slower development and may lack support for newer CSS properties. OpenHTMLtoPDF's code appears more streamlined, using a builder pattern, while Flying Saucer uses a more traditional object-oriented approach. Users should consider their specific needs, desired features, and project stability when choosing between the two libraries.

Converts a pdf file into a text file while keeping the layout of the original pdf. Useful to extract the content from a table in a pdf file for instance. This is a subclass of PDFTextStripper class (from the Apache PDFBox library).

Pros of PDFLayoutTextStripper

  • Focused specifically on extracting text from PDF documents while preserving layout
  • Lightweight and easy to integrate into existing Java projects
  • Utilizes Apache PDFBox library for robust PDF parsing capabilities

Cons of PDFLayoutTextStripper

  • Limited to text extraction and doesn't offer HTML rendering capabilities
  • Less actively maintained compared to FlyingSaucer
  • Narrower scope of functionality, primarily focused on PDF text extraction

Code Comparison

PDFLayoutTextStripper:

PDDocument document = PDDocument.load(new File("sample.pdf"));
PDFLayoutTextStripper stripper = new PDFLayoutTextStripper();
String text = stripper.getText(document);

FlyingSaucer:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("input.html"));
renderer.layout();
renderer.createPDF(new FileOutputStream("output.pdf"));

While PDFLayoutTextStripper focuses on extracting text from PDFs, FlyingSaucer is primarily used for converting HTML to PDF. The code snippets demonstrate their different use cases and APIs. PDFLayoutTextStripper provides a straightforward method for text extraction, while FlyingSaucer offers more complex functionality for PDF generation from HTML sources.

iText for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText can be a boon to nearly every workflow.

Pros of iText

  • More comprehensive PDF manipulation capabilities, including creation, editing, and digital signatures
  • Actively maintained with regular updates and extensive documentation
  • Supports both Java and .NET platforms

Cons of iText

  • Commercial license required for many use cases, which can be costly
  • Steeper learning curve due to its extensive feature set
  • Larger library size, which may impact application size and performance

Code Comparison

iText example:

PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
document.add(new Paragraph("Hello World!"));
document.close();

Flying Saucer example:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("input.html"));
renderer.layout();
renderer.createPDF(new FileOutputStream("output.pdf"));

Summary

iText is a powerful, feature-rich PDF library with broad capabilities, while Flying Saucer focuses primarily on HTML-to-PDF conversion. iText offers more extensive PDF manipulation options but comes with licensing costs and a steeper learning curve. Flying Saucer is open-source and simpler to use for basic HTML-to-PDF conversion tasks but has more limited functionality compared to iText.

2,609

Mirror of Apache PDFBox

Pros of PDFBox

  • More comprehensive PDF manipulation capabilities, including creation, modification, and extraction
  • Actively maintained by Apache Software Foundation with regular updates
  • Larger community and ecosystem, providing better support and resources

Cons of PDFBox

  • Steeper learning curve due to its extensive feature set
  • Larger library size, which may impact application size and performance
  • More complex setup and configuration compared to Flying Saucer

Code Comparison

Flying Saucer (HTML to PDF conversion):

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("input.html"));
renderer.layout();
renderer.createPDF(new FileOutputStream("output.pdf"));

PDFBox (PDF creation):

PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.showText("Hello, World!");
contentStream.endText();
contentStream.close();
document.save("output.pdf");
document.close();

Flying Saucer is primarily focused on converting HTML/CSS to PDF, while PDFBox offers a wider range of PDF manipulation features. The code examples demonstrate the different approaches: Flying Saucer's simplicity in HTML-to-PDF conversion versus PDFBox's more detailed PDF creation process.

4,646

Fast, Nimble PDF Writer for Ruby

Pros of Prawn

  • Written in Ruby, making it more accessible for Ruby developers
  • Focuses specifically on PDF generation, offering more specialized features
  • Lighter weight and potentially faster for PDF-only tasks

Cons of Prawn

  • Limited to PDF generation, unlike Flying Saucer's HTML/CSS rendering capabilities
  • May require more manual layout and styling compared to HTML/CSS approach
  • Less suitable for converting existing web content to PDF

Code Comparison

Flying Saucer (Java):

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("input.html"));
renderer.layout();
renderer.createPDF(new FileOutputStream("output.pdf"));

Prawn (Ruby):

Prawn::Document.generate("output.pdf") do
  text "Hello, World!"
  move_down 20
  text "This is a PDF generated with Prawn."
end

The code comparison shows that Flying Saucer uses HTML as input, while Prawn requires programmatic content generation. Flying Saucer is more suitable for converting existing HTML/CSS, whereas Prawn offers more fine-grained control over PDF content creation directly in Ruby.

Convert HTML to PDF using Webkit (QtWebKit)

Pros of wkhtmltopdf

  • Supports a wider range of HTML and CSS features, including modern web technologies
  • Faster rendering speed for complex layouts and large documents
  • Ability to generate PDFs from URLs, not just local HTML files

Cons of wkhtmltopdf

  • Larger file size and more dependencies, making it harder to integrate into some projects
  • Less control over the PDF generation process compared to Java-based solutions
  • May require more system resources to run effectively

Code Comparison

While a direct code comparison is not particularly relevant due to the different nature of these projects, here's a brief example of how they might be used:

flyingsaucer:

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("input.html"));
renderer.layout();
renderer.createPDF(new FileOutputStream("output.pdf"));

wkhtmltopdf:

wkhtmltopdf https://example.com output.pdf

Both projects aim to convert HTML to PDF, but they approach the task differently. flyingsaucer is a Java library that can be integrated into Java applications, while wkhtmltopdf is a standalone command-line tool that can be used in various environments and programming languages through system calls or wrappers.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Flying Saucer Download License: LGPL v2.1

OVERVIEW

Flying Saucer is a pure-Java library for rendering arbitrary well-formed XML (or XHTML) using CSS 2.1 for layout and formatting, output to Swing panels, PDF, and images.

Comprehensive documentation available in The Flying Saucer User's Guide.

If you use Flying Saucer in a project, please tell us; it helps suggest directions for the code and may inspire other developers.

LICENSE

Flying Saucer is distributed under the LGPL. Flying Saucer itself is licensed under the GNU Lesser General Public License, version 2.1 or later, available at http://www.gnu.org/copyleft/lesser.html. You can use Flying Saucer in any way and for any purpose you want as long as you respect the terms of the license. A copy of the LGPL license is included as LICENSE-LGPL-2.1.txt or LICENSE-LGPL-3.txt in our distributions and in our source tree.

Flying Saucer uses a couple of FOSS packages to get the job done. A list of these, along with the license they each have, is listed in the LICENSE file in our distribution.

GETTING FLYING SAUCER

New releases of Flying Saucer are distributed through Maven. The available artifacts are:

  • org.xhtmlrenderer:flying-saucer-core - Core library and Java2D rendering
  • org.xhtmlrenderer:flying-saucer-pdf - PDF output using OpenPDF (ex. iText 2.x)
  • org.xhtmlrenderer:flying-saucer-pdf-openpdf - not supported anymore (replaced by flying-saucer-pdf)
  • org.xhtmlrenderer:flying-saucer-swt - SWT output
  • org.xhtmlrenderer:flying-saucer-log4j - Logging plugin for log4j

Flying Saucer from version 9.5.0, requires Java 11 or later. Flying Saucer from version 9.6.0, requires Java 17 or later.

GETTING STARTED

See the sample code under the flying-saucer-examples directory.

flying-saucer-core, flying-saucer-pdf, and flying-saucer-swt must be on the build path, as well as the SWT JAR for your OS.

org.xhtmlrenderer.demo.browser.BrowserStartup will start the browser demo.

Other notable entry points include:

  • org.xhtmlrenderer.simple.XHTMLPanel
  • org.xhtmlrenderer.pdf.PDFRenderer
  • org.xhtmlrenderer.simple.ImageRenderer

CONTACT

See the discussion group for ongoing discussions.

PROJECT STATUS

Excerpt from the latest merge/release policy:

If you create a PR that can merge without conflicts, I'll merge it. I might ask for additional changes (or feedback from others), but I expect that will be extremely rare.

The basic contract is this: If you're still using Flying Saucer, need something changed, and are willing to do the work yourself, the change will be accepted and released.

Along that line, please also let me know if you need a release done right away. Otherwise, I'll bundle up the changes and do a release every four to six months as long as it's required.

This is also the reason why Issues are currently deactivated on GitHub.

HISTORIC LINKS