生成PDF并上传到图片服务器
解决思路
在application.yml文件中配置三个文件地址:分别是文字模版地址、文件模版地址、目标生成地址
pom.xml
<!--生成PDF--><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.6</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>
application.yml
pdf:fontPath: D:\***\***\src\main\resources\static\common\pdf\font\font1.ttftemplatePath: D:\***\***\src\main\resources\static\common\pdf\doc\1111.pdftargetPath: D:\tools\
需要在项目部署服务器上创建文件夹并将这两个模版文件放进去,如果POM文件有过滤.ttf文件的,将其注释,最后将服务器返回的储存地址保存到相应字段。
模板文件需要用到专业的PDF编辑工具,所需的文件也可以直接去我的资源里下载
PDF生成工具类
package com.***.***.***.controller.pdf;import cn.afterturn.easypoi.pdf.PdfExportUtil;
import cn.afterturn.easypoi.pdf.entity.PdfExportParams;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.***.***.***.dao.model.AgreementEntity;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class PdfImageGenerate {public static final String PDF_DATA_MAP = "PDF_DATA_MAP";public static final String PDF_IMG_MAP = "PDF_IMG_MAP";//生成Excel表格型PDFpublic static File savePdfImg(String id) {try {List<AgreementEntity> studentList = new ArrayList<AgreementEntity>();AgreementEntity agreementEntity = new AgreementEntity();String fileName="common/pdf/doc/"+id+".pdf";agreementEntity.setName("团队绑定协议");agreementEntity.setImage("static/common/img/500.png");studentList.add(agreementEntity);PdfExportParams params = new PdfExportParams("协议");File file = new File(fileName);file.createNewFile();PdfExportUtil.exportPdf(params, AgreementEntity.class, studentList, new FileOutputStream(file));return file;} catch (Exception e) {e.printStackTrace();}return null;}// 利用模板生成pdfpublic static void pdfout(String fontPath, Map<String, Object> o, String templatePath, String destPath) {PdfReader reader;FileOutputStream out;ByteArrayOutputStream bos;PdfStamper stamper;try {
// BaseFont bf = BaseFont.createFont("common/pdf/font/font1.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);out = new FileOutputStream(destPath);reader = new PdfReader(templatePath);bos = new ByteArrayOutputStream();stamper = new PdfStamper(reader, bos);AcroFields form = stamper.getAcroFields();//文字类的内容处理Map<String, String> datemap = (Map<String, String>) o.get(PDF_DATA_MAP);form.addSubstitutionFont(bf);for (String key : datemap.keySet()) {String value = datemap.get(key);form.setField(key, value);}//图片类的内容处理Map<String, String> imgmap = (Map<String, String>) o.get(PDF_IMG_MAP);for (String key : imgmap.keySet()) {String value = imgmap.get(key);String imgpath = value;int pageNo = form.getFieldPositions(key).get(0).page;Rectangle signRect = form.getFieldPositions(key).get(0).position;float x = signRect.getLeft();float y = signRect.getBottom();//根据路径读取图片Image image = Image.getInstance(imgpath);//获取图片页面PdfContentByte under = stamper.getOverContent(pageNo);//图片大小自适应image.scaleToFit(signRect.getWidth(), signRect.getHeight());//添加图片image.setAbsolutePosition(x, y);under.addImage(image);}stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑stamper.close();Document doc = new Document();Font font = new Font(bf, 32);PdfCopy copy = new PdfCopy(doc, out);doc.open();PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);copy.addPage(importPage);doc.close();} catch (IOException e) {System.out.println(e);} catch (DocumentException e) {System.out.println(e);}}
}
调用时
第一步调用PDF生成方法
@Value("${pdf.fontPath}")private String fontPath;//文字模版地址@Value("${pdf.templatePath}")private String templatePath;//文件模版地址@Value("${pdf.targetPath}")//目标地址private String targetPath;...
String destPath = targetPath+id+".pdf";//目标地址Map<String, String> map = new HashMap();map.put("fill_1", "张三");map.put("fill_2", "李四");map.put("fill_3", "男");map.put("fill_4", "这里是住址信息");Map<String, String> map2 = new HashMap();map2.put("image", "http://106.12.157.128:89/login/common/img/fw.png");Map<String, Object> o = new HashMap();o.put(PdfImageGenerate.PDF_DATA_MAP, map);o.put(PdfImageGenerate.PDF_IMG_MAP, map2);PdfImageGenerate.pdfout(fontPath,o, templatePath, destPath);
第二部 将生成的PDF文件从生成目标地址上传到目标服务器
FileSystemResource resource = new FileSystemResource(new File(destPath));MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();param.add("file", resource);param.add("system", "lab");RestTemplate restTemplate = new RestTemplate();HttpHeaders headers = new HttpHeaders();HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(param, headers);String recv = restTemplate.postForObject(uploadUrl, files, String.class);
第三部 将服务器返回的文件保存地址取出并保存到指定表
JSONObject jsonObject = JSONObject.parseObject(recv);JSONObject data = jsonObject.getJSONObject("data");JSONArray fileList = data.getJSONArray("fileList");String filePath = "";if (fileList.size() > 0) {JSONObject object = JSONObject.parseObject(fileList.get(0).toString());filePath = object.getString("filePath");}info.put("treatyUrl", filePath);int totls = teamLabShareService.saveTreatyUrl(info);
生成PDF并上传到图片服务器相关推荐
- java 图片服务器 上传_Java实现把图片上传到图片服务器(nginx+vsftp)
前言: 在我另一篇笔记中已经记载了如何用nginx + vsftp搭建图片服务器(请参考nginx + vsftp搭建图片服务器),并且用vsftp的客户端工具filezilla测试过已经可用.但是在 ...
- Java实现把图片上传到图片服务器(nginx+vsftp)
在我另一篇笔记中已经记载了如何用nginx + vsftp搭建图片服务器,并且用vsftp的客户端工具filezilla测试过已经可用.但是在开发中应该是把用户在前端页面提交的图片保存到图片服务器中, ...
- java分布式实现图片上传到图片服务器
java分布式实现图片上传到图片服务器 操作步骤 第一步 第二步 第三步 第四步 第五步 第六步 代码实现 第七步 JS代码 大功告成!! 操作步骤 第一步 在页面中的form表单里面增加一个inpu ...
- php 怎么上传图片,php怎么把图片上传到图片服务器
php怎么把图片上传到图片服务器? 图片服务器代码<?php /** * 图片服务器上传API接口 * by Zx * date 2016-04-28 */ header('Content-ty ...
- 服务器搭建微信编辑器,ueditor编辑器实现微信上传和图片服务器上传
如题. 前端部分: var ue = UE.getEditor('container', { serverUrl:'{pigcms::$f_siteUrl}<?php echo U(" ...
- 将本地图片或者网上图片用post方式上传到图片服务器
一个简单的应用 Java代码 package com.bj95ol.test; import java.io.BufferedReader; import java.io.File; import ...
- asp.net ftp上传文件到服务器,.net 文件上传到服务器上
详解 Linux 下 SSH 远程文件传输命令 scp 3.将本地文件上传到服务器上 scp-P 2222/home/lnmp0.4.tar.gz root@www.vpser.net:/root/l ...
- 上传商品图片——淘淘商城(九)
需求 分析 总结 Service层 Controller层 需求 分析 通过底部的js,调用common.js中的方法初始化图片上传器. js中初始化图片上传器,参数为TT.kingEditorPar ...
- js文件夹上传到服务器,js 上传img到服务器
jQuery插件之ajaxFileUpload 前端视图,HTML与JS代码,成功上传后,返回图片真实地址并绑定到的SRC地址lt;head>lt;script src="/jquer ...
- 无水印上传本地图片到网络获取地址+小书匠使用 +github和 七牛云 图床绑定--菜鸟小回
无水印上传本地图片到网络获取地址+小书匠使用 +github和 七牛云 图床绑定 简介: 最近开始写博客,缺少一个Markdown编辑器.寻找了两三天终于找到了这款<小书匠>,可复制粘图, ...
最新文章
- iOS开发笔记 8、真机调试和发布软件
- 算法笔记_120:蓝桥杯第六届省赛(Java语言B组部分习题)试题解答
- js中for-in的坑
- 来,带你鸟瞰 Java 中的并发框架!
- [设计原则与模式] 如何理解TDD的三条规则
- a5d27 emmc启动 修改2
- (79)FPGA复位激励(initial)
- opend和open的区别_open与open up的区别
- c++学习笔记(7) 面向对象思想
- 别再被三次握手和四次挥手所支配!把TCP这玩意儿给你掰开了说
- 瑞芯微开发工具怎么刷机
- [收藏】正确使用SqlConnection对象,兼谈数据库连接池
- Oracle 重置过期密码,Oracle重置过期的密码
- 2022年G3锅炉水处理考试模拟100题及答案
- Oralce性能优化-绑定变量窥视
- java程序设计实用教程_清华大学出版社-图书详情-《Java程序设计实用教程》
- smartgwt (B)
- 使用wordpress搭建个人网站保姆级教程
- 博客推荐系列第一篇:我收藏的BLOG分类
- 2022全新老照片修复小程序/AI图片处理小程序