溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot怎樣解決引入資源文件生成pdf遇到的坑

發(fā)布時(shí)間:2021-09-29 17:17:27 來(lái)源:億速云 閱讀:139 作者:柒染 欄目:大數(shù)據(jù)

springboot怎樣解決引入資源文件生成pdf遇到的坑,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

最近項(xiàng)目中,需求是將設(shè)備信息生成pdf  .首先想到的就是thymeleaf + flying-saucer-pdf  來(lái)實(shí)現(xiàn)動(dòng)態(tài)html 轉(zhuǎn)pdf,經(jīng)過(guò)一番折騰,終于解決了,期間想過(guò)放棄這個(gè)方式,但是覺(jué)得只有遇到問(wèn)題,解決問(wèn)題,理解能力和編程水平才能提高!奔著這一目的,終于搞定了!

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

代碼量很少,但是卻開(kāi)箱即用,避免少走很多彎路

第一個(gè)坑,在開(kāi)發(fā)工具中運(yùn)行一切正常,打成jar 包后,部署出現(xiàn)模板文件和字體文件無(wú)法讀取 ,首先想到在配置文件中添加外部資源路徑提供給springboot 讀取,但是項(xiàng)目是運(yùn)維人員部署,考慮到部署會(huì)創(chuàng)建外部目錄。這樣容易出錯(cuò),最后通過(guò)一下代碼解決

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

  將資源轉(zhuǎn)為流,然后獲取,避免讀取物理路徑產(chǎn)生的資源不存在

第二個(gè)坑,我們的項(xiàng)目使用的springboot 2.0.1版本的,但是thyleaf 卻需要高版本的,此時(shí)需要調(diào)整版本依賴關(guān)系,最后在pom文件中定義了這樣的關(guān)系

 高版本的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>

第三個(gè)坑  字體文件在打成jar包發(fā)現(xiàn)字體文件在window 電腦無(wú)法識(shí)別 ,字體文件被損壞.此時(shí)我們需要排序資源目錄里面文件類型

    <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>
            <!--不同環(huán)境的打包插件-->
            <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包解壓后查看,發(fā)現(xiàn)字體文件正常!

關(guān)于springboot怎樣解決引入資源文件生成pdf遇到的坑問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI