Java中对OFD文件转为PDF操作(简单,详解)

目录

1、引入Maven

2、 代码示例

 3、基于Minio存储文件返回转换后的pdf文件


1、引入Maven

ps:

        引入后启动项目控制台可能会打印红色警告:

        SLF4J: Class path contains multiple SLF4J bindings.

如果你出现了这个问题,不要慌。出错的原因可能不一样,exclusion写法也会不一样。解决方法放在另外一篇文章了:解决SLF4J: Class path contains multiple SLF4J bindings.

<dependency>
    <groupId>org.ofdrw</groupId>
    <artifactId>ofdrw-converter</artifactId>
    <version>1.17.9</version>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
    </exclusions>
</dependency>
2、 代码示例
public class TestOfd2Pdf {
    // 测试比较简单的转换方式
    public static void main(String[] args) {
        File ofdFile = new File("E:\\044002000311_72262900.ofd");
        Path pdfPath = Paths.get("E:\\1.pdf");
        ConvertHelper.toPdf(ofdFile.toPath(), pdfPath);
    }
}
 3、基于Minio存储文件返回转换后的pdf文件

ps:

        byte[]、File、Path等之间的转换。

public File convertOFD2PDF(String bucketName, String fileName, String filePdfPath) {
        byte[] bytes = downloadUseBytes(bucketName, fileName).get("fileBytes");
        String ofdFileName = "/toPdf" + System.currentTimeMillis() + ".ofd";
        File ofdFile = FileUtils.writeBytes(bytes, 
                                  FileUtils.createTempFile(ofdFileName, true));
        Path ofdPath = ofdFile.toPath();
        Path pdfPath = Paths.get(filePdfPath);
        ConvertHelper.toPdf(ofdPath, pdfPath);
        ofdFile.delete(); // 临时ofd文件使用完后删除
        return pdfPath.toFile();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值