控制台
@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()); } }
POM
<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>
Gradle
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'
扩展其他方式 apose
https://yanwc.blog.csdn.net/article/details/128285226