中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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遇到的坑問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大埔县| 新巴尔虎右旗| 阜阳市| 昆山市| 耒阳市| 五华县| 崇左市| 宜兴市| 安图县| 保定市| 电白县| 西吉县| 吴川市| 鹤岗市| 巫山县| 呼伦贝尔市| 尼勒克县| 商丘市| 同心县| 清徐县| 桑植县| 东乡| 忻州市| 诸城市| 大新县| 兰考县| 安阳县| 仪征市| 温泉县| 四会市| 竹溪县| 新绛县| 比如县| 当阳市| 霸州市| 龙川县| 嫩江县| 马尔康县| 南陵县| 尼玛县| 图木舒克市|