word转pdf --(documents4j 方式)
documents4j,在本地使用很方便,但是部署到LINUX上面之后,抛出异常,
官方文档:documents4j是使用本地的MS Office应用做的文件格式转换,Linux没有对应的MS Office应用
依赖:
com.documents4j documents4j-local ${documents4j.version} com.documents4j documents4j-transformer-msoffice-word ${documents4j.version} e-iceblued spire.doc.free ${spire.version}
版本:
1.1.5 3.9.0
代码:
package com.infinitepossibilities.pdf; import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter; import com.documents4j.job.LocalConverter; import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.ImageType; import org.apache.commons.io.FileUtils; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; public class PdfUtil { /** * word转pdf * @param wordFilePath word文件路径 * @param pdfFilePath pdf文件路径 * @return 成功或失败 */ public static boolean docxToPdf(String wordFilePath, String pdfFilePath) { boolean result = false; File inputFile = new File(wordFilePath); File outputFile = new File(pdfFilePath); try { InputStream inputStream = new FileInputStream(inputFile); OutputStream outputStream = new FileOutputStream(outputFile); IConverter converter = LocalConverter.builder().build(); converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); outputStream.close(); // converter.shutDown(); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void main(String[] args) { docxToPdf("C:\\Users\\admin\\Desktop\\1.7.doc", "C:\\Users\\admin\\Desktop\\1.7.pdf"); } }