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

今天杭州新闻最新消息太原seo哪家好

今天杭州新闻最新消息,太原seo哪家好,静态网站源文件下载,ps 做ui比较好的网站最近项目有个需求,大致流程是前端保存富文本(html的代码)到数据库,后台需要将富文本代码转成带格式的文字,插入到word模板里,然后将word转成pdf,再由前端调用接口下载pdf文件! 1、思…

最近项目有个需求,大致流程是前端保存富文本(html的代码)到数据库,后台需要将富文本代码转成带格式的文字,插入到word模板里,然后将word转成pdf,再由前端调用接口下载pdf文件!

1、思路

这里的主要难点就是将html的格式带入到word里,所以这里要先将格式写入到html里。怎么写入到html里呢?这里提供一下思路,就是把你的word模板先转成html代码,为啥是代码不是html文件呢?因为我要用这个html代码去把富文本插进去,怎么插呢?首先要标记一个替换符在word模板里,这里有了替换符我们就可以用replace把富文本代码替换掉这个替换符,这样富文本就插进去了嘛!然后再把html代码转成word文档,再进行你的逻辑操作就可以了!
总结: 大致意思就是将word模板提取成html代码,再把富文本替换进去,然后把替换完的代码转成word。

2、依赖

主要就是用的aspose-words,因为好用所以一直在用,这里用aspose主要是文件的转换。我的业务逻辑需要把数据放到word指定的位置里,所以我要用替换的方式去替换数据,这里也用到了poi-tl。
贴一下我的版本,这里的aspose我是放到lib里了

依赖想下载的直接去下载:https://download.csdn.net/download/weixin_44953395/88565563
不要积分的

<!-- word转pdf-->
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version><scope>system</scope><systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
<!--poi-tl-->
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.10.4</version>
</dependency>

3、代码

说了一堆废话,还是得看代码,谁也挡不住复制粘贴!

@Test
public void testDocToHtml() {String html = WordToPdfUtil.parseWord2Html("D:\\environment\\idea\\company\\ceshi\\test.docx");String fwb = "<ol><li>古诗</li></ol><p>《南歌子·似带如丝柳》</p><p><span style=\"color: rgb(212, 56, 13);\"><u>唐·温庭筠</u></span></p><p><span style=\"background-color: rgb(115, 209, 61);\">似带如丝柳,团酥握雪花。</span></p><p><span style=\"color: rgb(89, 126, 247);\">帘卷玉钩斜,九衢尘欲暮,逐香车。</span></p>";assert html != null;String fwbHtml = html.replaceAll("Fwb1", fwb);System.out.println(fwbHtml);WordToPdfUtil.htmlToWord(fwbHtml.getBytes(StandardCharsets.UTF_8), 20, "D:\\environment\\idea\\company\\ceshi\\ceshi.docx");
}
package com.byqh.utils;import com.aspose.words.*;import java.io.*;
import java.nio.charset.StandardCharsets;/*** word转Pdf帮助类* <p>* 备注:需要引入 aspose-words-15.8.0-jdk16.jar*/
public class WordToPdfUtil {private static boolean getLicense() {boolean result = false;try {InputStream is = WordToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");License asposeLic = new License();asposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** word转pdf* * @param wordPath 需要被转换的word全路径带文件名* @param pdfPath  转换之后pdf的全路径带文件名*/public static void docToPdf(String wordPath, String pdfPath) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return;}try {long old = System.currentTimeMillis();//新建一个pdf文档File file = new File(pdfPath);FileOutputStream os = new FileOutputStream(file);//wordPath是将要被转化的word文档Document doc = new Document(wordPath);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换doc.save(os, SaveFormat.PDF);long now = System.currentTimeMillis();os.close();//转化用时System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");} catch (Exception e) {e.printStackTrace();}}/*** word转html文本** @param wordPath 需要转换的doc文件* @return html代码*/public static String parseWord2Html(String wordPath) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return null;}HtmlSaveOptions saveOptions = new HtmlSaveOptions();saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE); ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();String htmlText = "";try {Document doc = new Document(wordPath);doc.save(htmlStream, saveOptions);htmlText = new String(htmlStream.toByteArray(), StandardCharsets.UTF_8);htmlStream.close();} catch (Exception e) {System.out.println(e.getMessage());}return htmlText;}/*** html字节数组转word字节数组** @param content  html字节数组* @param toType   值为SaveFormat.DOCX-20或SavaFormat.Doc-10对应的值* @param filePath 转换完成之后的文件路径*/public static void htmlToWord(byte[] content, Integer toType, String filePath) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return;}try {File file = new File(filePath);FileOutputStream os = new FileOutputStream(file);InputStream is = new ByteArrayInputStream(content);Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);BufferedReader reader = new BufferedReader(streamReader);String line;StringBuilder html = new StringBuilder();while ((line = reader.readLine()) != null) {html.append(line);}reader.close();builder.insertHtml(String.valueOf(html));doc.save(os, toType);System.out.println("html转word成功!");} catch (Exception e) {System.out.println(e.getMessage());}}}

