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

poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)

选中图片右击  选择设置图片格式

 

例如word模板

 

maven依赖

        <!-- java 读取word文件里面的加颜色的字体  转pdf 使用  --><dependency><groupId> e-iceblue </groupId><artifactId>spire.doc.free</artifactId><version>3.9.0</version></dependency><!--poi 的相关组件 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.2</version></dependency><!-- 不添加此包会提示错误 :   org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory   --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency><!-- 用于  word 转pdf  --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>xdocreport</artifactId><version>2.0.2</version></dependency><!-- pdf 添加水印  对PDF文件的操作 --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.1</version></dependency><!-- PDF文件 字体 防止中文乱码  --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><!--基于 poi实现word数据的替换 --><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.9.1</version></dependency><!--如果下载jar失败,说明下载jar失败,需要以下的maven依赖[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException--> <repository><id>com.e-iceblue</id><url>http://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories><!--        <dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.25</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId><version>1.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.core</artifactId><version>1.0.6</version></dependency>--><!-- 打包使用,需要配置 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!-- 打包时会将本地jar一起打包 --><configuration><includeSystemScope>true</includeSystemScope></configuration></plugin></plugins></build>

 

  <!--基于 poi实现word数据的替换 --><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.9.1</version></dependency><!-- 不添加此包会提示错误 :   org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory   --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>org.apache.poi.xwpf.converter.pdf</artifactId><version>1.0.6</version></dependency><!-- 用于  word 转pdf  --><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>xdocreport</artifactId><version>2.0.2</version></dependency>

读取

 代码

 @GetMapping("exportPDF")public AjaxResult exportPDF(HttpServletResponse response) throws Exception {LoginUser loginUser = getLoginUser();OpenCompanyInfo companyInfo = openCompanyInfoService.selectEnterpriseInfo(loginUser.getId());if (companyInfo != null) {Map<String, Object> data = new HashMap<>();data.put("legalDeputy", companyInfo.getLegalDeputy());data.put("companyName", companyInfo.getCompanyName());data.put("companyCode", companyInfo.getCompanyCode());if (StringUtils.isNoneBlank(companyInfo.getBusinessLicenseUrl())) {InputStream businessLicenseUrl = minioClientUtils.returnInputStream(null, companyInfo.getBusinessLicenseUrl());data.put("businessLicenseUrl", Pictures.ofStream(businessLicenseUrl, PictureType.JPEG).size(120, 120).create());}if (StringUtils.isNoneBlank(companyInfo.getCompanyLogoUr())) {InputStream companyLogoUrl = minioClientUtils.returnInputStream(null, companyInfo.getCompanyLogoUr());data.put("companyLogoUrl", Pictures.ofStream(companyLogoUrl, PictureType.JPEG).size(120, 120).create());}InputStream inputStreamFile = ResourceReader.class.getResourceAsStream("/templates/模版.docx");XWPFTemplate template = XWPFTemplate.compile(inputStreamFile).render(data);
//            XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\11949\\Desktop\\模版.docx").render(data);byte[] array = null;ByteArrayOutputStream baos = new ByteArrayOutputStream();template.writeAndClose(baos);array = baos.toByteArray();baos.close();template.close();InputStream inputStream = new ByteArrayInputStream(array);ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();XWPFDocument xwpfDocument = new XWPFDocument(inputStream);fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());inputStream.close();xwpfDocument.close();response.setContentType("application/octet-stream");String format = DateUtil.format(new Date(), "yyyy-MM-dd");response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("认证授权函" + format + ".pdf", "UTF-8"));OutputStream out = response.getOutputStream();BufferedOutputStream bos = new BufferedOutputStream(out);pdfBaos.writeTo(bos);bos.flush();out.flush();PoitlIOUtils.closeQuietlyMulti(template, bos, out);}return null;}

http://www.lryc.cn/news/145804.html

相关文章:

  • [element-ui] el-tree 懒加载load
  • 【C++】使用 nlohmann 解析 json 文件
  • Nginx到底是什么,他能干什么?
  • 如何判断一个java对象还活着
  • Go语言基础之结构体
  • 前端食堂技术周刊第 96 期:2023 CSS 状态、Nuxt 3.7、TypeScript 5.2、eBay 性能优化、贝塞尔曲线
  • 一文总结Redis知识点
  • ARM寄存器组
  • Windows查看当前文件夹下的所有.c文件的个数
  • ubuntu Qt 地图离线调用
  • Android Studio升级到Android API 33版本后,XML布局输入没有提示
  • 操作XML(带命名空间)
  • 二叉搜索树(C++)
  • 软件架构知识点
  • C语言日常刷题6
  • 微信小程序使用stomp.js实现STOMP传输协议的实时聊天
  • 基于饥饿游戏算法优化的BP神经网络(预测应用) - 附代码
  • [ 云计算 | AWS ] Java 应用中使用 Amazon S3 进行存储桶和对象操作完全指南
  • 【Spring Boot】Spring Boot 配置 Hikari 数据库连接池
  • MR混合现实石油化工课堂情景实训教学演示
  • php thinkphp 抖音支付,订单同步接口分享
  • excel功能区(ribbonx)编程笔记--2 button控件与checkbox控件
  • JavaScript 知识点
  • 深入理解 JVM 之——动手编译 JDK
  • [移动通讯]【Carrier Aggregation in LTE】【 Theory + Log analysis-1】
  • Sui诚邀您参加KBW「首尔Web3之夜」
  • 19.CSS雨云动画特效
  • 第61步 深度学习图像识别:多分类建模(TensorFlow)
  • Spark 7:Spark SQL 函数定义
  • ThinkPHP 文件上传 fileSystem 扩展的使用