Word模板替换,并转PDF格式输出,Linux服务器乱码问题解决

Poi-tl参考文档地址:http://deepoove.com/poi-tl/1.8.x/#hack-loop-table

1. 依赖引入:

  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
  </dependency>

  <dependency>
      <groupId>com.aspose</groupId>
      <artifactId>aspose-words</artifactId>
      <version>18.2</version>
      <classifier>jdk16</classifier>
  </dependency>

  <dependency>
      <groupId>com.deepoove</groupId>
      <artifactId>poi-tl</artifactId>
      <version>1.8.2</version>
  </dependency>

<!-- 通过Maven库拿不到,就加下面的配置 -->
<repositories>
     <repository>
         <id>AsposeJavaAPI</id>
         <name>Aspose Java API</name>
         <url>https://repository.aspose.com/repo/</url>
     </repository>
 </repositories>
 <pluginRepositories>
     <pluginRepository>
         <id>AsposeJavaAPI</id>
         <url>https://repository.aspose.com/repo/</url>
     </pluginRepository>
 </pluginRepositories>

2. word模板配置:

        绿色部分是直接渲染的,对应map中的key-value,蓝色部分是绑定collections对象,进行遍历循环集合数据
在这里插入图片描述

3. 示例demo:

public static void main(String[] args) throws Exception {
       public static void main(String[] args) throws Exception {
        ByteArrayOutputStream docOutput = new ByteArrayOutputStream();

        Map<String, Object> map = new HashMap<>(3);
        map.put("zfkp", "AAAA");
        map.put("kpjg", "123");
        map.put("kpr", "李四");

        List<Map> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>(3);
        map1.put("number", 1);
        map1.put("kprlist", "xxxa");
        map1.put("fs", 17);
        Map<String, Object> map2 = new HashMap<>(3);
        map2.put("number", 2);
        map2.put("kprlist", "xxxa3aa");
        map2.put("fs", 10);
        list.add(map1);
        list.add(map2);
        //列表集合数据
        map.put("collections", list);

        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder()
                //绑定集合数据
                .bind("collections", policy).build();

        System.out.println(map);
        //加载配置的word模板
        XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\DELL\\Desktop\\kpwtlb.docx", config);

        template.render(map);
        //写入word,swapStream可输出word文档
        template.write(docOutput);
        ByteArrayInputStream swapStream = new ByteArrayInputStream(docOutput.toByteArray());

        //word转PDF
        Document doc = new Document(swapStream);
        ByteArrayOutputStream pdfOutput = new ByteArrayOutputStream();
        doc.save(pdfOutput, SaveFormat.PDF);

        ByteArrayInputStream pdfinput = new ByteArrayInputStream(pdfOutput.toByteArray());
        FileUtil.inputStreamToFile(pdfinput, "C:\\Users\\DELL\\Desktop\\", "123.pdf");
    }

4 . 效果图

        转换的pdf有水印,去水印很方便, 加载License(要买)即可 封装一个工具类
https://blog.csdn.net/weixin_42827159/article/details/105031663
在这里插入图片描述

5. 本地测试没问题,上Linux服务乱码,出现小方框

参考博客:https://blog.csdn.net/qq_42055933/article/details/128285226

1、将windows字体压缩上传到Linux,并安装进行缓存

//查看linux有的字体
fc-list
//linux中的中文字体
fc-list :lang=zh
//进入c:\windows\fonts,压缩字体包,上传至Linux /usr/share/fonts/路径,自建一个文件夹 win
//解压上传的windows字体至win中
//给以下格式字体文件赋权限
chmod 755 *.ttf  chmod 755 *.ttc  chmod 755 *.TTF
//安装字体以及缓存字体
sudo mkfontscale
sudo mkfontdir 
sudo fc-cache -fv
//生效指令
source /etc/profile

2、在转换之前设置一下字体路径来源
        执行完以上步骤,加上以下代码,两个截图是两种不同的方法,选其一,打包上传重启基本就解决了,如果是docker启动,还需做一步。重构docker容器,将windows字体存放的路径,即以下代码指定的路径,做挂载,然后重启
        docker相关操作:https://blog.csdn.net/loney_wolf/article/details/128255199 构建容器时,除了挂载项目路径,再加 -v /usr/share/fonts/(window字体的文件夹) :/usr/share/fonts/ 将安装的windows字体路径挂在至docker容器中,重启即可

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中有多种库可以用来实现Word文档PDF的功能,其中比较常用的是iText和Apache POI。下面我会分别介绍一下这两个库的使用方法。 1. iText: iText是一个开源的Java库,可以用来创建和操作PDF文档。它提供了丰富的API,可以实现对PDF文档的各种操作,包括创建、编辑、合并、拆分、加密等。要将Word文档换为PDF,可以使用iText的功能来读取Word文档内容,并将其换为PDF格式。 首先,你需要在项目中引入iText的依赖。可以在Maven项目中的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> ``` 然后,你可以使用以下代码将Word文档换为PDF: ```java import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.FileInputStream; import java.io.FileOutputStream; public class WordToPdfConverter { public static void main(String[] args) { try { // 读取Word文档 FileInputStream fis = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(fis); // 创建PDF文档 Document pdfDocument = new Document(); PdfWriter.getInstance(pdfDocument, new FileOutputStream("output.pdf")); pdfDocument.open(); // 逐段读取Word文档内容,并写入PDF文档 for (XWPFParagraph paragraph : document.getParagraphs()) { String text = paragraph.getText(); pdfDocument.add(new Paragraph(text)); } // 关闭文档 pdfDocument.close(); document.close(); System.out.println("WordPDF成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先使用Apache POI库的XWPFDocument类来读取Word文档的内容,然后使用iText库的Document类和PdfWriter类来创建和写入PDF文档。最后,我们关闭文档并输出成功信息。 2. Apache POI: Apache POI是一个用于读写Microsoft Office格式文件的Java库。它提供了对Word、Excel和PowerPoint等文件格式的支持。要将Word文档换为PDF,可以使用Apache POI的功能来读取Word文档内容,并将其写入PDF格式。 首先,你需要在项目中引入Apache POI的依赖。可以在Maven项目中的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 然后,你可以使用以下代码将Word文档换为PDF: ```java import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.FileInputStream; import java.io.FileOutputStream; public class WordToPdfConverter { public static void main(String[] args) { try { // 读取Word文档 FileInputStream fis = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(fis); // 创建PDF选项 PdfOptions options = PdfOptions.create(); // 将Word文档换为PDF FileOutputStream fos = new FileOutputStream("output.pdf"); PdfConverter.getInstance().convert(document, fos, options); // 关闭流 fos.close(); document.close(); System.out.println("WordPDF成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中,我们使用Apache POI库的XWPFDocument类来读取Word文档的内容,然后使用Apache POI提供的PdfConverter类将其换为PDF格式。最后,我们关闭流并输出成功信息。 希望以上代码可以帮助到你。如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值