Last active
February 8, 2023 04:50
-
-
Save RuolinZheng08/2ea8902f3316bf51c3555dce4a2dff5f to your computer and use it in GitHub Desktop.
Revisions
-
RuolinZheng08 revised this gist
Sep 5, 2020 . 3 changed files with 35 additions and 39 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,14 @@ 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) { @@ -25,9 +17,11 @@ private static String htmlToXhtml(String html) { 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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,37 +5,48 @@ <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> -
RuolinZheng08 revised this gist
Aug 11, 2020 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,11 +22,7 @@ <artifactId>flying-saucer-pdf-openpdf</artifactId> <version>9.1.20</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <groupId>org.jsoup</groupId> -
RuolinZheng08 revised this gist
Jul 30, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ Java 8+ ``` mvn clean install ``` -
RuolinZheng08 revised this gist
Jul 30, 2020 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,8 @@ ``` mvn clean install ``` IntelliJ > Find Actions > Reload All Maven Projects Input: `index.html` (can link to `index.css`) Output: `index.pdf` -
RuolinZheng08 revised this gist
Jul 30, 2020 . 1 changed file with 2 additions and 39 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,45 +38,8 @@ </dependencies> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project> -
RuolinZheng08 created this gist
Jul 30, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ ``` mvn clean install ``` IntelliJ > Find Actions > Reload All Maven Projects This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ import java.io.*; import java.util.Scanner; import org.jsoup.Jsoup; import org.xhtmlrenderer.pdf.ITextRenderer; import org.jsoup.nodes.Document; public class htmlToPdf { public static void main(String[] args) throws IOException { File input = new File("index.html"); Scanner scanner = new Scanner(input); StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); sb.append(line); } String html = sb.toString(); String xhtml = htmlToXhtml(html); xhtmlToPdf(xhtml); } 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) throws IOException { File output = new File("index.pdf"); ITextRenderer iTextRenderer = new ITextRenderer(); iTextRenderer.setDocumentFromString(xhtml); iTextRenderer.layout(); OutputStream os = new FileOutputStream(output); iTextRenderer.createPDF(os); os.close(); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ <?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>htmlToPdf</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- 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> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>xhtmlrenderer</artifactId> <version>20091012</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.13.1</version> </dependency> </dependencies> <properties> <maven.compiler.sourceimport java.io.*; import java.util.Scanner; import org.jsoup.Jsoup; import org.xhtmlrenderer.pdf.ITextRenderer; import org.jsoup.nodes.Document; public class htmlToPdf { public static void main(String[] args) throws IOException { File input = new File("index.html"); Scanner scanner = new Scanner(input); StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); sb.append(line); } String html = sb.toString(); String xhtml = htmlToXhtml(html); xhtmlToPdf(xhtml); } 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) throws IOException { File output = new File("index.pdf"); ITextRenderer iTextRenderer = new ITextRenderer(); iTextRenderer.setDocumentFromString(xhtml); iTextRenderer.layout(); OutputStream os = new FileOutputStream(output); iTextRenderer.createPDF(os); os.close(); } } >1.13</maven.compiler.source> <maven.compiler.target>1.13</maven.compiler.target> </properties> </project>