Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the pits encountered by introducing resource files to generate pdf by springboot

Shulou Source: shulou.com Published: 2022-06-02 03:37:08 10月16日 Update

Springboot how to solve the pit encountered in the introduction of resource files to generate pdf, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

In the recent project, the requirement is to generate equipment information into pdf. The first thing that comes to mind is that thymeleaf + flying-saucer-pdf to achieve dynamic html to pdf, after some twists and turns, finally solved, during the thought of giving up this way, but feel that only encounter problems, solve problems, understanding and programming level can be improved! With this goal, it is finally done!

Demo address https://gitee.com/wanjianning_code/springboot-generate-pdf on Code Cloud

The amount of code is very small, but it is used out of the box to avoid taking a lot of detours.

The first pit, everything runs normally in the development tool. After the jar package, the template file and font file cannot be read in the deployment. The first thought is to add an external resource path to the configuration file for springboot to read, but the project is deployed by operation and maintenance personnel, taking into account that the deployment will create an external directory. This is easy to make mistakes, and finally solve it through the code.

Public String test () throws IOException {String str = ""; InputStream instream = getClass (). GetClassLoader (). GetResourceAsStream ("templates/test.txt"); InputStreamReader isr = new InputStreamReader (instream); BufferedReader br = new BufferedReader (isr); StringBuffer content = new StringBuffer (); while (str = br.readLine ())! = null) {content = content.append (str);} return content.toString ();}

Convert the resource into a stream and get it to avoid reading the resource generated by the physical path does not exist

The second pit, our project uses version 2.0.1 of springboot, but thyleaf needs a higher version, so the version dependency needs to be adjusted. Finally, such a relationship is defined in the pom file.

Higher versions of springboot, such as 2.3.0, use the following dependencies

Org.thymeleaf thymeleaf org.xhtmlrenderer flying-saucer-pdf 9.1.6

Lower versions of springboot such as 2.0.1 use the following dependencies

Org.springframework.boot spring-boot-starter-thymeleaf 2.3.1.RELEASE ognl ognl 3.1.12 org.xhtmlrenderer flying-saucer-pdf 9.1.6

The third pit font file in the jar package found that the font file is not recognized in the window computer, the font file is corrupted. At this point, we need to sort the file types in the resource directory.

Taskmanage org.apache.maven.plugins maven-resources-plugin ttf TTC ttc TTF woff Woff2 org.springframework.boot spring-boot-maven-plugin com.isoft.system.BootApplication Repackage Org.codehaus.mojo cobertura-maven-plugin 2.7 html xml Lombok.Generated clean Org.apache.maven.plugins maven-resources-plugin 3.0.2 default-resources validate Copy-resources target/classes false # Src/main/resources/ true Src/main/resources.$ {spring.profiles.active} false Src/main/java * * / * .properties * * / .xml * * / * .yml false src/main/resources * * /. * false

Exclude TTF and TTC suffixes of the file, and finally into a jar package decompressed to view, found that the font file is normal!

This is the answer to the problem of how springboot solves the problem of introducing resource files to generate pdf. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Files resources fonts versions questions builds projects code more directories paths help answers easy easy three people creative content dynamic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Huawei macOS MariaDB