springboot怎样解决引入资源文件生成pdf遇到的坑

发布时间:2021-09-29 17:17:27 作者:柒染
来源:亿速云 阅读:139

springboot怎样解决引入资源文件生成pdf遇到的坑,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

最近项目中,需求是将设备信息生成pdf  .首先想到的就是thymeleaf + flying-saucer-pdf  来实现动态html 转pdf,经过一番折腾,终于解决了,期间想过放弃这个方式,但是觉得只有遇到问题,解决问题,理解能力和编程水平才能提高!奔着这一目的,终于搞定了!

码云上demo 地址  https://gitee.com/wanjianning_code/springboot-generate-pdf    

代码量很少,但是却开箱即用,避免少走很多弯路

第一个坑,在开发工具中运行一切正常,打成jar 包后,部署出现模板文件和字体文件无法读取 ,首先想到在配置文件中添加外部资源路径提供给springboot 读取,但是项目是运维人员部署,考虑到部署会创建外部目录。这样容易出错,最后通过一下代码解决

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();
}

  将资源转为流,然后获取,避免读取物理路径产生的资源不存在

第二个坑,我们的项目使用的springboot 2.0.1版本的,但是thyleaf 却需要高版本的,此时需要调整版本依赖关系,最后在pom文件中定义了这样的关系

 高版本的springboot  比如2.3.0  ,使用一下的依赖

<!-- Thymeleaf 模板引擎 -->
 <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
 </dependency>
 <dependency>
       <groupId>org.xhtmlrenderer</groupId>
       <artifactId>flying-saucer-pdf</artifactId>
       <version>9.1.6</version>
</dependency>

低版本的springboot  比如2.0.1 使用以下的依赖

<!-- Thymeleaf 模板引擎 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
	<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>ognl</groupId>
	<artifactId>ognl</artifactId>
	<version>3.1.12</version>
</dependency>
<dependency>
	<groupId>org.xhtmlrenderer</groupId>
	<artifactId>flying-saucer-pdf</artifactId>
	<version>9.1.6</version>
</dependency>

第三个坑  字体文件在打成jar包发现字体文件在window 电脑无法识别 ,字体文件被损坏.此时我们需要排序资源目录里面文件类型

    <build>
        <finalName>taskmanage</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                        <nonFilteredFileExtension>TTC</nonFilteredFileExtension>
                        <nonFilteredFileExtension>ttc</nonFilteredFileExtension>
                        <nonFilteredFileExtension>TTF</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>
                        com.isoft.system.BootApplication
                    </mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-compiler-plugin</artifactId>-->
            <!--<version>3.7.0</version>-->
            <!--<configuration>-->
            <!--<source>${java.version}</source>-->
            <!--<target>${java.version}</target>-->
            <!--</configuration>-->
            <!--</plugin>-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <instrumentation>
                        <ignoreMethodAnnotations>
                            <ignoreMethodAnnotation>lombok.Generated</ignoreMethodAnnotation>
                        </ignoreMethodAnnotations>
                    </instrumentation>
                    <check/>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--不同环境的打包插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>default-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes</outputDirectory>
                            <useDefaultDelimiters>false</useDefaultDelimiters>
                            <delimiters>
                                <delimiter>#</delimiter>
                            </delimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>true</filtering>
                                </resource>
                                <resource>
                                    <directory>src/main/resources.${spring.profiles.active}</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!-- 定义包含这些资源文件,能在jar包中获取这些文件 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <!--是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <!--是否替换资源中的属性-->
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

排除TTF和TTC 后缀的文件,最后打成jar包解压后查看,发现字体文件正常!

关于springboot怎样解决引入资源文件生成pdf遇到的坑问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. hadoop遇到的坑
  2. addView遇到的坑及其解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot

上一篇:如何实现批处理BAT创建快捷

下一篇:如何实现炫酷的批处理BAT走字特效源码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》