通过FreeMarker生成word文档docx转pdf(二)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

通过FreeMarker⽣成word⽂档docx转pdf(⼆)docx属于zip的⼀种,这⾥只需要操作word/document.xml中的数据
1、新建detection.docx⽂件,⽤winrar打开⽂件,取出word/document.xml,进⾏freeMarker语句替换。

替换xml⽂件w:body中传⼊值为freeMarker语句,更改为detection.xml,
<w:body>
<w:p w14:paraId="59211CB6" w14:textId="2234D275" w:rsidR="00C76028" w:rsidRDefault="00C76028" w:rsidP="00C76028">
<w:pPr>
<w:pStyle w:val="1"/>
<w:keepNext w:val="0"/>
<w:keepLines w:val="0"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>四性检测报告(${link})</w:t>
</w:r>
</w:p>
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="a3"/>
<w:tblW w:w="8532" w:type="dxa"/>
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1129"/>
<w:gridCol w:w="1756"/>
<w:gridCol w:w="1079"/>
<w:gridCol w:w="154"/>
<w:gridCol w:w="1264"/>
<w:gridCol w:w="163"/>
<w:gridCol w:w="825"/>
<w:gridCol w:w="570"/>
<w:gridCol w:w="599"/>
<w:gridCol w:w="993"/>
</w:tblGrid>
<w:tr w:rsidR="00C76028" w14:paraId="544E66C0" w14:textId="77777777" w:rsidTr="002C287D">
<w:trPr>
<w:trHeight w:val="549"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1129" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="66574DC1" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>检验编号</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1756" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="3E4EC493" w14:textId="2475519D" w:rsidR="00C76028" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidRPr="00C76028">
<w:t>${inNumber}</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1079" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="6ED7FE9C" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:t>检验时间</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1581" w:type="dxa"/>
<w:gridSpan w:val="3"/>
</w:tcPr>
<w:p w14:paraId="6F4FA440" w14:textId="055BCC6B" w:rsidR="00C76028" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidRPr="00C76028">
<w:t>${inTime}</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1395" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="5D5D1592" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>检验⼈</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1592" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="571178A6" w14:textId="4099E11C" w:rsidR="00C76028" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidRPr="00C76028">
<w:t>${createName}</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:tc>
</w:tr>
<w:tr w:rsidR="00C76028" w14:paraId="110D174C" w14:textId="77777777" w:rsidTr="002C287D">
<w:trPr>
<w:trHeight w:val="487"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1129" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="5EC8A38C" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>检验总数</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1756" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="52371CDB" w14:textId="43C8B1DA" w:rsidR="00C76028" w:rsidRPr="00975BA5" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidRPr="00C76028">
<w:t>${inTotal}</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1079" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="275AE616" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
<w:bCs/>
</w:rPr>
<w:t>通过数</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1581" w:type="dxa"/>
<w:gridSpan w:val="3"/>
</w:tcPr>
<w:p w14:paraId="65399C60" w14:textId="1BC347B2" w:rsidR="00C76028" w:rsidRPr="00975BA5" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidRPr="00C76028">
<w:t>${passTotal}</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1395" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="6E310C87" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>未通过数</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1592" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="6ABA8855" w14:textId="18CEF9D0" w:rsidR="00C76028" w:rsidRPr="00975BA5" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r w:rsidRPr="00C76028">
<w:t>${nopassTotal}</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
</w:p>
</w:tc>
</w:tr>
<w:tr w:rsidR="00C76028" w14:paraId="3E960F7B" w14:textId="77777777" w:rsidTr="00C76028">
<w:trPr>
<w:trHeight w:val="706"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="8532" w:type="dxa"/>
<w:gridSpan w:val="10"/>
</w:tcPr>
<w:p w14:paraId="3E438019" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:pStyle w:val="4"/>
<w:keepNext w:val="0"/>
<w:keepLines w:val="0"/>
<w:spacing w:line="377" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>检测概述</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr w:rsidR="00C76028" w14:paraId="2B20A3D5" w14:textId="77777777" w:rsidTr="00C76028">
<w:trPr>
<w:trHeight w:val="499"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="4118" w:type="dxa"/>
<w:gridSpan w:val="4"/>
</w:tcPr>
<w:p w14:paraId="1DDCB7B4" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>档号</w:t>
</w:r>
<w:p w14:paraId="37803BF3" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>真实性</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="988" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="4D889EA9" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00AF1AEA">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>完整性</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1169" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="0D3B9AE1" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00AF1AEA" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00975BA5">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>可⽤性</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="993" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="42B16947" w14:textId="77777777" w:rsidR="00C76028" w:rsidRPr="00975BA5" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00975BA5">
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>安全性</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<#list summary as work>
<w:tr w:rsidR="00C76028" w14:paraId="2D9C8C8A" w14:textId="77777777" w:rsidTr="00C76028">
<w:trPr>
<w:trHeight w:val="499"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="4118" w:type="dxa"/>
<w:gridSpan w:val="4"/>
</w:tcPr>
<w:p w14:paraId="1E39F075" w14:textId="0559CDD4" w:rsidR="00C76028" w:rsidRPr="00A15B12" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="00C76028">
<w:t>${work.DH}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1264" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="2FCA7652" w14:textId="19867DC6" w:rsidR="00C76028" w:rsidRPr="00A15B12" w:rsidRDefault="00C76028" w:rsidP="005E6CB1"> <w:pPr>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="988" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="0529FC03" w14:textId="58C8F1D1" w:rsidR="00C76028" w:rsidRPr="00A15B12" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="00C76028">
<w:t>${work.integrity}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1169" w:type="dxa"/>
<w:gridSpan w:val="2"/>
</w:tcPr>
<w:p w14:paraId="36B5B8DC" w14:textId="416A9A40" w:rsidR="00C76028" w:rsidRPr="00A15B12" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="00C76028">
<w:t>${work.availability}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="993" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="7481AB7B" w14:textId="17CDE97C" w:rsidR="00C76028" w:rsidRPr="00A15B12" w:rsidRDefault="00C76028" w:rsidP="005E6CB1">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r w:rsidRPr="00C76028">
<w:t>${work.security}</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</#list>
</w:tbl>
<w:p w14:paraId="419AD697" w14:textId="77777777" w:rsidR="00962D4E" w:rsidRDefault="00962D4E" w:rsidP="00C76028"/>
<w:sectPr w:rsidR="00962D4E">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="851" w:footer="992" w:gutter="0"/>
<w:cols w:space="425"/>
<w:docGrid w:type="lines" w:linePitch="312"/>
</w:sectPr>
</w:body>
yml配置⽂件
testReport:
# 检测报告配置⽂件地址
templatePath: /home/dagl/work/template
# 模板docx⽂件地址
detectionFilePath: /home/dagl/work/template/detection.docx
# 检测报告概述配置⽂件名称
detectionFileName: detection.xml
# ⽣成报告地址
generatePath: /home/dagl/work
# 字体⽂件配置
windowsFont: C:\\WINDOWS\\Fonts\\SIMYOU.TTF
linuxFont: /usr/share/fonts/chinese/simheittf.ttf
pom⽂的maven
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
@Value("${testReport.templatePath}")
private String templatePath;
/*检测报告概述模板docx⽂件地址及名称*/
@Value("${testReport.detectionFilePath}")
private String detectionFilePath;
/*检测报告概述配置⽂件名称*/
@Value("${testReport.detectionFileName}")
private String detectionFileName;
/*⽣成⽂件地址*/
@Value("${testReport.generatePath}")
private String generatePath;
/*windows字体地址*/
@Value("${testReport.windowsFont}")
private String windowsFont;
/*linux字体地址*/
@Value("${testReport.linuxFont}")
private String linuxFont;
@Autowired
private DetectionMapper detectionMapper;
/**
* 检擦报告概述
* @date 18:33 2021/12/24
* @param monitoringZongId 检测报告总表
* @return⽂件地址
**/
@Override
public String testReportSummary(String monitoringZongId){
Map<String,Object> map = new HashMap<>();
List<Map<String,String>> mapList = detectionMapper.selectMonitoringZong(monitoringZongId);
/*判断⽂件是否存在,存在则直接返回地址*/
String inspectionNumber = mapList.get(0).get("inspectionNumber");
String generatePathNamePdf = generatePath + File.separator + inspectionNumber + File.separator + inspectionNumber + ".pdf";
File file = new File(generatePathNamePdf);
if(file.exists()){
return generatePathNamePdf;
}
List<Map<String,String>> mapListSummary = detectionMapper.selectMonitoringSummary(monitoringZongId, null);
map.put("link", mapList.get(0).get("link"));
map.put("inNumber", inspectionNumber);
map.put("inTime", mapList.get(0).get("inspectionTime"));
map.put("createName", mapList.get(0).get("createName"));
map.put("inTotal", mapList.get(0).get("inspectionTotal"));
map.put("passTotal", mapList.get(0).get("passTotal"));
map.put("nopassTotal", mapList.get(0).get("nopassTotal"));
List<Map<String,String>> listSummary = new ArrayList<>();
for (Map<String,String> map2 : mapListSummary){
Map<String,String> map1 = new HashMap<>();
map1.put("DH", map2.get("dh"));
map1.put("authenticity", map2.get("authenticity"));
map1.put("security", map2.get("security"));
map1.put("integrity", map2.get("integrity"));
map1.put("availability", map2.get("availability"));
listSummary.add(map1);
}
map.put("summary", listSummary);
try {
/*⽣成doc⽂件⽅法*/
/*DocumentHandler documentHandler = new DocumentHandler();
generatePathNamePdf = documentHandler.createDoc(map, detectionFilePath, detectionFileName, generatePath + File.separator + mapList.get(0).get("inspectionNumber"), mapList.get(0).get("inspectionNumber")); */
/*输出⽂档路径是否存在,不存在创建⽂件夹*/
File filePath = new File(generatePath + File.separator + mapList.get(0).get("inspectionNumber"));
if (!filePath.exists()) {
filePath.mkdir();
}
/*⽣成docx⽂件xml⽂件*/
String xmlTemp = generatePath + File.separator + mapList.get(0).get("inspectionNumber") + File.separator + mapList.get(0).get("inspectionNumber") + ".xml";
/*⽣成docx⽂件*/
String toFilePath = generatePath + File.separator + mapList.get(0).get("inspectionNumber") + File.separator + mapList.get(0).get("inspectionNumber") + ".docx";
Writer out = new FileWriter(new File(xmlTemp));
XmlToExcel.process(detectionFileName, map, out, templatePath);
XmlToDocx xtd = new XmlToDocx();
xtd.outDocx(new File(xmlTemp), detectionFilePath, toFilePath, generatePathNamePdf, windowsFont, linuxFont);
} catch (Exception e) {
e.printStackTrace();
File pathFile = new File(generatePathNamePdf);
if (pathFile.exists()) {
pathFile.delete();
}
return null;
}
return generatePathNamePdf;
}
/**
* ⽇期格式化
*
* @return
* @date 23:32 2021/12/12
**/
public String dateTool() {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return simpleDateFormat.format(date);
}
}
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;
import freemarker.template.TemplateException;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import ermodel.XWPFDocument;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
* 临时模板xml⽣成docx⽂件
* @date 1:17 2021/12/27
* @version 1.0
**/
public class XmlToDocx {
/**
*
* @param documentFile 动态⽣成数据的docunment.xml⽂件
* @param docxTemplate docx的模板
* @param toFilePath 需要导出的⽂件路径
* @param generatePathNamePdf 导出pdf⽂件地址
* @throws ZipException
* @throws IOException
*/
public void outDocx(File documentFile, String docxTemplate, String toFilePath, String generatePathNamePdf,
String windowsFont, String linuxFont) throws ZipException, IOException {
try {
File docxFile = new File(docxTemplate);
ZipFile zipFile = new ZipFile(docxFile);
Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(toFilePath));
int len = -1;
byte[] buffer = new byte[1024];
while (zipEntrys.hasMoreElements()) {
ZipEntry next = zipEntrys.nextElement();
InputStream is = zipFile.getInputStream(next);
// 把输⼊流的⽂件传到输出流中如果是word/document.xml由我们输⼊
zipout.putNextEntry(new ZipEntry(next.toString()));
if ("word/document.xml".equals(next.toString())) {
InputStream in = new FileInputStream(documentFile);
while ((len = in.read(buffer)) != -1) {
zipout.write(buffer, 0, len);
}
in.close();
} else {
while ((len = is.read(buffer)) != -1) {
zipout.write(buffer, 0, len);
}
is.close();
}
}
zipout.close();
} catch (Exception e) {
e.printStackTrace();
}
/*docx⽣成pdf*/
XWPFDocument document;
try (InputStream doc = Files.newInputStream(Paths.get(toFilePath))) {
document = new XWPFDocument(doc);
}
PdfOptions options = PdfOptions.create();
/*docx⽣成pdf设置字体,windows⽣成pdf时可以不需要以下内容,linux需要进⾏字体设置*/
options.fontProvider(new IFontProvider() {
@Override
public Font getFont(String familyName, String encoding, float size, int style, java.awt.Color color) {
try {
/*iTextAsian.jar*/
/*BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);*/ String prefixFont = null;
String os = System.getProperties().getProperty("");
if (os.startsWith("win") || os.startsWith("Win")) {
/*windows字体*/
prefixFont = windowsFont;
} else {
/*linux字体*/
prefixFont = linuxFont;
}
BaseFont bfChinese = BaseFont.createFont(prefixFont, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, size, style, color);
if (familyName != null)
fontChinese.setFamily(familyName);
return fontChinese;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
});
/*⽣成pdf*/
try (OutputStream out = Files.newOutputStream(Paths.get(generatePathNamePdf))) {
PdfConverter.getInstance().convert(document, out, options);
out.close();
}
}
}
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import ermodel.XWPFDocument;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
/**
* @ClassName XmlToExcel
* freemker⽣成模板零时⽂件xml
* @date 1:10 2021/12/27
* @version 1.0
**/
public class XmlToExcel {
private static XmlToExcel tplm = null;
private Configuration configuration = null;
private XmlToExcel(String templatePath) {
configuration = new Configuration(new Version("2.3.0"));
try {
// 注册tmlplate的load路径。

相关文档
最新文档