本文介绍: Apache poi xwpf word转PDF中文显示问题解决,一定要往字体上靠,不要想着代码简单设置一下就搞定了,尤其是在精简的Docker容器中。

目录

POM依赖

核心配置代码

PDF下载流问题

WPS模板问题


问题解决方法https://github.com/opensagres/xdocreport/issues/161

POM依赖

   <properties>
        &lt;java.version&gt;1.8</java.version>
        <poi.version>3.14</poi.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>2.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

记住word转换如果没问题,那么就要设置pdf字体,这个是铁律!

核心配置代码

将PdfOptions提出来作为方法使用

 private PdfOptions getPdfOptions() {
        PdfOptions pdfOptions = PdfOptions.create();
        // 解决中文显示问题
        pdfOptions.fontProvider(new IFontProvider() {
            @Override
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    File file = new File(pdfChineseTTF);
                    Preconditions.checkState(file.exists(), "中文字体文件存在:" + pdfChineseTTF);
                    BaseFont bfChinese = BaseFont.createFont(pdfChineseTTF, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null) {
                        fontChinese.setFamily(familyName);
                    }
                    return fontChinese;
                } catch (Throwable e) {
                    e.printStackTrace();
                    // An error occurs, use the default font provider.
                    return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
                }
            }
        });
        return pdfOptions;
    }

PDF下载流问题

注意前端PDF文件流空白问题参考以下方案进行处理

js下载PDF文件流,打开空白问题_使用jspdf导出服务器上pdf文件的时候文件里面没有内容_夏小锋的博客-CSDN博客

前端下载二进制流pdf文件,下载成功,打开空白问题解决_前端下载pdf文件流打开空白_Helen_fight的博客-CSDN博客

WPS模板问题

使用wps处理template.docx文件不能识别到页脚信息,请使用微软提供的office进行编辑

以下是WPS footer xml部分,不是OOXML需要解析范式

