1. 下载libreoffice
2. 配置path路径,F:\Program Files\LibreOffice 5\program  之后cmd窗口执行soffice命令可以打开libreoffice软件
3. 引依赖

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
</dependency>

4. 代码
@RequestMapping("/openPDF")
    public void openPDF(HttpServletResponse response) {
        try {
            String targetPath = System.getProperty("java.io.tmpdir")+File.separator;
            String docxPath = "http://192.168.200.201:9093/M00/00/01/wKjIyVqQ34yAUO-uAAAtY6q46LU28.docx";
            wordConverterToPdf(docxPath, targetPath);
            String name = docxPath.substring(docxPath.lastIndexOf("/")+1);
            String[] nameArr = name.split("\\.");

            CommandExecute.findPdf(response,targetPath+nameArr[0]+".pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
// 执行转换的方法
public static boolean wordConverterToPdf(String docxPath,String targetPath) throws IOException {
        File file = new File(docxPath);
//        String path = file.getParent();
try {
            String osName = System.getProperty("os.name");
            String command = "";
            if (osName.contains("Windows")) {
                command = "soffice --convert-to pdf  -outdir " + targetPath + " " + docxPath;
            } else {
                command = "doc2pdf --output=" + targetPath + File.separator + file.getName().replaceAll(".(?i)docx", ".pdf") + " " + docxPath;
            }
            String result = CommandExecute.executeCommand(command);
            if (result.equals("") || result.contains("writer_pdf_Export")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return false;
    }
// CommandExecute类
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
 * linux或windows命令执行
 */
public class CommandExecute {
//    public static void main(String[] args) {
//        CommandExecute obj = new CommandExecute();
//        String domainName = "www.baidu.com";
//        //in mac oxs
//        String command = "ping " + domainName;
//        //in windows
//        //String command = "ping -n 3 " + domainName;
//        String output = obj.executeCommand(command);
//        System.out.println(output);
//    }
public static String executeCommand(String command) {
        StringBuffer output = new StringBuffer();
        Process p;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            inputStreamReader = new InputStreamReader(p.getInputStream(), "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(inputStreamReader);
        }
        System.out.println(output.toString());
        return output.toString();
}
public static void findPdf(HttpServletResponse response, String targetPath) throws IOException {
response.setContentType("application/pdf");
        response.setCharacterEncoding("utf-8");
        FileInputStream in = new FileInputStream(new File(targetPath));
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[512];
        while ((in.read(b)) != -1) {
            out.write(b);
        }
        out.flush();
        in.close();
        out.close();
    }
}

java中使用libreoffice将word转换成pdf格式相关推荐

  1. word转换成pdf java代码_java代码实现word转换成pdf

    [实例简介] va代码 word转pdf ,word批量转换成pdf,word单独转换成pdf,只需要调用WordToPDFUtil方法就可以实现批量或者单独转换 [实例截图] [核心代码] e6f6 ...

  2. 用Python批处理将WORD文件转换成PDF格式(工具:win32com模块)

    用Python批处理将WORD文件转换成PDF格式 一.问题分析 key words:批处理.WORD转换PDF.办公自动化 二.材料准备 三.代码实现 ☆其他问题:日常遇到问题,整理笔记不易,欢迎交 ...

  3. 如何批量将Word转换成PDF?这几种方法都可以实现批量转换

    怎么把Word文档批量转换成PDF格式呢?我们在日常的工作学习中,经常会使用Word文档来编辑文件,把文件编辑好后在发送的时候一般都会选择PDF格式来发送,,因为这样我们编辑好的格式才不会混乱.当我们 ...

  4. 迅捷word转换成pdf转换器最新版

    怎样把Word转换成PDF格式的好呢?随着PDF格式文件使用的范围越来越广,很多时候我们就需要把Word格式转换成PDF进行使用,很多朋友都会觉得手上都没有辅助软件而无法进行操作.也可能是没有找到正确 ...

  5. Word转换成PDF文件在线转换

    Word文档转换成PDF文件怎么转换?为了文档的安全性考虑,有时候需要将Word转换成PDF格式的.如何进行两者的的转换是成功的主要因素所在,小编也有过这样的经历个人使用后,感觉迅捷word转换成pd ...

  6. 如何把word转成pdf格式以及如何从指定页开始设置页码技巧

    Word文档我相信没几个人不认识,但word的使用技巧数不胜数,你确定你全部都掌握了吗?今天为大家分享两个Word使用过程中常见的难题!打包拿走,不谢! ·首先是Word转换成PDF格式的技巧 如果你 ...

  7. CAD看图软件怎么将CAD图纸转换成PDF格式

    我们在CAD制图的时候,常常会遇到CAD图纸转换的各 种问题.其中较常见的就有怎么将CAD图纸转换成PDF格式?若是我们使用CAD看图软件,在查看CAD图纸的时候就直接将CAD图纸转换成PDF格式,该 ...

  8. 将word文档转换成pdf格式【使用Aspose技术实现:亲测可用】

    提示:Java使用Aspose技术将word文件转换成pdf文件 文章目录 一.介绍 二.下载依赖并引入jar包 三.编写功能 一.介绍 Java语言使用Aspose技术将word转换成pdf文件的功 ...

  9. C# 使用Microsoft.Office.Interop将Excel、Word转换成PDF遇到的问题总结

    首先应用中引入Microsoft.Office.Interop.Excel.Microsoft.Office.Interop.Word两个dll,将嵌入式互操作类型设为False, WORD转换成PD ...

  10. 用什么方法可以将Word转换成PDF文档?

    我们有时编辑完Word文件需要打印或发送给他人查看,一般会先转换为PDF格式,这样在别的电脑上打开时就不会出现格式的变动,也不会出现因为版本而无法打开的问题,那么用什么方法可以将Word转换成PDF文 ...

最新文章

  1. SEO优化如何“搭乘”线上营销推广“列车”?
  2. ITRON系统使用方法
  3. 超图三维GIS开发概念学习
  4. php如何转换类型,PHP数据类型转换
  5. 【iCore3 双核心板_ uC/OS-III】例程七:信号量——任务同步
  6. POI的入门:单元格样式处理
  7. 0419 一些不错的UI作品,以后陆续更新
  8. 提升对前端的认知,不得不了解Web API的DOM和BOM
  9. 最新PC游戏下载链接
  10. Keras 报错:An operation has `None` for gradient.
  11. LM NTLM ophcrack RainBow table (转)
  12. 物联网云计算成本核算 小厂的出路在哪里
  13. 计算机考试考什么二级,计算机国家二级考试会考什么内容?怎么考?
  14. JavaScript 计数器
  15. 火热升级:360Safe VS 雅虎助手
  16. Lock接口和AQS原理与实现(Java并发编程的艺术整理)
  17. python pkl、npy数据转换为csv
  18. 物联网嵌入式系统开发应用软件公司怎么选择
  19. 计算机网络速度测试指令,怎么ping网速 ping命令简单测试网速方法【详解】
  20. 昇腾AI室外移动机器人原理与应用(二 初识室外移动机器人)

热门文章

  1. Codeforces Round 1299 简要题解
  2. ppt太大怎么压缩整个文件
  3. graphpad如何换柱状图与折线图能否混合一起_Graphpad Prism 绘制柱状图与散点图共存图...
  4. 百度小程序如何搭建对搜索更友好?
  5. unity3d 词典访问_正确的词典访问方式
  6. 如何启用计算机睡眠功能,台式机睡眠如何开启
  7. python 生成器
  8. python pdfminer的功能_使用Python pdfMiner提取每页文本?
  9. 语音转文字的测试用例
  10. PC端后台项目的总结