java converts database data into word documents and generates pdf files

Posted by benwestgarth on Sun, 16 Jan 2022 20:56:33 +0100

Purpose: to present the data to users by converting it into word pictures

Tools: openoffice +java code + database, of which openoffice has windows version and Linux version.

The download address of openoffice is: link: https://pan.baidu.com/s/1Y5Ra3TfNCcvVX1bF-29hjw Password: 44ms

The installation and startup of openoffice is not described: Baidu on the Internet.

The following are the steps to achieve the purpose.

Step 1: create the connection pool of openoffice and implement it with linkedlist. Simple connection pool to avoid excessive resource consumption!

 <!--word turn pdf as well as pdf To use pictures jar-->
        <dependency>
            <groupId>wisemax</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.2.1</version>
        </dependency>
 
        <dependency>
            <groupId>xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.1.3</version>
        </dependency>
 
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
 
 
        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.1</version>
        </dependency>
 
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.0.1</version>
        </dependency>
 
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.slf4j</groupId>-->
            <!--<artifactId>slf4j-jdk14</artifactId>-->
            <!--<version>1.5.6</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.icepdf</groupId>
            <artifactId>icepdf-core</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.2.1</version>
        </dependency>

Step 2: convert the data into the corresponding template information. I use the Word template engine, an open source project on GitHub https://github.com/Sayi/poi-tl

The specific steps and methods of generating documents are introduced in detail.

Step 3: generate data into word document

① : prepare a template as shown in the above figure or define a template yourself, and use {{xx}} to occupy the data to be dynamically generated.
The above code is the method of generating word from data. Output path is

The put key in the map is a placeholder variable in the template. Value is the corresponding value.

Step 4: convert word into pdf. At this time, openoffice tool is used. Check whether the tool is on.
Step 5: convert pdf into picture. The format of the picture can be png or jpg. The name of the picture can be customized. And return the downloaded url

Reminders and suggestions:
1: openoffice installation, online Baidu has, very simple, carefully read the open source project on github.
2: The template for generating word is self-defined. The text position, bold, italic, underline, etc. are provided by POI TL, similar to freemaker. But I think it's easier to use than freemaker.
3: The generated word is one page, but the pdf may be two pages. The last converted picture is converted according to the number of pages of the pdf. Anyway, it will be converted in the end.
4: Finally, the generated image url can be modified according to its own needs.
5: The jar package may be lacking. The blogger's jar is just right. If there is a conflict or lack, you can search the information and modify it.

Topics: Java Database pdf