<pkg:part pkg:name="/word/footer1.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml">
    <pkg:xmlData>
      <w:ftr xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpsCustomData="http://www.wps.cn/officeDocument/2013/wpsCustomData" mc:Ignorable="w14 w15 wp14">
        <w:p>
          <w:pPr>
            <w:pStyle w:val="2"/>
            <w:tabs>
              <w:tab w:val="right" w:pos="8426"/>
            </w:tabs>
            <w:jc w:val="right"/>
            <w:rPr>
              <w:rFonts w:hint="default" w:eastAsia="宋体"/>
              <w:lang w:val="en-US" w:eastAsia="zh-CN"/>
            </w:rPr>
          </w:pPr>
          <w:r>
            <w:rPr>
              <w:sz w:val="18"/>
            </w:rPr>
            <mc:AlternateContent>
              <mc:Choice Requires="wps">
                <w:drawing>
                  <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251659264" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1">
                    <wp:simplePos x="0" y="0"/>
                    <wp:positionH relativeFrom="margin">
                      <wp:align>center</wp:align>
                    </wp:positionH>
                    <wp:positionV relativeFrom="paragraph">
                      <wp:posOffset>0</wp:posOffset>
                    </wp:positionV>
                    <wp:extent cx="1828800" cy="1828800"/>
                    <wp:effectExtent l="0" t="0" r="0" b="0"/>
                    <wp:wrapNone/>
                    <wp:docPr id="3" name="文本框 3"/>
                    <wp:cNvGraphicFramePr/>
                    <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                      <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
                        <wps:wsp>
                          <wps:cNvSpPr txBox="1"/>
                          <wps:spPr>
                            <a:xfrm>
                              <a:off x="0" y="0"/>
                              <a:ext cx="1828800" cy="1828800"/>
                            </a:xfrm>
                            <a:prstGeom prst="rect">
                              <a:avLst/>
                            </a:prstGeom>
                            <a:noFill/>
                            <a:ln w="6350">
                              <a:noFill/>
                            </a:ln>
                          </wps:spPr>
                          <wps:style>
                            <a:lnRef idx="0">
                              <a:schemeClr val="accent1"/>
                            </a:lnRef>
                            <a:fillRef idx="0">
                              <a:schemeClr val="accent1"/>
                            </a:fillRef>
                            <a:effectRef idx="0">
                              <a:schemeClr val="accent1"/>
                            </a:effectRef>
                            <a:fontRef idx="minor">
                              <a:schemeClr val="dk1"/>
                            </a:fontRef>
                          </wps:style>
                          <wps:txbx>
                            <w:txbxContent>
                              <w:p>
                                <w:pPr>
                                  <w:pStyle w:val="2"/>
                                </w:pPr>
                                <w:r>
                                  <w:t xml:space="preserve">第 </w:t>
                                </w:r>
                                <w:r>
                                  <w:fldChar w:fldCharType="begin"/>
                                </w:r>
                                <w:r>
                                  <w:instrText xml:space="preserve"> PAGE  * MERGEFORMAT </w:instrText>
                                </w:r>
                                <w:r>
                                  <w:fldChar w:fldCharType="separate"/>
                                </w:r>
                                <w:r>
                                  <w:t>1</w:t>
                                </w:r>
                                <w:r>
                                  <w:fldChar w:fldCharType="end"/>
                                </w:r>
                                <w:r>
                                  <w:t xml:space="preserve"> 页 共 </w:t>
                                </w:r>
                                <w:r>
                                  <w:fldChar w:fldCharType="begin"/>
                                </w:r>
                                <w:r>
                                  <w:instrText xml:space="preserve"> NUMPAGES  * MERGEFORMAT </w:instrText>
                                </w:r>
                                <w:r>
                                  <w:fldChar w:fldCharType="separate"/>
                                </w:r>
                                <w:r>
                                  <w:t>2</w:t>
                                </w:r>
                                <w:r>
                                  <w:fldChar w:fldCharType="end"/>
                                </w:r>
                                <w:r>
                                  <w:t xml:space="preserve"> 页</w:t>
                                </w:r>
                              </w:p>
                            </w:txbxContent>
                          </wps:txbx>
                          <wps:bodyPr rot="0" spcFirstLastPara="0" vertOverflow="overflow" horzOverflow="overflow" vert="horz" wrap="none" lIns="0" tIns="0" rIns="0" bIns="0" numCol="1" spcCol="0" rtlCol="0" fromWordArt="0" anchor="t" anchorCtr="0" forceAA="0" upright="0" compatLnSpc="1">
                            <a:spAutoFit/>
                          </wps:bodyPr>
                        </wps:wsp>
                      </a:graphicData>
                    </a:graphic>
                  </wp:anchor>
                </w:drawing>
              </mc:Choice>
              <mc:Fallback>
                <w:pict>
                  <v:shape id="_x0000_s1026" o:spid="_x0000_s1026" o:spt="202" type="#_x0000_t202" style="position:absolute;left:0pt;margin-top:0pt;height:144pt;width:144pt;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-wrap-style:none;z-index:251659264;mso-width-relative:page;mso-height-relative:page;" filled="f" stroked="f" coordsize="21600,21600" o:gfxdata="UEsDBAoAAAAAAIdO4kAAAAAAAAAAAAAAAAAEAAAAZHJzL1BLAwQUAAAACACHTuJAs0lY7tAAAAAF
