Commit c45034f8 authored by 盖献康's avatar 盖献康

Merge branch 'gaixiankang' into 'dev'

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

See merge request !242
parents 62b113b8 ed4c8a5a
...@@ -165,4 +165,9 @@ public class InspectionReportPdfVO { ...@@ -165,4 +165,9 @@ public class InspectionReportPdfVO {
*/ */
private String brandImage; private String brandImage;
/**
* 样品照片
*/
private String samplePhotos;
} }
...@@ -25,11 +25,9 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -25,11 +25,9 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.file.Files; import java.net.URL;
import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
...@@ -85,7 +83,7 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe ...@@ -85,7 +83,7 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
// 获取表单所有元素 // 获取表单所有元素
AcroFields fields = ps.getAcroFields(); 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); fields.addSubstitutionFont(bf);
// 测试数据 // 测试数据
InspectionReportPdfVO inspectionReportPdfVO = requireData(taskId); InspectionReportPdfVO inspectionReportPdfVO = requireData(taskId);
...@@ -97,33 +95,83 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe ...@@ -97,33 +95,83 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
ps.close(); ps.close();
bos.close(); bos.close();
reader.close(); reader.close();
// // 把第一个模板填入数据后生成一个oss
// String templateName = uploadMinio(bos, "整车信息安全检验临时模板"); // 把第一个模板填入数据后生成一个oss
// // 创建一个空PDF, 融合两个PDF String templateName = uploadMinio(bos, "整车信息安全检验临时模板");
// Document document = new Document(); // 创建一个空PDF, 融合两个PDF
// ByteArrayOutputStream out = new ByteArrayOutputStream(); Document document = new Document();
// PdfCopy copy = new PdfCopy(document, out); ByteArrayOutputStream allOut = new ByteArrayOutputStream();
// document.open(); PdfCopy copy = new PdfCopy(document, allOut);
// // 第一个PDF document.open();
// PdfReader readerFirst = new PdfReader(minioUrl() + templateName); // 第一个PDF
// int numberPage = readerFirst.getNumberOfPages(); PdfReader templateReader = new PdfReader(minioUrl() + templateName);
// for (int i = 1;i < numberPage;i++) { int numberPage = templateReader.getNumberOfPages();
// PdfImportedPage importedPage = copy.getImportedPage(readerFirst, i); for (int i = 1;i < numberPage;i++) {
// copy.addPage(importedPage); PdfImportedPage importedPage = copy.getImportedPage(templateReader, i);
// } copy.addPage(importedPage);
// // 第二个PDF }
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // 第二个PDF
// PdfBaseWriter writer = new PdfBaseWriter(outputStream); String codeName = getInspectionReportCode(inspectionReportPdfVO);
// writer.open(); PdfReader codeReader = new PdfReader(minioUrl() + codeName);
// // 代码渲染 for (int i = 1; i <= codeReader.getNumberOfPages(); i++) {
// writer.setParagraph("企业留档文件", 30f, Element.ALIGN_CENTER, 0,15f, 180f); copy.addPage(copy.getImportedPage(codeReader, i));
// writer.close(); }
// String codeName = uploadMinio(outputStream, "整车信息安全检验临时代码"); document.close();
// PdfReader pr = new PdfReader(minioUrl() + codeName);
// copy.addPage(copy.getImportedPage(pr, 1)); return uploadMinio(allOut, "整车信息安全检验报告-" + getReportName());
// document.close(); }
return uploadMinio(bos, "整车信息安全检验报告-" + 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 @@ ...@@ -319,7 +319,8 @@
GROUP_CONCAT(distinct s.identification_code) AS vehicleIdentificationNumber, GROUP_CONCAT(distinct s.identification_code) AS vehicleIdentificationNumber,
t.task_list AS inspectionItem, t.task_list AS inspectionItem,
DATE_FORMAT(t.create_time, '%Y-%m-%d') AS productionDate, 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 from
t_task t t_task t
left join t_task_sample_relation tsr on t.id = tsr.task_id left join t_task_sample_relation tsr on t.id = tsr.task_id
......
...@@ -20,14 +20,19 @@ import java.util.regex.Pattern; ...@@ -20,14 +20,19 @@ import java.util.regex.Pattern;
public class PdfBaseWriter extends Document{ 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