package com.base.util; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import org.apache.log4j.Logger; import com.amarsoft.are.ARE; public class WordToPDFUtil { // 将word格式的文件转换为pdf格式 public synchronized static Boolean Word2Pdf(String srcPath, String desPath) throws Exception { // 源文件目录 File inputFile = new File(srcPath); if (!inputFile.exists()) { ARE.getLog().error("源文件不存在!"); return false; } // 输出文件目录 File outputFile = new File(desPath); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().exists(); } Process process = null; //获取当前时间的毫秒数 long start= System.currentTimeMillis(); ARE.getLog().info("============转换pdf开始============"); try{ String cmd = "C:\\Program Files\\LibreOffice 5\\program\\soffice --headless -invisible --convert-to pdf "+srcPath+" --outdir "+desPath; ARE.getLog().info("python:"+cmd); process = Runtime.getRuntime().exec(cmd); String cmdMsg = ""; BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); while((cmdMsg=bufferedReader.readLine()) !=null){ ARE.getLog().info(cmdMsg); } process.waitFor(); }catch(Exception e){ e.printStackTrace(); return false; }finally{ if(null!=process){ process.destroy(); } long end = System.currentTimeMillis(); //输出转换pdf所需毫秒数 ARE.getLog().info(end-start+"毫秒"); ARE.getLog().info("============转换pdf结束============"); } return true; } public static void main(String[] args) throws Exception { WordToPDFUtil.Word2Pdf("D:\\project\\clms_app_manage\\WebContent\\fileTemplate\\光大金融租赁公司融资租赁合同.docx", "d:\\woqu"); } }