AQAADwAAAGRycy9kb3ducmV2LnhtbE2PQUvDQBCF74L/YRmhN7tpKxJiNgVL06Ng48HjNjsm0d3Z
sLtN4793FEEvwzze8OZ75XZ2VkwY4uBJwWqZgUBqvRmoU/DS1Lc5iJg0GW09oYJPjLCtrq9KXRh/
oWecjqkTHEKx0Ar6lMZCytj26HRc+hGJvTcfnE4sQydN0BcOd1aus+xeOj0Qf+j1iLse24/j2SnY
1U0TJozBvuKh3rw/Pd7hflZqcbPKHkAknNPfMXzjMzpUzHTyZzJRWAVcJP1M9tZ5zvL0u8iqlP/p
qy9QSwMEFAAAAAgAh07iQOGA044yAgAAYQQAAA4AAABkcnMvZTJvRG9jLnhtbK1UzY7TMBC+I/EO
lu80aStWVdV0VbYqQqrYlQri7DpOE8l/st0m5QHgDThx4c5z9Tn2c366aOGwBy7O2DP+xt83M1nc
NkqSk3C+Mjqj41FKidDc5JU+ZPTzp82bGSU+MJ0zabTI6Fl4ert8/WpR27mYmNLIXDgCEO3ntc1o
GYKdJ4nnpVDMj4wVGs7COMUCtu6Q5I7VQFcymaTpTVIbl1tnuPAep+vOSXtE9xJAUxQVF2vDj0ro
0KE6IVkAJV9W1tNl+9qiEDzcF4UXgciMgmloVySBvY9rslyw+cExW1a8fwJ7yROecVKs0kh6hVqz
wMjRVX9BqYo7400RRtyopCPSKgIW4/SZNruSWdFygdTeXkX3/w+Wfzw9OFLlGZ1SoplCwS8/vl9+
/r78+kamUZ7a+jmidhZxoXlnGjTNcO5xGFk3hVPxCz4Efoh7voormkB4vDSbzGYpXBy+Y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D6m9QSwECFAAU
AAAACACHTuJAfublIPcAAADhAQAAEwAAAAAAAAABACAAAACbBAAAW0NvbnRlbnRfVHlwZXNdLnht
bFBLAQIUAAoAAAAAAIdO4kAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAEAAAAH0DAABfcmVscy9QSwEC
FAAUAAAACACHTuJAihRmPNEAAACUAQAACwAAAAAAAAABACAAAAChAwAAX3JlbHMvLnJlbHNQSwEC
FAAKAAAAAACHTuJAAAAAAAAAAAAAAAAABAAAAAAAAAAAABAAAAAAAAAAZHJzL1BLAQIUABQAAAAI
AIdO4kCzSVju0AAAAAUBAAAPAAAAAAAAAAEAIAAAACIAAABkcnMvZG93bnJldi54bWxQSwECFAAU
AAAACACHTuJA4YDTjjICAABhBAAADgAAAAAAAAABACAAAAAfAQAAZHJzL2Uyb0RvYy54bWxQSwUG
AAAAAAYABgBZAQAAwwUAAAAA
">
                    <v:fill on="f" focussize="0,0"/>
                    <v:stroke on="f" weight="0.5pt"/>
                    <v:imagedata o:title=""/>
                    <o:lock v:ext="edit" aspectratio="f"/>
                    <v:textbox inset="0mm,0mm,0mm,0mm" style="mso-fit-shape-to-text:t;">
                      <w:txbxContent>
                        <w:p>
                          <w:pPr>
                            <w:pStyle w:val="2"/>
                          </w:pPr>
                          <w:r>
                            <w:t xml:space="preserve">第 </w:t>
                          </w:r>
                          <w:r>
                            <w:fldChar w:fldCharType="begin"/>
                          </w:r>
                          <w:r>
                            <w:instrText xml:space="preserve"> PAGE  * MERGEFORMAT </w:instrText>
                          </w:r>
                          <w:r>
                            <w:fldChar w:fldCharType="separate"/>
                          </w:r>
                          <w:r>
                            <w:t>1</w:t>
                          </w:r>
                          <w:r>
                            <w:fldChar w:fldCharType="end"/>
                          </w:r>
                          <w:r>
                            <w:t xml:space="preserve"> 页 共 </w:t>
                          </w:r>
                          <w:r>
                            <w:fldChar w:fldCharType="begin"/>
                          </w:r>
                          <w:r>
                            <w:instrText xml:space="preserve"> NUMPAGES  * MERGEFORMAT </w:instrText>
                          </w:r>
                          <w:r>
                            <w:fldChar w:fldCharType="separate"/>
                          </w:r>
                          <w:r>
                            <w:t>2</w:t>
                          </w:r>
                          <w:r>
                            <w:fldChar w:fldCharType="end"/>
                          </w:r>
                          <w:r>
                            <w:t xml:space="preserve"> 页</w:t>
                          </w:r>
                        </w:p>
                      </w:txbxContent>
                    </v:textbox>
                  </v:shape>
                </w:pict>
              </mc:Fallback>
            </mc:AlternateContent>
          </w:r>
          <w:r>
            <w:rPr>
              <w:rFonts w:hint="eastAsia"/>
              <w:sz w:val="18"/>
              <w:lang w:val="en-US" w:eastAsia="zh-CN"/>
            </w:rPr>
            <w:tab/>
          </w:r>
          <w:r>
            <w:rPr>
              <w:rFonts w:hint="eastAsia"/>
              <w:sz w:val="18"/>
              <w:lang w:val="en-US" w:eastAsia="zh-CN"/>
            </w:rPr>
            <w:tab/>
          </w:r>
          <w:r>
            <w:rPr>
              <w:rFonts w:hint="eastAsia"/>
              <w:sz w:val="18"/>
              <w:lang w:val="en-US" w:eastAsia="zh-CN"/>
            </w:rPr>
            <w:t>合同编号:</w:t>
          </w:r>
          <w:r>
            <w:t>skdfdz20231019142503</w:t>
          </w:r>
        </w:p>
      </w:ftr>
    </pkg:xmlData>
  </pkg:part>

原文地址:https://blog.csdn.net/boonya/article/details/133892573

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20452.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注