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

news2024/10/5 23:30:56

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

 

例如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.coloradmin.cn/o/946711.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据的语言:学习数据可视化的实际应用

数据可视化应该学什么&#xff1f;这是一个在信息时代越来越重要的问题。随着数据不断增长和积累&#xff0c;从社交媒体到企业业务&#xff0c;从科学研究到医疗健康&#xff0c;我们都面临着海量的数据。然而&#xff0c;数据本身往往是冰冷、抽象的数字&#xff0c;对于大多…

03-基础例程3

基础例程3 01、外部中断 ESP32的外部中断有上升沿、下降沿、低电平、高电平触发模式。 实验目的 使用外部中断功能实现按键控制LED的亮灭 按键按下为0。【即下降沿】 * 接线说明&#xff1a;按键模块-->ESP32 IO* (K1-K4)-->(14,27,26,25)* * …

安全生产作业现场违规行为识别 opencv

安全生产作业现场违规行为识别算法通过pythonopencv网络模型算法框架设定了各种合规行为和违规行为的模型&#xff0c;安全生产作业现场违规行为识别算法检测到违规行为&#xff0c;将立即进行抓拍并发送告警信息给相关人员&#xff0c;以便及时采取相应的处置措施。OpenCV是一…

HT for Web (Hightopo) 使用心得(6)- 3D场景环境配置(天空球,雾化,辉光,景深)

在前一篇文章《Hightopo 使用心得&#xff08;5&#xff09;- 动画的实现》中&#xff0c;我们将一个直升机模型放到了3D场景中。同时&#xff0c;还利用动画实现了让该直升机围绕山体巡逻。在这篇文章中&#xff0c;我们将对上一篇的场景进行一些环境上的丰富与美化。让场景更…

02-基础例程2

基础例程2 01、直流电机 对于UL2003来说&#xff0c;可以看作是非门 输入为1&#xff0c;输出为0&#xff1b; 输入为0&#xff0c;输出为高组态[接一个上拉电阻即为1] 线路的连接 4路是用做步进电机的 对于直流电机&#xff0c;只需要一路。即异端接VCC&#xff0c;一段接…

unordered-------Hash

✅<1>主页&#xff1a;我的代码爱吃辣&#x1f4c3;<2>知识讲解&#xff1a;数据结构——哈希表☂️<3>开发环境&#xff1a;Visual Studio 2022&#x1f4ac;<4>前言&#xff1a;哈希是一种映射的思想&#xff0c;哈希表即使利用这种思想&#xff0c;…

基于野马算法优化的BP神经网络(预测应用) - 附代码

基于野马算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码 文章目录 基于野马算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码1.数据介绍2.野马优化BP神经网络2.1 BP神经网络参数设置2.2 野马算法应用 4.测试结果&#xff1a;5.Matlab代码 摘要…

【深入浅出设计模式--状态模式】

深入浅出设计模式--状态模式 一、背景二、问题三、解决方案四、 适用场景总结五、后记 一、背景 状态模式是一种行为设计模式&#xff0c;让你能在一个对象的内部状态变化时改变其行为&#xff0c;使其看上去就像改变了自身所属的类一样。其与有限状态机的概念紧密相关&#x…

RT-Thread在STM32硬件I2C的踩坑记录

RT-Thread在STM32硬件I2C的踩坑记录 0.前言一、软硬件I2C区别二、RT Thread中的I2C驱动三、尝试适配硬件I2C四、i2c-bit-ops操作函数替换五、Attention Please!六、总结 参考文章&#xff1a; 1.将硬件I2C巧妙地将“嫁接”到RTT原生的模拟I2C驱动框架 2.基于STM32F4平台的硬件I…

flutter 上传图片并裁剪

1.首先在pubspec.yaml文件中新增依赖pub.dev image_picker: ^0.8.75 image_cropper: ^4.0.1 2.在Android的AndroidManifest.xml文件里面添加权限 <activityandroid:name"com.yalantis.ucrop.UCropActivity"android:screenOrientation"portrait"andro…

Golang网络编程

