Simple use of openhtmltopdf

The main reason for writing this article is that the whole Baidu can't find an article using openhtmltopdf without a demonstration project, and github only has a simple demonstration, which can't see the effect.

openhtmltopdf is an open source framework for html fragment to pdf file. The source code is on github, and the address is https://github.com/danfickle/openhtmltopdf

At present, the html fragment does not support js. It supports pictures, some css3 styles and some h5 styles. Here's a simple example.

1.pom.xml import

<dependency>
    <!-- ALWAYS required. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-core</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Required for PDF output. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-pdfbox</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Required for image output only. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-java2d</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Optional, leave out if you do not need right-to-left or bi-directional text support. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-rtl-support</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Optional, leave out if you do not need HTML5 parsing support. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-jsoup-dom-converter</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Optional, leave out if you do not need logging via slf4j. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-slf4j</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Optional, leave out if you do not need logging via log4j. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-log4j</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Optional, leave out if you do not need SVG support. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-svg-support</artifactId>
    <version>${openhtml.version}</version>
</dependency>

<dependency>
    <!-- Optional, leave out if you do not need MathML support. -->
    <!-- Introduced in RC-13. -->
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-mathml-support</artifactId>
    <version>${openhtml.version}</version>
</dependency>

2. Simple use of code

public class OpenHtmlToPDF {
    public static void main(String[] args) {
        try (OutputStream os = new FileOutputStream("F://pdf//pdfbox//out3.pdf ") {/ / output pdf
            PdfRendererBuilder builder = new PdfRendererBuilder();
            builder.useFastMode();
            //The following method is to specify the font file by yourself or the Chinese in the transferred pdf file will become####
            builder.useFont(new FSSupplier<InputStream>() {
                @Override
                public InputStream supply() {
                    try {
                        //Specify font file
                        return new FileInputStream("F://pdf//pdfbox//simhei.ttf");
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }, "simhei", 400, BaseRendererBuilder.FontStyle.NORMAL, true); //The second parameter must be the same as the file name!! Act on html page  
            //The first parameter is the html page. The second parameter is similar to a sketchpad. I don't know what the function is. I think the example project uses it
            builder.withHtmlContent(readTxt("F://pdf//pdfbox//pdf-css3.html"),                              
            Surrogate.Generator.class.getResource("/root.htm").toExternalForm());
            builder.toStream(os);
            builder.run();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

3. Precautions for HTML page

1. About fonts.

If you specify your own font file, you need to add it to the html page

<style>
* {
Font family: 'simhei'; / / value is the second parameter when specifying font
}
</style>

2. The label must be closed or it will fail to parse

Effect

Transferred pdf

Tags: github Fragment css3 log4j

Posted on Tue, 03 Dec 2019 07:57:54 -0800 by Anxious