doc,docx转pdf工具类PdfUtil

依赖

<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.1.5</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.1.5</version>
</dependency>

工具类

PdfUtil

文件类型为doc和docx后缀转pdf格式,支持图片转换

public class PdfUtil {
    /**
     * 本地文件word转pdf
     * @param wordFilePath word文件路径
     * @param pdfFilePath  pdf文件路径
     * @param suffix  文件后缀
     * @return 成功或失败
     */
    public static boolean doc2Pdf(File wordFilePath, File pdfFilePath, String suffix) {
        boolean result = false;
        try {
            InputStream inputStream = new FileInputStream(wordFilePath);
            OutputStream outputStream = new FileOutputStream(pdfFilePath);
            IConverter converter = LocalConverter.builder().build();

            // 工具后缀进行转换
            if(suffix.equals(".doc")){
                converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            } else if(suffix.equals(".docx")){
                converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            } else if(suffix.equals(".txt")){
                converter.convert(inputStream).as(DocumentType.TEXT).to(outputStream).as(DocumentType.PDF).execute();
            }
            outputStream.close();
            result = true;
        } catch (Exception e) {

            e.printStackTrace();
        }
        return result;
    }
}

ResponseUtil

浏览器在线预览工具类

public class ResponseUtil {
    public static void responseBrowser(File file, HttpServletResponse response){
        //下面的是进行响应客户端的测试代码
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            //设置Http响应头告诉浏览器下载这个附件,下载的文件名也是在这里设置的

            System.out.println("file.getName()   >>>"+file.getName());

            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
            // response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
            System.out.println("file.getPath() >>>>>>"+file.getPath());

            OutputStream outputStream = null;

            outputStream = response.getOutputStream();

            byte[] bytes = new byte[2048];
            int len = 0;
            while ((len = fileInputStream.read(bytes))>0){
                outputStream.write(bytes,0,len);
            }
            fileInputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件响应出错,读写出错");
        }

    }


    public static void onlineReader(File file, HttpServletResponse response){
        if (file.exists()){
            byte[] data = null;
            try {
                response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
                FileInputStream input = new FileInputStream(file);
                data = new byte[input.available()];
                input.read(data);

                response.getOutputStream().write(data);
                input.close();
            } catch (Exception e) {
                System.out.println(e);
            }

        }else{
            System.out.println("文件不存在");
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李熠漾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值