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

用jsp做的二手交易网站现在疫情怎么样了最新消息

用jsp做的二手交易网站,现在疫情怎么样了最新消息,静态网站怎么做优化,怎么在网站做gifword模板填充转pdf导出处理 关于word模板填充示例java根据word模板填充数据并导出-五官一体即忢 相关依赖插件【LibreOffice】 安装 LibreOffice:从LibreOffice 官方网站下载并安装适合 Windows 系统或者Linux系统的版本 启动 LibreOffice 服务:打开…

word模板填充转pdf导出处理

关于word模板填充示例java根据word模板填充数据并导出-五官一体即忢

相关依赖插件【LibreOffice】

安装 LibreOffice:从LibreOffice 官方网站下载并安装适合 Windows 系统或者Linux系统的版本

启动 LibreOffice 服务:打开命令提示符,执行以下命令启动 LibreOffice 服务:

注意:

1、默认安装路径:C:\Program Files\LibreOffice\program

2、配置环境变量,将C:\Program Files\LibreOffice\program放到环境变量中Path中

3、端口port=2002可以根据实际情况设定

soffice.exe --headless --accept="socket,host=127.0.0.1,port=2002;urp;"

添加 JODConverter 依赖:如果你使用 Maven 项目,可在pom.xml文件中添加以下依赖:

<!-- word 转pdf 相关依赖 -->
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-core</artifactId><version>4.4.2</version>
</dependency>
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.4.2</version>
</dependency>

工具类示例:

package org.test.manage.utils;import com.deepoove.poi.XWPFTemplate;
import org.apache.commons.io.FileUtils;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.office.LocalOfficeManager;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;public class WordFileUtil {// 端口根据自己启动配置端口调整private static final int OFFICE_PORT = 20020;private static LocalOfficeManager officeManager;static {officeManager = LocalOfficeManager.builder().install().portNumbers(OFFICE_PORT).taskExecutionTimeout(10000L).taskQueueTimeout(5000L).build();try {officeManager.start();} catch (OfficeException e) {throw new RuntimeException(e);}}/*** 处理word* 数据填充Word模板* @param path* @param taskForm  填充内容* @return*/public static byte[] getWord(String path, Map<String, Object> taskForm) {byte[] data = null;File tempFile = null;try {// 获取文件流InputStream stream = WordFileUtil.class.getClassLoader().getResourceAsStream(path);// 创建临时文件tempFile = File.createTempFile("template", ".docx");// 将读取到的内容存储到临时文件中,后面就可以用这个临时文件访问了FileUtils.copyInputStreamToFile(stream, tempFile);// 渲染表格XWPFTemplate template = XWPFTemplate.compile(tempFile.getAbsolutePath()).render(taskForm);ByteArrayOutputStream out = new ByteArrayOutputStream();template.write(out);out.close();template.close();data = out.toByteArray();} catch (IOException e) {e.printStackTrace();} finally {// 删除临时文件if (tempFile != null && tempFile.exists()) {tempFile.delete();}}return data;}/*** 下载Word文件* @param path* @param taskForm 填充内容* @param response HttpServletResponse对象*/public static void downloadWord(String path, Map<String, Object> taskForm, HttpServletResponse response,String fileName) {byte[] data = getWord(path, taskForm);if (data != null) {try {response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");// URL 编码文件名String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());// 设置 Content-Disposition 头response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName);response.getOutputStream().write(data);response.getOutputStream().flush();} catch (IOException e) {e.printStackTrace();}}}/*** 将Word文件转换为PDF* @param wordBytes Word文件的字节数组* @return PDF文件的字节数组*/public static byte[] convertWordToPdf(byte[] wordBytes) {byte[] pdfBytes = null;File tempWordFile = null;File tempPdfFile = null;try {// 创建临时文件来存储Word字节数组tempWordFile = File.createTempFile("temp", ".docx");try (FileOutputStream fos = new FileOutputStream(tempWordFile)) {fos.write(wordBytes);}// 创建临时文件来存储PDF字节数组tempPdfFile = File.createTempFile("temp", ".pdf");// 使用jodconverter进行转换DocumentConverter converter = org.jodconverter.local.LocalConverter.make(officeManager);converter.convert(tempWordFile).to(tempPdfFile).execute();// 读取PDF文件到字节数组pdfBytes = FileUtils.readFileToByteArray(tempPdfFile);} catch (Exception e) {e.printStackTrace();} finally {// 删除临时文件if (tempWordFile != null && tempWordFile.exists()) {tempWordFile.delete();}if (tempPdfFile != null && tempPdfFile.exists()) {tempPdfFile.delete();}}return pdfBytes;}/*** 下载PDF文件* @param path* @param taskForm 填充内容* @param response HttpServletResponse对象* @param fileName 文件名*/public static void downloadPdf(String path, Map<String, Object> taskForm, HttpServletResponse response, String fileName) {byte[] wordBytes = getWord(path, taskForm);if (wordBytes != null) {byte[] pdfBytes = convertWordToPdf(wordBytes);if (pdfBytes != null) {try {response.setContentType("application/pdf");// URL 编码文件名String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());// 设置 Content-Disposition 头response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName);response.getOutputStream().write(pdfBytes);response.getOutputStream().flush();} catch (IOException e) {e.printStackTrace();}}}}// 调试插件是否运行正常public static void main(String[] args) {// 输入的Word文件路径File inputFile = new File("C:\\Users\\PHJ\\OneDrive\\Desktop\\开发部门_2025年第13周_周报 (2).docx");// 输出的PDF文件路径File outputFile = new File("C:\\Users\\PHJ\\OneDrive\\Desktop\\开发部门_2025年第13周_周报11 (2).pdf");// 创建一个本地Office管理器实例OfficeManager officeManager = LocalOfficeManager.builder().portNumbers(2002).build();try {// 启动Office管理器officeManager.start();// 创建一个文档转换器实例DocumentConverter converter = org.jodconverter.local.LocalConverter.make(officeManager);// 执行转换操作converter.convert(inputFile).to(outputFile).execute();System.out.println("文档转换成功!");} catch (OfficeException e) {System.err.println("文档转换失败: " + e.getMessage());} finally {try {// 停止Office管理器officeManager.stop();} catch (OfficeException e) {System.err.println("停止Office管理器失败: " + e.getMessage());}}}
}

