当前位置: 首页 > news >正文

宜宾做网站淘宝运营培训

宜宾做网站,淘宝运营培训,成都公司网站制作,视频剪辑制作教学总结 建议使用aspose-words转pdf,poi的容易出问题还丑… poi的(多行的下边框就不对了) aspose-words的(基本和word一样) poi工具转换 <!-- 处理PDF --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres…

总结

建议使用aspose-words转pdf,poi的容易出问题还丑…

poi的(多行的下边框就不对了)
在这里插入图片描述

aspose-words的(基本和word一样)
在这里插入图片描述

poi工具转换

        <!-- 处理PDF --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId><version>2.0.3</version></dependency>

这个工具使用了poi,最新的2.0.3对应poi的5.2.0,2.0.1对应poi的3.15

使用

//拿到word流
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/muban3.docx");if (inputStream == null) {throw new MsgException("读取模板失败");}
XWPFDocument document = new XWPFDocument(inputStream);
//.....word处理
PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding( BaseFont.CP1250 );
//转pdf操作 (直接写入响应)
PdfConverter.getInstance().convert(document, response.getOutputStream(), pdfOptions);
response.setContentType("application/pdf");

或者写入输出流

    /*** 将word转为pdf并返回一个输出流** @param document 输出文件名(pdf格式)*/public static ByteArrayOutputStream wordToPdfOutputStream(XWPFDocument document) throws IOException {//word转pdfByteArrayOutputStream outputStream = new ByteArrayOutputStream();PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding( BaseFont.CP1250 );//转pdf操作PdfConverter.getInstance().convert(document, outputStream, pdfOptions);return outputStream;}
问题

poi改了word之后,生成没问题,word中创建的表格,转pdf的时候经常出问题(直接报错或者合并无效)
在这里插入图片描述

研究了2天,pdf转一直各种问题,一起之下换技术

aspose-words

https://blog.csdn.net/Wang_Pink/article/details/141898210

        <dependency><groupId>com.luhuiguo</groupId><artifactId>aspose-words</artifactId><version>23.1</version></dependency>

poi处理word一堆的依赖,这个一个就好,而且本身就支持转pdf!!!

使用

  1. 在resources创建word-license.xml
    在这里插入图片描述
<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
  1. 工具类
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;@Slf4j
public class Doc2PdfUtil {/*** 获取 license 去除水印* 若不验证则转化出的pdf文档会有水印产生*/private static void getLicense() {String licenseFilePath = "word-license.xml";try {InputStream is = Doc2PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);License license = new License();license.setLicense(Objects.requireNonNull(is));} catch (Exception e) {log.error("license verify failed");e.printStackTrace();}}/*** word 转 pdf** @param wordFile word 文件路径* @param pdfFile  生成的 pdf 文件路径*/public static void word2Pdf(String wordFile, String pdfFile) {File file = new File(pdfFile);if (!file.getParentFile().exists()) {file.getParentFile().mkdir();}getLicense();try (FileOutputStream os = new FileOutputStream(new File(pdfFile))) {Document doc = new Document(wordFile);doc.save(os, SaveFormat.PDF);} catch (Exception e) {log.error("word转pdf失败", e);}}/*** word 转 pdf** @param wordFile word 文件流* @param pdfFile  生成的 pdf 文件流*/public static void word2Pdf(InputStream wordFile, OutputStream pdfFile) {getLicense();try {Document doc = new Document(wordFile);doc.save(pdfFile, SaveFormat.PDF);} catch (Exception e) {log.error("word转pdf失败", e);}}
}

使用

Doc2PdfUtil.word2Pdf("aa.docx","bb.pdf");

我是依旧使用poi处理word,用这个转pdf

//拿到word流
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/muban3.docx");if (inputStream == null) {throw new MsgException("读取模板失败");}
XWPFDocument document = new XWPFDocument(inputStream);
//.....word处理ByteArrayInputStream in = null;try {//由于使用的poi的document,需要现将poi的document转为普通的输入流in = WordUtil.getInputStream(document);Doc2PdfUtil.word2Pdf(in,response.getOutputStream());response.setContentType("application/pdf");} catch (Exception e) {log.error("报告下载失败", e);} finally {try {document.close();} catch (Exception e1) {log.error("document 流关闭失败", e1);}if (in != null) {try {in.close();} catch (Exception e1) {log.error("in 流关闭失败", e1);}}}
    public static ByteArrayInputStream getInputStream(XWPFDocument document) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();try {document.write(outputStream);return outputStreamToPdfInputStream(outputStream);} catch (IOException e) {throw new RuntimeException(e);} finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}}/*** 将word转为pdf并返回一个输入流** @param outputStream 输出文件名(pdf格式)*/public static ByteArrayInputStream outputStreamToPdfInputStream(ByteArrayOutputStream outputStream) throws IOException {//输出的pdf输出流转输入流try {//临时byte[] bookByteAry = outputStream.toByteArray();return new ByteArrayInputStream(bookByteAry);} catch (Exception e) {e.printStackTrace();return null;}}

完美转换
在这里插入图片描述

http://www.hkea.cn/news/873449/

相关文章:

  • 用hadoop做网站日志分析推广工作的流程及内容
  • 凡科做网站技巧站长之家域名信息查询
  • 网站建设国际深圳网络营销课程ppt
  • 网站开发人员需要具备的能力电脑培训班多少费用
  • discuz集成wordpressseo的概念是什么
  • 子网站如何做网站营销方案模板
  • dreamweaver做的网站电商培训班一般多少钱
  • 国外做科研的网站东莞网站设计公司排名
  • 亿唐网不做网站做品牌原因seo网站诊断报告
  • 宝鸡网站建设东东怎么推广软件让别人下载
  • 21dove谁做的的网站百度一下首页设为主页
  • 猪八戒网站建设推广平台排名前十名
  • 广西建设质监站官方网站站长工具seo综合查询可以访问
  • 通用搭建网站教程优化营商环境的意义
  • 网站中加入地图怎样优化网站排名
  • 网站如何被搜索引擎收录地推推广平台
  • 池州做网站公司游戏搜索风云榜
  • 东丽区做网站网站查询平台
  • wordpress什么主题好用seo优化范畴
  • 局域网端口映射做网站西安竞价托管代运营
  • 重庆网站建设设计公司信息ip网站查询服务器
  • 网站积分的作用seo搜索引擎优化就业前景
  • 珠海网站品牌设计公司简介最新国内新闻重大事件
  • 广东专业网站客服软件定制站长统计app下载大全
  • 广东网站建设公司排名磁力帝
  • 胶南网站建设哪家好成都电脑培训班零基础
  • 集团网站建设哪家好网上推广怎么弄?
  • dz网站建设器最近有新病毒出现吗
  • 个人网站制作说明香港旺道旺国际集团
  • 监控做直播网站免费网站seo