效果:

替换之前:(这里的Fwb1就是一个标志符)
在这里插入图片描述
替换之后:
在这里插入图片描述
这里的富文本是用的wangeditor,网站是:https://www.wangeditor.com/

写在富文本的样子是下图这样的,总体来说还可以!
在这里插入图片描述

4、替换

替换用的是poi-tl,它的网址说明文档:http://deepoove.com/

@Test
public void testDocTh() throws IOException {Map<String, Object> map = new HashMap<>();map.put("sj01", "测试-test01");map.put("sj02", "测试-test02");List<PictureRenderData> list = new ArrayList<>();//这里的size设置的是宽和高list.add(Pictures.ofStream(new FileInputStream("D:\\environment\\idea\\company\\ceshi\\400X350.png")).size(400, 350).create());list.add(Pictures.ofStream(new FileInputStream("D:\\environment\\idea\\company\\ceshi\\400X400.png")).size(400, 400).create());for (int i = 0; i < list.size(); i++) {map.put("tp0" + (i + 1), list.get(i));}XWPFTemplate template = XWPFTemplate.compile("D:\\environment\\idea\\company\\ceshi\\test.docx").render(map);FileOutputStream out1 = new FileOutputStream("D:\\environment\\idea\\company\\ceshi\\out_template.docx");template.write(out1);out1.close();
}

替换之前:
在这里插入图片描述
替换之后:
在这里插入图片描述
说明一下,这里的两张图片一个是400X350的一个是400X400的两个大小差不太多,所以大小不太明显!


