java word文档转PDF(服务端)

<!-- 文件转换成pdf-->

MAVEN依赖
<dependency>   
    <groupId>com.documents4j</groupId>    
    <artifactId>documents4j-local</artifactId>    
    <version>1.1.1</version>
</dependency>
<dependency>    
    <groupId>com.documents4j</groupId>    
    <artifactId>documents4j-transformer-msoffice-word</artifactId>    
    <version>1.1.1</version>
</dependency>

代码部分

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

    /**
     * 文档文件预览
     */
    @RequestMapping(path = "/previewFile")
    public void preview(HttpServletResponse response,  @RequestParam(required = true)String path, @RequestParam(required = true)String fileName, @RequestParam(required = true)String suffix) throws Exception {
        // 读取pdf文件的路径
        String pdfPath="";
        // 将对应的后缀转换成小写
        String lastSuffix=suffix.toLowerCase();
        //读取文件内容,获取文件存储的路径
        String orgPath = filePath  + path;
        // 生成pdf文件的路径
        String toPath = filePath + "pdf/";
        // 判断对应的pdf是否存在,不存在则创建
        File folder = new File(toPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        // doc类型
        if (lastSuffix.equals("pdf")) {
            // pdf 文件不需要转换,直接从上传文件路径读取即可
            pdfPath=orgPath;
        } else {
            // 转换之后的pdf文件
            String newName=fileName.replace(lastSuffix,"pdf");;
            File newFile = new File( toPath+"/"+newName);
            // 如果转换之后的文件夹中有转换后的pdf文件,则直接从里面读取即可
            if (newFile.exists()) {
                pdfPath =toPath+"/"+newName;
            }else {
                pdfPath = wordToPdf(fileName,orgPath, toPath,lastSuffix);
            }
        }
        // 读取文件流上
        FileInputStream fis = new FileInputStream(pdfPath);
        //设置返回的类型
        response.setContentType("application/pdf");
        //得到输出流,其实就是发送给客户端的数据。
        OutputStream os = response.getOutputStream();
        try {
            int count = 0;
            //fis.available()返回文件的总字节数
            byte[] buffer = new byte[fis.available()];
            //read(byte[] b)方法一次性读取文件全部数据。
            while ((count = fis.read(buffer)) != -1)
                os.write(buffer, 0, count);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null)
                os.close();
            if (fis != null)
                fis.close();
        }
    }

 

   /**
     * 将之前对应的word文件转换成pdf,然后预览pdf文件
     */
    public String wordToPdf(String orgFile,String orgPath, String toPath, String suffix ){
        // 转换之后的pdf文件
        String targetFile=orgFile.replace(suffix,"pdf");
        File inputWord = new File(orgPath);
        File outputFile = new File(toPath+targetFile);
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            if(suffix.equals("doc")){
                converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            } else if(suffix.equals("docx")){
                converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            } else if(suffix.equals("txt")){
                converter.convert(docxInputStream).as(DocumentType.TEXT).to(outputStream).as(DocumentType.PDF).execute();
            }
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toPath+targetFile;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值