选中图片右击 选择设置图片格式
例如word模板
maven依赖
<!-- java 读取word文件里面的加颜色的字体 转pdf 使用 -->
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
<!--poi 的相关组件 -->
<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>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<!-- 不添加此包会提示错误 : org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<!-- 用于 word 转pdf -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>
<!-- pdf 添加水印 对PDF文件的操作 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>
<!-- PDF文件 字体 防止中文乱码 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--基于 poi实现word数据的替换 -->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.1</version>
</dependency>
<!--
如果下载jar失败,说明下载jar失败,需要以下的maven依赖
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
-->
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<!-- <dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.25</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.6</version>
</dependency>-->
<!-- 打包使用,需要配置 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 打包时会将本地jar一起打包 -->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
<!--基于 poi实现word数据的替换 -->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.1</version>
</dependency>
<!-- 不添加此包会提示错误 : org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
<!-- 用于 word 转pdf -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>
读取
代码
@GetMapping("exportPDF")
public AjaxResult exportPDF(HttpServletResponse response) throws Exception {
LoginUser loginUser = getLoginUser();
OpenCompanyInfo companyInfo = openCompanyInfoService.selectEnterpriseInfo(loginUser.getId());
if (companyInfo != null) {
Map<String, Object> data = new HashMap<>();
data.put("legalDeputy", companyInfo.getLegalDeputy());
data.put("companyName", companyInfo.getCompanyName());
data.put("companyCode", companyInfo.getCompanyCode());
if (StringUtils.isNoneBlank(companyInfo.getBusinessLicenseUrl())) {
InputStream businessLicenseUrl = minioClientUtils.returnInputStream(null, companyInfo.getBusinessLicenseUrl());
data.put("businessLicenseUrl", Pictures.ofStream(businessLicenseUrl, PictureType.JPEG).size(120, 120).create());
}
if (StringUtils.isNoneBlank(companyInfo.getCompanyLogoUr())) {
InputStream companyLogoUrl = minioClientUtils.returnInputStream(null, companyInfo.getCompanyLogoUr());
data.put("companyLogoUrl", Pictures.ofStream(companyLogoUrl, PictureType.JPEG).size(120, 120).create());
}
InputStream inputStreamFile = ResourceReader.class.getResourceAsStream("/templates/模版.docx");
XWPFTemplate template = XWPFTemplate.compile(inputStreamFile).render(data);
// XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\11949\\Desktop\\模版.docx").render(data);
byte[] array = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
template.writeAndClose(baos);
array = baos.toByteArray();
baos.close();
template.close();
InputStream inputStream = new ByteArrayInputStream(array);
ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());
inputStream.close();
xwpfDocument.close();
response.setContentType("application/octet-stream");
String format = DateUtil.format(new Date(), "yyyy-MM-dd");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("认证授权函" + format + ".pdf", "UTF-8"));
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
pdfBaos.writeTo(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
}
return null;
}