Skip to content

Instantly share code, notes, and snippets.

@RuolinZheng08
Last active February 8, 2023 04:50
Show Gist options
  • Select an option

  • Save RuolinZheng08/2ea8902f3316bf51c3555dce4a2dff5f to your computer and use it in GitHub Desktop.

Select an option

Save RuolinZheng08/2ea8902f3316bf51c3555dce4a2dff5f to your computer and use it in GitHub Desktop.

Revisions

  1. RuolinZheng08 revised this gist Sep 5, 2020. 3 changed files with 35 additions and 39 deletions.
    24 changes: 9 additions & 15 deletions htmlToPdf.java → HtmlToPdf.java
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,14 @@
    import java.io.*;
    import java.util.Scanner;

    import org.jsoup.Jsoup;
    import org.xhtmlrenderer.pdf.ITextRenderer;
    import org.jsoup.nodes.Document;
    import org.xhtmlrenderer.extend.FontResolver;
    import org.xhtmlrenderer.pdf.ITextRenderer;

    public class htmlToPdf {
    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 html = "<h1>hello</h1>";
    String xhtml = htmlToXhtml(html);
    xhtmlToPdf(xhtml);
    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) throws IOException {
    File output = new File("index.pdf");
    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);
    9 changes: 0 additions & 9 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    Java 8+
    ```
    mvn clean install
    ```
    IntelliJ > Find Actions > Reload All Maven Projects

    Input: `index.html` (can link to `index.css`)

    Output: `index.pdf`
    41 changes: 26 additions & 15 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -5,37 +5,48 @@
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>htmlToPdf</artifactId>
    <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>

    <!-- 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.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    </project>
  2. RuolinZheng08 revised this gist Aug 11, 2020. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions pom.xml
    Original 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>
    <dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>xhtmlrenderer</artifactId>
    <version>20091012</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
    <dependency>
    <groupId>org.jsoup</groupId>
  3. RuolinZheng08 revised this gist Jul 30, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    Java 8+
    ```
    mvn clean install
    ```
  4. RuolinZheng08 revised this gist Jul 30, 2020. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    ```
    mvn clean install
    ```
    IntelliJ > Find Actions > Reload All Maven Projects
    IntelliJ > Find Actions > Reload All Maven Projects

    Input: `index.html` (can link to `index.css`)

    Output: `index.pdf`
  5. RuolinZheng08 revised this gist Jul 30, 2020. 1 changed file with 2 additions and 39 deletions.
    41 changes: 2 additions & 39 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -38,45 +38,8 @@
    </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>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    </project>
  6. RuolinZheng08 created this gist Jul 30, 2020.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    ```
    mvn clean install
    ```
    IntelliJ > Find Actions > Reload All Maven Projects
    37 changes: 37 additions & 0 deletions htmlToPdf.java
    Original 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();
    }
    }
    82 changes: 82 additions & 0 deletions pom.xml
    Original 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>