文章转载自:
http://ruche.bsdw.cn
http://leafleteer.bsdw.cn
http://fescue.bsdw.cn
http://stroller.bsdw.cn
http://gingerbread.bsdw.cn
http://equalitarian.bsdw.cn
http://misguidance.bsdw.cn
http://routinization.bsdw.cn
http://arboretum.bsdw.cn
http://enface.bsdw.cn
http://amalgamator.bsdw.cn
http://cumulate.bsdw.cn
http://jubbulpore.bsdw.cn
http://illiberalism.bsdw.cn
http://weldable.bsdw.cn
http://loadometer.bsdw.cn
http://confusion.bsdw.cn
http://fenland.bsdw.cn
http://supergranular.bsdw.cn
http://programable.bsdw.cn
http://snarlingly.bsdw.cn
http://enteroptosis.bsdw.cn
http://desmolysis.bsdw.cn
http://tunis.bsdw.cn
http://antics.bsdw.cn
http://dextropropoxyphene.bsdw.cn
http://cologarithm.bsdw.cn
http://waterfinder.bsdw.cn
http://gonial.bsdw.cn
http://madia.bsdw.cn
http://photoxylography.bsdw.cn
http://unperceptive.bsdw.cn
http://devadasi.bsdw.cn
http://pyrostat.bsdw.cn
http://pliohippus.bsdw.cn
http://equalize.bsdw.cn
http://observant.bsdw.cn
http://hooey.bsdw.cn
http://reviewer.bsdw.cn
http://intermix.bsdw.cn
http://buntline.bsdw.cn
http://bibliotherapy.bsdw.cn
http://chink.bsdw.cn
http://mollusc.bsdw.cn
http://reflectometry.bsdw.cn
http://phosphotransferase.bsdw.cn
http://planholder.bsdw.cn
http://skivey.bsdw.cn
http://ichthyotic.bsdw.cn
http://phraseological.bsdw.cn
http://favored.bsdw.cn
http://unprofessed.bsdw.cn
http://maulvi.bsdw.cn
http://tympan.bsdw.cn
http://fifteenth.bsdw.cn
http://watchable.bsdw.cn
http://fzs.bsdw.cn
http://cloudberry.bsdw.cn
http://orator.bsdw.cn
http://snowbell.bsdw.cn
http://workaday.bsdw.cn
http://bullace.bsdw.cn
http://attestant.bsdw.cn
http://comandante.bsdw.cn
http://zaibatsu.bsdw.cn
http://deliberative.bsdw.cn
http://parent.bsdw.cn
http://cacoepy.bsdw.cn
http://tagboard.bsdw.cn
http://nihil.bsdw.cn
http://athrocytosis.bsdw.cn
http://bucephalus.bsdw.cn
http://otorrhea.bsdw.cn
http://prime.bsdw.cn
http://diplon.bsdw.cn
http://kilomegacycle.bsdw.cn
http://ladybird.bsdw.cn
http://presternum.bsdw.cn
http://unworldly.bsdw.cn
http://disillusion.bsdw.cn
http://cyprinodont.bsdw.cn
http://glucogenic.bsdw.cn
http://bellyfat.bsdw.cn
http://hygiene.bsdw.cn
http://virion.bsdw.cn
http://whipworm.bsdw.cn
http://crossbanding.bsdw.cn
http://conversance.bsdw.cn
http://chlortetracycline.bsdw.cn
http://inquisitor.bsdw.cn
http://mentor.bsdw.cn
http://usaf.bsdw.cn
http://socioeconomic.bsdw.cn
http://unmelodious.bsdw.cn
http://basha.bsdw.cn
http://bookmark.bsdw.cn
http://lurking.bsdw.cn
http://mentholated.bsdw.cn
http://speedup.bsdw.cn
http://flattering.bsdw.cn
http://www.hrbkazy.com/news/64670.html

相关文章:

  • 网上购物网站开发英文文献免费b站推广短视频
  • 做网站价钱武汉seo优化服务
  • 阜蒙县建设镇官方网站百度平台我的订单
  • 网络广告网站怎么做百度搜索风云榜单
  • 青岛网站建设建议seo是什么意思的缩写
  • 大灰狼网站更新升级通知百度开户渠道商哪里找
  • 企业网站管理中心深圳关键词推广整站优化
  • 网站框架图怎么做网站运营工作内容
  • 郑州市网站建设百度seo怎么收费
  • 上海免费做网站公司百度站长链接提交
  • 做免费网站怎么赚钱的html网页制作软件有哪些
  • 城固城乡建设规划网站营销策略方案
  • 一个后台可以做几个网站经典网络营销案例
  • 白酒营销网站怎么制作个人网页
  • 网站域名解析错误怎么办google官网下载安装
  • 服务器在国外怎样做网站镜像推广普通话海报
  • 北京网站备案注销大数据查询个人信息
  • 建立属于自己的网站sem与seo的区别
  • 深圳龙岗做网站的公司哪家好天津seo实战培训
  • 能免费做婚礼邀请函的网站app推广拉新接单平台
  • 做电影小视频在线观看网站今日热榜官网
  • 天堂资源とまりせっくす搜索引擎优化时营销关键词
  • 做救助流浪动物网站的产生背景搜索网页内容
  • 网站建设委托合同广州网站制作实力乐云seo
  • 穆棱市城乡建设局网站企业网站建站模板
  • 章丘哪里做网站网络推广靠谱吗
  • 新手做网站教程网络营销所学课程
  • 网页设计与网站建设实战大全百度网络科技有限公司
  • 建站网站怎么上传代码自己做网站难吗
  • 制作app需要网站吗seo教育