互联网协议介绍引入 1. 物理层&#xff08;Physical Layer&#xff09;&#xff1a; - 功能&#xff1a;物理层负责定义物理介质传输数据的方式和规范&#xff0c;它传输的是原始数据比特流。 - 协议&#xff1a;Ethernet、Wi-Fi、USB、光纤等。 - 例子&#xff1a;将…

[javaWeb]Socket网络编程

网络编程&#xff1a;写一个应用程序,让这个程序可以使用网络通信。这里就需要调用传输层提供的 api。 Socket套接字 传输层提供协议&#xff0c;主要是两个: UDP和TCP 提供了两套不同的 api&#xff0c;这api也叫做socket api。 UDP和 TCP 特点对比&#xff1a; UDP: 无连…

数据结构day05(单链表)

今日任务&#xff1a; 思维导图&#xff1a; 实现 代码&#xff1a;&#xff08;多文件&#xff09; head.h #ifndef __HEAD_H__ #define __HEAD_H__#include <stdio.h> #include <string.h> #include <stdlib.h> typedef int datatype;typedef struct Lin…

Nuxt3 与 Vue3 的 Server api 全栈开发之路

Nuxt Server Api 阅读时长&#xff1a;15分钟 本文内容&#xff1a; 国内关于Nuxt3的资料太少了&#xff0c;而Nuxt3又发布了没有多久&#xff0c;导致资料太少。本文浓缩讲解了&#xff0c;对于一个前端开发&#xff0c;上手使用 Nuxt3&#xff0c;并一个人承担前后端开发的所…

Hive3第六章:更换引擎

系列文章目录 Hive3第一章&#xff1a;环境安装 Hive3第二章&#xff1a;简单交互 Hive3第三章&#xff1a;DML数据操作 Hive3第三章&#xff1a;DML数据操作(二) Hive3第四章&#xff1a;分区表和分桶表 Hive3第五章&#xff1a;函数 Hive3第六章&#xff1a;更换引擎 文章目…

网络安全—0基础学习笔记(黑客)

一、前言 1.这是一条坚持的道路,三分钟的热情可以放弃往下看了. 2.多练多想,不要离开了教程什么都不会了.最好看完教程自己独立完成技术方面的开发. 3.有时多 google,baidu,我们往往都遇不到好心的大神,谁会无聊天天给你做解答. 4.遇到实在搞不懂的,可以先放放,以后再来解决. …

2023年智慧政务一网通办云平台顶层设计与建设方案PPT

导读&#xff1a;原文《2023年智慧政务一网通办云平台顶层设计与建设方案PPT》&#xff08;获取来源见文尾&#xff09;&#xff0c;本文精选其中精华及架构部分&#xff0c;逻辑清晰、内容完整&#xff0c;为快速形成售前方案提供参考。 部分内容&#xff1a; 喜欢文章&#…

Spring——RESTful Web服务

文章目录 RESTful Web 服务介绍内容概览下载 Lombok 优化代码利器RESTful Web 服务开发运行项目并测试效果 RESTful Web 服务介绍 本节我们将开发一个简单的 RESTful Web 服务。 RESTful Web 服务与传统的 MVC 开发一个关键区别是返回给客户端的内容的创建方式&#xff1a;传…

Go操作各大消息队列教程(RabbitMQ、Kafka)

Go操作各大消息队列教程 1 RabbitMQ 1.1 概念 ①基本名词 当前市面上mq的产品很多&#xff0c;比如RabbitMQ、Kafka、ActiveMQ、ZeroMQ和阿里巴巴捐献给Apache的RocketMQ。甚至连redis这种NoSQL都支持MQ的功能。 Broker&#xff1a;表示消息队列服务实体Virtual Host&#x…

感觉车载测试的这一波敏捷风快过去了

敏捷&#xff0c;算不得汽车行业的原生产物&#xff0c;几年前&#xff0c;耳边很少听到这个字眼&#xff0c;基本算是在近几年传统汽车行业开始衰落的大背景下&#xff0c;而后伴随着软件从互联网等行业传进来的。 这两年&#xff0c;大家开始把敏捷谈得风生水起&#xff0c;…