Skip to content

Instantly share code, notes, and snippets.

@RuolinZheng08
Last active February 8, 2023 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save RuolinZheng08/2ea8902f3316bf51c3555dce4a2dff5f to your computer and use it in GitHub Desktop.
Save RuolinZheng08/2ea8902f3316bf51c3555dce4a2dff5f to your computer and use it in GitHub Desktop.
[Java] HTML (to XHTML) then to PDF with CSS 2.1 Support | Flying Saucer + OpenPDF
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.xhtmlrenderer.extend.FontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class HtmlToPdf {
public static void main(String[] args) throws IOException {
String html = "<h1>hello</h1>";
String xhtml = htmlToXhtml(html);
xhtmlToPdf(xhtml, "output.pdf");
}
private static String htmlToXhtml(String html) {
Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
return document.html();
}
private static void xhtmlToPdf(String xhtml, String outFileName) throws IOException {
File output = new File(outFileName);
ITextRenderer iTextRenderer = new ITextRenderer();
FontResolver resolver = iTextRenderer.getFontResolver();
iTextRenderer.getFontResolver().addFont("MyFont.ttf", true);
iTextRenderer.setDocumentFromString(xhtml);
iTextRenderer.layout();
OutputStream os = new FileOutputStream(output);
iTextRenderer.createPDF(os);
os.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>html-to-pdf</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.13</maven.compiler.source>
<maven.compiler.target>1.13</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-core -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-openpdf -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>9.1.20</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment