java ofd文件转pdf
之前有博客写了ofd与pdf文件进行相互转化, 【pdf与ofd相互转化 】,但是spire-pdf的jar包进行ofd转为pdf时,如果是双层ofd文件,最后转化的结果会丢失上层内容。因此可以使用spire.free.pdf进行pdf转化为ofd,使用ofdr进行ofd转为pdf。
ofd转为pdf
ofdrw-full git 官方文档 ofdrw-full能完整转化spire转化不了的双层ofd文件。
引入依赖:
<dependency>
<groupId>org.ofdrw</groupId>
<artifactId>ofdrw-full</artifactId>
<version>1.20.2</version>
</dependency>
如果出现报错,log4j-slf4j-impl依赖与spring-boot引用的log4j依赖有冲出,直接排除引用即可
<dependency>
<groupId>org.ofdrw</groupId>
<artifactId>ofdrw-full</artifactId>
<version>1.20.2</version>
<exclusions>
<exclusion>
<artifactId>log4j-slf4j-impl</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
@Override
public void onlinePreviewOFD(String fileName, HttpServletResponse response) {
if(StrUtil.isBlank(fileName)){
return ;
}
OutputStream outputStream = null;
QueryWrapper<SysFile> sysFileQueryWrapper = new QueryWrapper<>();
sysFileQueryWrapper.lambda().eq(SysFile::getFileName,fileName);
sysFileQueryWrapper.last(" limit 1 ");
SysFile sysFile = this.getOne(sysFileQueryWrapper);
if(ObjectUtil.isNotEmpty(sysFile)){
// 为不规范的字体名创建映射
FontLoader.getInstance()
.addAliasMapping("小标宋体", "方正小标宋简体")
.addAliasMapping("KaiTi_GB2312", "楷体")
.addAliasMapping("楷体", "KaiTi")
.addSimilarFontReplaceRegexMapping(".*Kai.*", "楷体")
.addSimilarFontReplaceRegexMapping(".*Kai.*", "楷体")
.addSimilarFontReplaceRegexMapping(".*MinionPro.*", "SimSun")
.addSimilarFontReplaceRegexMapping(".*SimSun.*", "SimSun")
.addSimilarFontReplaceRegexMapping(".*Song.*", "宋体")
.addSimilarFontReplaceRegexMapping(".*MinionPro.*", "SimSun");
FontLoader.getInstance().scanFontDir(new File("src/main/resources/fonts"));
FontLoader.setSimilarFontReplace(true);
//oss文件夹
String bucketName = sysFile.getBucketName();
//本身文件名
String original = sysFile.getOriginal();
//oss文件文件名
String ossFileName = sysFile.getOssFileName();
//type
String type = sysFile.getType();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
OSSObject ossObject = ossClient.getObject(bucketName, ossFileName);
if (StrUtil.equals("ofd",type)) {
try {
// 1. 文件输入路径
InputStream objectContent = ossObject.getObjectContent();
// 2. 转换后文件输出位置
outputStream = response.getOutputStream();标签:文件,outputStream,java,ofdrw,ofd,文件格式,pdf,addSimilarFontReplaceRegexMapping From: https://www.cnblogs.com/wjsqqj/p/17217207.html
// 3. OFD转换PDF
ConvertHelper.toPdf(objectContent, outputStream);
// int read = 0;
// while ((read = input.read(buffBytes)) != -1) {
// outputStream.write(buffBytes, 0, read);
// }
outputStream.flush();
// System.out.println("生成文档位置: " + dst.toAbsolutePath());
} catch (GeneralConvertException e) {
// GeneralConvertException 类型错误表明转换过程中发生异常
e.printStackTrace();
}
}
} catch (Exception e) {
try {
if (outputStream != null) {
outputStream.close();
}
// if (input != null) {
// input.close();
// }
} catch (IOException e2) {
e2.printStackTrace();
}
log.error("文件读取异常", e);
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
可以参考该项目https://gitee.com/ofdrw/ofdrw