Commit 5301a6e6 authored by 盖献康's avatar 盖献康

检验报告PDF - 模板+代码渲染

parent facde725
......@@ -165,4 +165,9 @@ public class InspectionReportPdfVO {
*/
private String brandImage;
/**
* 样品照片
*/
private String samplePhotos;
}
......@@ -25,11 +25,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.net.URL;
import java.util.*;
import java.util.List;
......@@ -85,7 +83,7 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
// 获取表单所有元素
AcroFields fields = ps.getAcroFields();
// 设置具体字体格式的编码, 不设置的话中文可能不会显示
BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
BaseFont bf = BaseFont.createFont("/fonts/STSong.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fields.addSubstitutionFont(bf);
// 测试数据
InspectionReportPdfVO inspectionReportPdfVO = requireData(taskId);
......@@ -97,33 +95,83 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
ps.close();
bos.close();
reader.close();
// // 把第一个模板填入数据后生成一个oss
// String templateName = uploadMinio(bos, "整车信息安全检验临时模板");
// // 创建一个空PDF, 融合两个PDF
// Document document = new Document();
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// PdfCopy copy = new PdfCopy(document, out);
// document.open();
// // 第一个PDF
// PdfReader readerFirst = new PdfReader(minioUrl() + templateName);
// int numberPage = readerFirst.getNumberOfPages();
// for (int i = 1;i < numberPage;i++) {
// PdfImportedPage importedPage = copy.getImportedPage(readerFirst, i);
// copy.addPage(importedPage);
// }
// // 第二个PDF
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// PdfBaseWriter writer = new PdfBaseWriter(outputStream);
// writer.open();
// // 代码渲染
// writer.setParagraph("企业留档文件", 30f, Element.ALIGN_CENTER, 0,15f, 180f);
// writer.close();
// String codeName = uploadMinio(outputStream, "整车信息安全检验临时代码");
// PdfReader pr = new PdfReader(minioUrl() + codeName);
// copy.addPage(copy.getImportedPage(pr, 1));
// document.close();
return uploadMinio(bos, "整车信息安全检验报告-" + getReportName());
// 把第一个模板填入数据后生成一个oss
String templateName = uploadMinio(bos, "整车信息安全检验临时模板");
// 创建一个空PDF, 融合两个PDF
Document document = new Document();
ByteArrayOutputStream allOut = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(document, allOut);
document.open();
// 第一个PDF
PdfReader templateReader = new PdfReader(minioUrl() + templateName);
int numberPage = templateReader.getNumberOfPages();
for (int i = 1;i < numberPage;i++) {
PdfImportedPage importedPage = copy.getImportedPage(templateReader, i);
copy.addPage(importedPage);
}
// 第二个PDF
String codeName = getInspectionReportCode(inspectionReportPdfVO);
PdfReader codeReader = new PdfReader(minioUrl() + codeName);
for (int i = 1; i <= codeReader.getNumberOfPages(); i++) {
copy.addPage(copy.getImportedPage(codeReader, i));
}
document.close();
return uploadMinio(allOut, "整车信息安全检验报告-" + getReportName());
}
/**
* 检验报告 - 代码渲染部分
* @param data
* @return
*/
@SneakyThrows
private String getInspectionReportCode(InspectionReportPdfVO data) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfBaseWriter document = new PdfBaseWriter(outputStream);
document.open();
// 代码渲染
document.setParagraph("报告编号:" + data.getReportNumber(), 10.5f, Element.ALIGN_RIGHT, 0,10f, 0f);
document.setParagraph("检 验 报 告", 22f, Element.ALIGN_CENTER, 0,0f, 0f);
PdfPTable signTable = new PdfPTable(new float[]{65, 20});
signTable.setSpacingBefore(5);
signTable.setWidthPercentage(85);
document.writeBottomCell("共 10 页", Element.ALIGN_RIGHT, 1, 1, signTable).setRightIndent(30f);
document.writeBottomCell("第 10 页", Element.ALIGN_RIGHT, 1, 1, signTable);
document.addContent(signTable);
document.setParagraph("7.附录", 10.5f, Element.ALIGN_LEFT, 45,1f, 0f);
document.setParagraph("图 1 样品照片", 10.5f, Element.ALIGN_CENTER, 0,20f, 0f);
// 添加内容到新页面
PdfPTable pictureTable = new PdfPTable(new float[]{25, 25, 25, 25});
pictureTable.setWidthPercentage(90);
pictureTable.setSpacingBefore(10);
List<String> pictureList = Arrays.asList(data.getSamplePhotos().split(","));
int result = pictureList.size();
while (result % 4 != 0) {
result++;
}
for (int i = 0; i < result; i++) {
PdfPCell cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
Optional<String> optionalElement = i < pictureList.size()
? Optional.of(pictureList.get(i))
: Optional.empty();
if (optionalElement.isPresent()) {
Image image = Image.getInstance(new URL(minioEndpoint + optionalElement.get()));
image.scaleAbsolute(100, 100);
cell.addElement(image);
} else {
Paragraph paragraph = new Paragraph();
cell.addElement(paragraph);
}
pictureTable.addCell(cell);
}
document.addContent(pictureTable);
document.setParagraph("图 2 试验照片", 10.5f, Element.ALIGN_CENTER, 0,20f, 0f);
// 关闭PDF
document.close();
return uploadMinio(outputStream, "整车信息安全检验临时代码");
}
/**
......
......@@ -319,7 +319,8 @@
GROUP_CONCAT(distinct s.identification_code) AS vehicleIdentificationNumber,
t.task_list AS inspectionItem,
DATE_FORMAT(t.create_time, '%Y-%m-%d') AS productionDate,
GROUP_CONCAT(distinct s.trademark) AS brandImage
GROUP_CONCAT(distinct s.trademark) AS brandImage,
GROUP_CONCAT(distinct s.sample_photos) AS samplePhotos
from
t_task t
left join t_task_sample_relation tsr on t.id = tsr.task_id
......
......@@ -20,14 +20,19 @@ import java.util.regex.Pattern;
public class PdfBaseWriter extends Document{
// ---------------------------------默认参数---------------------------------
// public final String FONT_NAME_ST_SONG_LIGHT = "STSongStd-Light";
// public final String FONT_ENCODING_UNI_GB_UCS2_H = "UniGB-UCS2-H";
/**
* 中文字体
*/
public final String FONT_NAME_ST_SONG_LIGHT = "STSongStd-Light";
public final String FONT_NAME_ST_SONG_LIGHT = "/fonts/STSong.TTF";
/**
* 中文字体编码
*/
public final String FONT_ENCODING_UNI_GB_UCS2_H = "UniGB-UCS2-H";
public final String FONT_ENCODING_UNI_GB_UCS2_H = BaseFont.IDENTITY_H;
//---------------------------------常用字体/颜色---------------------------------
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment