✍ 个人主页—— 默语 的博客👦🏻
🍩惟余辈才疏学浅,临摹之作或有不妥之处,还请读者海涵指正。☕🍭
《MYSQL从入门到精通》数据库是开发者必会基础之一~
🪁 吾期望此文有资助于尔,即使粗浅难及深广,亦备添少许微薄之助。苟未尽善尽美,敬请批评指正,以资改进。!💻⌨
@Override
public String printFile(Long id) throws IOException {
try {
FileDypeVo fileDypeVos = fileMapper.selectFileId(id);
String path = fileDypeVos.getFilePath();
String name = fileDypeVos.getFileName();
File file = new File(path + name);
// 获取后缀
String str = name.substring(name.lastIndexOf(".") + 1);
// 获取前面的名字
String str1 = name.substring(0, name.lastIndexOf(".") - 1);
// 拼装为pdf
String str2 = str1 + ".pdf";
LOGGER.info("str==》" + str);
LOGGER.info("str1===>" + str1);
LOGGER.info("str2===>" + str2);
LOGGER.info("file===>" + file);
BASE64Encoder encoder = new BASE64Encoder();
if (str.equals("pdf")) {
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
System.out.println("pdf预览成功");
return encoder.encode(buffer);
}
if (str.equals("docx") || str.equals("doc")) {
String sourcePath = path + name;
String targetPath = path + str2;
String files01 = str2;
LOGGER.info("sourcePath==>" + sourcePath + "==>targetPath==>" + targetPath + "==files" + files01);
File fileYan = new File(targetPath);
// 查看是否生成过pdf了查看文件是否是存在 不存在则进入 存在不执行
if (!fileYan.exists()) {
ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
}
FileInputStream inputFile = new FileInputStream(fileYan);
byte[] buffer = new byte[(int) fileYan.length()];
inputFile.read(buffer);
inputFile.close();
System.out.println("pdf预览成功");
return encoder.encode(buffer);
} else {
throw new ServiceException("该文档格式无法预览");
}
} catch (Exception e) {
throw new ServiceException("预览失败" + e.getMessage());
// TODO: handle exception
}
}
核心代码
ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
/**
* 通过documents4j 实现word转pdf
*
* @param sourcePath 源文件地址 如 /root/example.doc
* @param targetPath 目标文件地址 如 /root/example.pdf
*/
public static void documents4jWordToPdf(String sourcePath, String targetPath) {
File inputWord = new File(sourcePath);
File outputFile = new File(targetPath);
try {
InputStream docxInputStream = new FileInputStream(inputWord);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outputStream)
.as(DocumentType.PDF).execute();
outputStream.close();
} catch (Exception e) {
LOGGER.error("[documents4J] word转pdf失败:{}"+ e.toString());
// log.error("[documents4J] word转pdf失败:{}", e.toString());
}
}
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
compile 我这边设置以他为空,各位你们可自行查看;manve库和我们这边不一样
compile group: 'com.documents4j', name: 'documents4j-local', version: '1.0.3'
// https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer-msoffice-word
compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-word', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-api', version: '1.0.3'
// https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer
compile group: 'com.documents4j', name: 'documents4j-transformer', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-transformer-api', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-base', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-util-all', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-util-conversion', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-util-transformer-process', version: '1.0.3'
compile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.8'
https://cloud.tencent.com/developer/article/2467496@TOC
@Override
public String printFile(Long id) throws IOException {
try {
FileDypeVo fileDypeVos = fileMapper.selectFileId(id);
String path = fileDypeVos.getFilePath();
String name = fileDypeVos.getFileName();
File file = new File(path + name);
// 获取后缀
String str = name.substring(name.lastIndexOf(".") + 1);
// 获取前面的名字
String str1 = name.substring(0, name.lastIndexOf(".") - 1);
// 拼装为pdf
String str2 = str1 + ".pdf";
LOGGER.info("str==》" + str);
LOGGER.info("str1===>" + str1);
LOGGER.info("str2===>" + str2);
LOGGER.info("file===>" + file);
BASE64Encoder encoder = new BASE64Encoder();
if (str.equals("pdf")) {
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
System.out.println("pdf预览成功");
return encoder.encode(buffer);
}
if (str.equals("docx") || str.equals("doc")) {
String sourcePath = path + name;
String targetPath = path + str2;
String files01 = str2;
LOGGER.info("sourcePath==>" + sourcePath + "==>targetPath==>" + targetPath + "==files" + files01);
File fileYan = new File(targetPath);
// 查看是否生成过pdf了查看文件是否是存在 不存在则进入 存在不执行
if (!fileYan.exists()) {
ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
}
FileInputStream inputFile = new FileInputStream(fileYan);
byte[] buffer = new byte[(int) fileYan.length()];
inputFile.read(buffer);
inputFile.close();
System.out.println("pdf预览成功");
return encoder.encode(buffer);
} else {
throw new ServiceException("该文档格式无法预览");
}
} catch (Exception e) {
throw new ServiceException("预览失败" + e.getMessage());
// TODO: handle exception
}
}
核心代码
ExcelPdToWord.documents4jWordToPdf(sourcePath, targetPath);
/**
* 通过documents4j 实现word转pdf
*
* @param sourcePath 源文件地址 如 /root/example.doc
* @param targetPath 目标文件地址 如 /root/example.pdf
*/
public static void documents4jWordToPdf(String sourcePath, String targetPath) {
File inputWord = new File(sourcePath);
File outputFile = new File(targetPath);
try {
InputStream docxInputStream = new FileInputStream(inputWord);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outputStream)
.as(DocumentType.PDF).execute();
outputStream.close();
} catch (Exception e) {
LOGGER.error("[documents4J] word转pdf失败:{}"+ e.toString());
// log.error("[documents4J] word转pdf失败:{}", e.toString());
}
}
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
compile 我这边设置以他为空,各位你们可自行查看;manve库和我们这边不一样
compile group: 'com.documents4j', name: 'documents4j-local', version: '1.0.3'
// https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer-msoffice-word
compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-word', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-api', version: '1.0.3'
// https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer
compile group: 'com.documents4j', name: 'documents4j-transformer', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-transformer-api', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-transformer-msoffice-base', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-util-all', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-util-conversion', version: '1.0.3'
compile group: 'com.documents4j', name: 'documents4j-util-transformer-process', version: '1.0.3'
compile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.8'
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有