Apache poi xwpf word转PDF中文显示与页码问题解决

目录

POM依赖

核心配置代码

PDF下载流问题

WPS模板问题


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

POM依赖

   <properties>
        <java.version>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&#10;AQAADwAAAGRycy9kb3ducmV2LnhtbE2PQUvDQBCF74L/YRmhN7tpKxJiNgVL06Ng48HjNjsm0d3Z&#10;sLtN4793FEEvwzze8OZ75XZ2VkwY4uBJwWqZgUBqvRmoU/DS1Lc5iJg0GW09oYJPjLCtrq9KXRh/&#10;oWecjqkTHEKx0Ar6lMZCytj26HRc+hGJvTcfnE4sQydN0BcOd1aus+xeOj0Qf+j1iLse24/j2SnY&#10;1U0TJozBvuKh3rw/Pd7hflZqcbPKHkAknNPfMXzjMzpUzHTyZzJRWAVcJP1M9tZ5zvL0u8iqlP/p&#10;qy9QSwMEFAAAAAgAh07iQOGA044yAgAAYQQAAA4AAABkcnMvZTJvRG9jLnhtbK1UzY7TMBC+I/EO&#10;lu80aStWVdV0VbYqQqrYlQri7DpOE8l/st0m5QHgDThx4c5z9Tn2c366aOGwBy7O2DP+xt83M1nc&#10;NkqSk3C+Mjqj41FKidDc5JU+ZPTzp82bGSU+MJ0zabTI6Fl4ert8/WpR27mYmNLIXDgCEO3ntc1o&#10;GYKdJ4nnpVDMj4wVGs7COMUCtu6Q5I7VQFcymaTpTVIbl1tnuPAep+vOSXtE9xJAUxQVF2vDj0ro&#10;0KE6IVkAJV9W1tNl+9qiEDzcF4UXgciMgmloVySBvY9rslyw+cExW1a8fwJ7yROecVKs0kh6hVqz&#10;wMjRVX9BqYo7400RRtyopCPSKgIW4/SZNruSWdFygdTeXkX3/w+Wfzw9OFLlGZ1SoplCwS8/vl9+&#10;/r78+kamUZ7a+jmidhZxoXlnGjTNcO5xGFk3hVPxCz4Efoh7voormkB4vDSbzGYpXBy+YQP85Om6&#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ba1mrMm8BNir3hdLG61o5r1zj93/Ltkrp0q+WD6m9QSwECFAAU&#10;AAAACACHTuJAfublIPcAAADhAQAAEwAAAAAAAAABACAAAACbBAAAW0NvbnRlbnRfVHlwZXNdLnht&#10;bFBLAQIUAAoAAAAAAIdO4kAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAEAAAAH0DAABfcmVscy9QSwEC&#10;FAAUAAAACACHTuJAihRmPNEAAACUAQAACwAAAAAAAAABACAAAAChAwAAX3JlbHMvLnJlbHNQSwEC&#10;FAAKAAAAAACHTuJAAAAAAAAAAAAAAAAABAAAAAAAAAAAABAAAAAAAAAAZHJzL1BLAQIUABQAAAAI&#10;AIdO4kCzSVju0AAAAAUBAAAPAAAAAAAAAAEAIAAAACIAAABkcnMvZG93bnJldi54bWxQSwECFAAU&#10;AAAACACHTuJA4YDTjjICAABhBAAADgAAAAAAAAABACAAAAAfAQAAZHJzL2Uyb0RvYy54bWxQSwUG&#10;AAAAAAYABgBZAQAAwwUAAAAA&#10;">
                    <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>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值