【无标题】

使用poi-tl动态生成word模板填充数据,word转pdf

1.相关依赖

org.apache.poi poi-ooxml 4.1.2 org.apache.poi poi-excelant 4.1.2 org.apache.poi poi-scratchpad 4.1.2 org.apache.poi poi-ooxml-schemas 4.1.2 org.apache.poi poi 4.1.2 commons-codec commons-codec com.deepoove poi-tl 1.10.1 com.aspose aspose-words 15.12.0-jdk16 system ${pom.basedir}/lib/aspose-words-15.12.0-jdk16.jar aspose-words-15.12.0-jdk16.jar

2.引入外部jar时需配置

org.springframework.boot spring-boot-maven-plugin true

3.相关官网

http://deepoove.com/poi-tl/

4.其他问题

目前,在liunx环境会出现中文乱码,需要liunx安装中文字体,代码上要指定字体目录。

if (SystemUtils.IS_OS_LINUX) {
FontSettings.setFontsFolder(“/usr/share/fonts/cjkuni-uming”, true);
}

5.代码片段


package com.eedu.diagnosis.inclass.test.persist.util;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.DocumentRenderData;
import com.deepoove.poi.data.Documents;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.SystemUtils;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ObjectUtils;

import javax.validation.constraints.NotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**

  • Author: songpenghao@e-eduspace.com

  • Date: 2023/3/27 14:05

  • Description: word pdf 相关工具
    */
    @Slf4j
    public class WordPdfUtil {

    //定义标签
    public static final String INDEX = “index”;//序号
    public static final String TYPE_NAME = “typeName”; //题型名称
    public static final String QUESTION_STEM = “questionStem”; //题干
    public static final String OPTION_KEY = “optionKey”;//选项
    public static final String OPTION_VALUE = “optionValue”;//选项内容
    //定义符号
    public static final String BRACKET_START = “{{”;
    public static final String BRACKET_END = “}}”;
    public static final String AT = “@”;
    public static final String POINT = “、”;
    public static final String V_LINE = “|”;
    public static final String N_LINE = “\n”;
    public static final String B_LINE = “\t”;
    public static final String LEFT_BRACE = “(”;
    public static final String RIGHT_BRACE = “)”;
    public static final String LEFT_BRACE_EN = “(”;
    public static final String RIGHT_BRACE_EN = “)”;

    /**

    • 创建word

    • @param builder

    • @param data

    • @return
      */
      public static byte[] createWord(Documents.DocumentBuilder builder, Map<String, Object> data) throws IOException {
      DocumentRenderData document = builder.create();
      XWPFTemplate template = XWPFTemplate.create(document);
      //设置段落垂直居中
      List paragraphs = template.getXWPFDocument().getParagraphs();
      for (XWPFParagraph paragraph : paragraphs) {
      paragraph.setVerticalAlignment(TextAlignment.CENTER);
      }

      //读写文件流
      byte[] wordBytes = null;
      ByteArrayOutputStream modelOutStream = null;
      ByteArrayOutputStream wordOutStream = null;
      InputStream inputStream = null;
      try {
      //生成word
      modelOutStream = new ByteArrayOutputStream();
      wordOutStream = new ByteArrayOutputStream();
      template.writeAndClose(modelOutStream);
      inputStream = new ByteArrayInputStream(modelOutStream.toByteArray());
      XWPFTemplate.compile(inputStream).render(data).getXWPFDocument().write(wordOutStream);
      wordBytes = wordOutStream.toByteArray();
      //wordBytes = modelOutStream.toByteArray();
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      if (wordOutStream != null) {
      wordOutStream.close();
      }
      if (modelOutStream != null) {
      modelOutStream.close();
      }
      if (inputStream != null) {
      inputStream.close();
      }
      }
      return wordBytes;
      }

    /**

    • 创建pdf
    • @param bytes
    • @return
    • @throws IOException
      /
      public static byte[] createPdf(byte[] bytes) throws IOException {
      byte[] pdfBytes = null;
      ByteArrayOutputStream pdfOutStream = null;
      InputStream pdfInputStream = null;
      try {
      //生成pdf
      pdfInputStream = new ByteArrayInputStream(bytes);
      pdfOutStream = new ByteArrayOutputStream();
      // 获取生成pdf的凭证,要不然会有水印
      getVoucher();
      /
      //解决中文乱码
      if (SystemUtils.IS_OS_LINUX) {
      FontSettings.setFontsFolder(“/usr/share/fonts/cjkuni-uming”, true);
      }*/
      // 要转换的word文件
      Document doc = new Document(pdfInputStream);
      doc.save(pdfOutStream, SaveFormat.PDF);
      //输出流转化成byte
      pdfBytes = pdfOutStream.toByteArray();
      } catch (Exception e) {
      e.printStackTrace();
      } finally {
      if (pdfOutStream != null) {
      pdfOutStream.close();
      }
      if (pdfInputStream != null) {
      pdfInputStream.close();
      }
      }
      return pdfBytes;
      }

    /**

    • 样式
    • @param text
    • @param color
    • @param fontSize
    • @return
      */
      public static TextRenderData buildTextRenderData(@NotNull String text, String color, Integer fontSize, boolean bold) {
      TextRenderData textRenderData = new TextRenderData();
      //设置样式
      Style style = new Style();
      if (!ObjectUtils.isEmpty(color)) {
      style.setColor(color);
      }
      if (!ObjectUtils.isEmpty(fontSize)) {
      style.setFontSize(fontSize);
      }
      if (bold) {
      style.setBold(bold);
      }
      textRenderData.setStyle(style);
      textRenderData.setText(text);
      return textRenderData;
      }

    /**

    • 获取生成pdf的凭证,要不然会有水印
      */
      private static void getVoucher() {
      try {
      InputStream is = new ClassPathResource(“license.xml”).getInputStream();
      License aposeLic = new License();
      aposeLic.setLicense(is);
      } catch (Exception e) {
      log.info(“获取生成pdf凭证异常。{}”, e.getMessage());
      }
      }
      }


- 关于 **Flowchart流程图** 语法,参考 [这儿][4].

## 导出与导入

###  导出
如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 **文章导出** ,生成一个.md文件或者.html文件进行本地保存。

### 导入
如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。

 [1]: http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
 [2]: https://mermaidjs.github.io/
 [3]: https://mermaidjs.github.io/
 [4]: http://adrai.github.io/flowchart.js/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值