工具类调用示例

Map<String, Object> taskForm = new HashMap<>();
String signPicture = "https://uy.wzznft.com/i/2025/04/29/h9ttbw.png";
PictureRenderData picture = Pictures.ofUrl(signPicture, PictureType.PNG).size(50, 25).create();//网络图片地址
//PictureRenderData picture = Pictures.ofLocal(signPicture).size(10, 20).create();//本地图片地址
taskForm.put("测试", "测试");
taskForm.put("signPicture", picture);WordFileUtil.downloadPdf("template/word模板.docx", taskForm, response,ObjectUtil.isNotEmpty(dept) ? dept.getFullName() + "_" + deptReport.getReportDate() +"_周报.pdf" : "周报.pdf");
//        WordFileUtil.downloadWord("template/word模板.docx", taskForm, response,ObjectUtil.isNotEmpty(dept) ? dept.getFullName() + "_" + deptReport.getReportDate() +"_周报.docx" : "周报.docx");
http://www.dtcms.com/wzjs/384325.html

相关文章:

  • 服务行业网站建设app开发用什么软件
  • 重庆忠县网站建设公司常见的网络营销手段
  • 学校网站建设项目可行性分析报告引流推广软件
  • 专门做ppt的网站产品怎么做推广和宣传
  • 京东怎么做不同网站同步登陆的青岛百度推广优化怎么做的
  • 营销型网站是什么百度搜索排行榜风云榜
  • 海口网站自助建站发软文是什么意思
  • 做百度网站优化多少钱廊坊seo
  • 锦州做网站的个人昆明网络营销公司哪家比较好
  • 前端页面设计网站易观数据app排行
  • 网站竞品拦截广告怎么做搜索引擎优化中的步骤包括
  • 卢湾专业网站优化公司厦门seo排名公司
  • 在线分析网站深圳网络营销软件
  • 做网站上传照片的尺寸seo关键词推广怎么做
  • 专门做诺丽果的网站手游推广代理平台有哪些
  • 移动端网站欣赏提高工作效率图片
  • 做网站要多少钱汉狮seo云优化外包
  • 做炫光素材的网站友好链接
  • ico加网站各网站收录
  • 双流区规划局建设局网站2023新闻大事件摘抄
  • 营销型网站重要特点是?百度有什么办法刷排名
  • window2008 网站建设北京做网站推广
  • 做外贸怎么看外国网站自己如何做链接推广
  • 怎样自己做卖商品的网站百度推广登录平台网址
  • 徐家汇网站建设2022年国际十大新闻
  • 装修公司网站用的织梦网络营销的优势与不足
  • 网站建设合同缴印花税竞价排名什么意思
  • 工业设计网站哪个最教育培训网站设计
  • 10有免费建网站西地那非片能延时多久
  • 深圳专业建网站多少钱制作公司网站的公司