根据PDF模板生成文件和拼接产品的数据
/**
-
根据PDF模板生成PDF文件
-
@return
*/
@GetMapping(“generatePdf”)
public String generatePdf() throws Exception{
// File file = ResourceUtils.getFile(“classpath:”+SAVE_PATH);
File pdfFile = new File(ResourceUtils.getURL(“classpath:”).getPath()+SAVE_PATH);
try {
PdfReader pdfReader;
PdfStamper pdfStamper;
ByteArrayOutputStream baos;
Document document = new Document();
//
PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document,
new FileOutputStream(pdfFile));
document.open();
File file = ResourceUtils.getFile(“classpath:”+templatePath);
pdfReader = new PdfReader(file.getPath());
int n = pdfReader.getNumberOfPages();
log.info(“页数:”+n);
baos = new ByteArrayOutputStream();
pdfStamper = new PdfStamper(pdfReader, baos);
for(int i = 1; i <= n; i++) {
AcroFields acroFields = pdfStamper.getAcroFields();
//key statement 1
acroFields.setGenerateAppearances(true);
//acroFields.setExtraMargin(5, 5);
acroFields.setField(“customerAddress”, “上海市浦东新区田子路520弄1号楼”);
acroFields.setField(“customerCompanyName”, “上海百度有限公司”);
acroFields.setField(“customerName”, “张三”);
acroFields.setField(“customerPhone”, “15216667777”);
acroFields.setField(“customerMail”, “123456789@sian.com”);
acroFields.setField(“vendorAddress”, “上海市浦东新区瑟瑟发抖路182号”);
acroFields.setField(“vendorCompanyName”, “牧小农科技技术有限公司”);
acroFields.setField(“vendorName”, “王五”);
acroFields.setField(“vendorPhone”, “15688886666”);
acroFields.setField(“vendorMail”, “123567@qq.com”);
acroFields.setField(“effectiveStartTime”, “2021年05月25”);
acroFields.setField(“effectiveEndTime”, “2022年05月25”);
//true代表生成的PDF文件不可编辑
pdfStamper.setFormFlattening(true);
pdfStamper.close();
pdfReader = new PdfReader(baos.toByteArray());
pdfSmartCopy.addPage(pdfSmartCopy.getImportedPage(pdfReader, i));
pdfSmartCopy.freeReader(pdfReader);
pdfReader.close();
}
pdfReader.close();
document.close();
} catch(DocumentException dex) {
dex.printStackTrace();
} catch(IOException ex) {
ex.printStackTrace();
}
//创建PDF文件
createPdf();
File file3 = new File(ResourceUtils.getURL(“classpath:”).getPath()+TEMP_PATH);
File file1 = new File(ResourceUtils.getURL(“classpath:”).getPath()+outputFileName);
List files = new ArrayList<>();
files.add(pdfFile);
files.add(file3);
try {
PdfUtil pdfUtil = new PdfUtil();
pdfUtil.mergeFileToPDF(files,file1);
} catch (Exception e) {
e.printStackTrace();
}
//如果你是上传文件服务器上,这里可以上传文件
// String url = fileServer.uploadPdf(File2byte(file1));
//删除总文件
//如果是你本地预览就不要删除了,删了就看不到了
// if(file1.exists()){
// file1.delete();
// }
//删除模板文件
if(pdfFile.exists()){
System.gc();
pdfFile.delete();
}
//删除产品文件
if(file3.exists()){
file3.delete();
}
return “success”;
}
创建PDF附件信息拼接到主文件中:
/**
- 创建PDF附件信息
*/
public static void createPdf() {
Document doc = null;
try {
doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream(ResourceUtils.getURL(“classpath:”).getPath()+TEMP_PATH));
doc.open();
BaseFont bfChi = BaseFont.createFont(“STSong-Light”,“UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);
Font fontChi = new Font(bfChi, 8, Font.NORMAL);
PdfPTable table = new PdfPTable(5);
Font fontTitle = new Font(bfChi, 15, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Paragraph(“货运运输服务协议-附件1 运输费用报价”,fontTitle));
cell.setColspan(5);
table.addCell(cell);
// “序号”
table.addCell(new Paragraph(“序号”,fontChi));
table.addCell(new Paragraph(“品类”,fontChi));
table.addCell(new Paragraph(“名称”,fontChi));
table.addCell(new Paragraph(“计算方式”,fontChi));
table.addCell(new Paragraph(“费率”,fontChi));
table.addCell(new Paragraph(“1”,fontChi));
table.addCell(new Paragraph(“货运”,fontChi));
table.addCell(new Paragraph(“费率1.0”,fontChi));
table.addCell(new Paragraph(“算”,fontChi));
table.addCell(new Paragraph(“0~100万-5.7%,上限:500元,下限:20元”,fontChi));
table.addCell(new Paragraph(“2”,fontChi));
table.addCell(new Paragraph(“货运”,fontChi));
table.addCell(new Paragraph(“费率1.0”,fontChi));
table.addCell(new Paragraph(“倒”,fontChi));
table.addCell(new Paragraph(“100万~200万-5.6%,无上限、下限”,fontChi));
table.addCell(new Paragraph(“3”,fontChi));
table.addCell(new Paragraph(“货运”,fontChi));
table.addCell(new Paragraph(“费率1.0”,fontChi));
table.addCell(new Paragraph(“算”,fontChi));
table.addCell(new Paragraph(“200万~300万-5.5%,无上限、下限”,fontChi));
doc.add(table);
// doc.add(new Paragraph(“Hello World,看看中文支持不…aaaaaaaaaaaaaaaaa”,fontChi));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
doc.close();
}
}
合同签署:
/**
-
签署合同
-
@return
-
@throws IOException
-
@throws DocumentException
*/
@GetMapping(“addContent”)
public String addContent() throws IOException, DocumentException {
BaseFont baseFont = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
//这里可以填写本地地址,也可以是服务器上的文件地址
PdfReader reader = new PdfReader(ResourceUtils.getURL(“classpath:”).getPath()+outputFileName);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(ResourceUtils.getURL(“classpath:”).getPath()+endPdf));
//
PdfContentByte over = stamper.getOverContent(1);
ColumnText columnText = new ColumnText(over);
PdfContentByte over1 = stamper.getOverContent(1);
ColumnText columnText1 = new ColumnText(over1);
PdfContentByte over2 = stamper.getOverContent(1);
ColumnText columnText2 = new ColumnText(over2);
PdfContentByte over3 = stamper.getOverContent(1);
ColumnText columnText3 = new ColumnText(over3);
// llx 和 urx 最小的值决定离左边的距离. lly 和 ury 最大的值决定离下边的距离
// llx 左对齐
// lly 上对齐
// urx 宽带
// ury 高度
columnText.setSimpleColumn(29, 117, 221, 16);
Paragraph elements = new Paragraph(0, new Chunk(“上海壹站供应链有限公司”));
columnText1.setSimpleColumn(26, 75, 221, 16);
Paragraph elements1 = new Paragraph(0, new Chunk(“2021年03月03日”));
columnText2.setSimpleColumn(800, 120, 200, 16);
Paragraph elements2 = new Paragraph(0, new Chunk(“壹汇(江苏)供应链管理有限公司芜湖分公司”));
columnText3.setSimpleColumn(800, 74, 181, 16);
Paragraph elements3 = new Paragraph(0, new Chunk(“2022年03月03日”));
// acroFields.setField(“customerSigntime”, “2021年03月03日”);
// acroFields.setField(“vendorSigntime”, “2021年03月09日”);
// 设置字体,如果不设置添加的中文将无法显示
elements.setFont(font);
columnText.addElement(elements);
columnText.go();
elements1.setFont(font);
columnText1.addElement(elements1);
columnText1.go();
elements2.setFont(font);
columnText2.addElement(elements2);
columnText2.go();
elements3.setFont(font);
columnText3.addElement(elements3);
columnText3.go();
stamper.close();
File tempFile = new File(ResourceUtils.getURL(“classpath:”).getPath()+“签署测试.pdf”);
//如果是你要上传到服务器上,填写服务器的地址
// String url = fileServer.uploadPdf(File2byte(tempFile));
// log.info(“url:”+url);
//如果是上传服务器后,要删除信息
//本地不要删除,否则没有文件
// if(tempFile.exists()){
// tempFile.delete();
// }
return “success”;
}
PDF工具类:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.List;
/***
-
pdf 相关操作
-
@author mxn
*/
@Slf4j
@Component
public class PdfUtil {
/**
-
合并PDF文件
-
@param files 文件列表
-
@param output 输出的PDF文件
*/
public void mergeFileToPDF(List files, File output) {
Document document = null;
PdfCopy copy = null;
OutputStream os = null;
try {
os = new FileOutputStream(output);
document = new Document();
copy = new PdfCopy(document, os);
document.open();
for (File file : files) {
if (!file.exists()) {
continue;
}
String fileName = file.getName();
if (fileName.endsWith(“.pdf”)) {
PdfContentByte cb = copy.getDirectContent();
PdfOutline root = cb.getRootOutline();
new PdfOutline(root, new PdfDestination(PdfDestination.XYZ), fileName
.substring(0, fileName.lastIndexOf(“.”)));
// 不使用reader来维护文件,否则删除不掉文件,一直被占用
try (InputStream is = new FileInputStream(file)) {
PdfReader reader = new PdfReader(is);
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
} catch(Exception e) {
log.warn(“error to close file : {}” + file.getCanonicalPath(), e);
// e.printStackTrace();
}
} else {
log.warn(“file may not be merged to pdf. name:” + file.getCanonicalPath());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
document.close();
}
if (copy != null) {
copy.close();
}
if (os != null) {
IOUtils.closeQuietly(os);
}
}
}
/**
-
将文件转换成byte数组
-
@param file
-
@return
-
**/
public static byte[] File2byte(File file){
byte[] buffer = null;
try {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。
这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来
目录:
部分内容截图:
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!**
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。
这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来
目录:
[外链图片转存中…(img-21wa5TV6-1713423573686)]
部分内容截图:
[外链图片转存中…(img-u0yVKz0B-1713423573686)]
[外链图片转存中…(img-IKSpWV7W-1713423573686)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!