Commit 9586d22d authored by 盖献康's avatar 盖献康

检验报告 - PDF

parent 6e3f4bce
...@@ -17,6 +17,7 @@ import com.ruoyi.system.mapper.SysDictDataMapper; ...@@ -17,6 +17,7 @@ import com.ruoyi.system.mapper.SysDictDataMapper;
import io.minio.MinioClient; import io.minio.MinioClient;
import io.minio.ObjectWriteArgs; import io.minio.ObjectWriteArgs;
import io.minio.PutObjectArgs; import io.minio.PutObjectArgs;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -24,8 +25,11 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -24,8 +25,11 @@ 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.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
...@@ -64,12 +68,12 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe ...@@ -64,12 +68,12 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
* @param taskId * @param taskId
* @return * @return
*/ */
@SneakyThrows
@Override @Override
public String generateInspectionReport(Long taskId) throws Exception { public String generateInspectionReport(Long taskId) {
PdfReader reader = null; PdfReader reader = null;
PdfStamper ps = null; PdfStamper ps = null;
ByteArrayOutputStream bos = null; ByteArrayOutputStream bos = null;
try {
// 模板绝对路径--服务器 // 模板绝对路径--服务器
String fileName = "/template/receipt_template_03_27.pdf"; String fileName = "/template/receipt_template_03_27.pdf";
// 读取现有模板内容 // 读取现有模板内容
...@@ -80,35 +84,56 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe ...@@ -80,35 +84,56 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
ps = new PdfStamper(reader, bos); ps = new PdfStamper(reader, bos);
// 获取表单所有元素 // 获取表单所有元素
AcroFields fields = ps.getAcroFields(); AcroFields fields = ps.getAcroFields();
// 设置具体字体格式的编码, 不设置的话中文可能不会显示 // 设置具体字体格式的编码, 不设置的话中文可能不会显示
BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
fields.addSubstitutionFont(bf); fields.addSubstitutionFont(bf);
// 测试数据 // 测试数据
InspectionReportPdfVO inspectionReportPdfVO = requireData(taskId); InspectionReportPdfVO inspectionReportPdfVO = requireData(taskId);
// 渲染数据 // 渲染数据
renderData(fields, inspectionReportPdfVO, ps); renderData(fields, inspectionReportPdfVO, ps);
// 必须要调用这个,否则文档不会生成的
//必须要调用这个,否则文档不会生成的
ps.setFormFlattening(true); ps.setFormFlattening(true);
// 关闭
ps.close(); ps.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if (bos != null){
bos.close(); bos.close();
}
if(reader != null){
reader.close(); reader.close();
} // // 把第一个模板填入数据后生成一个oss
} // String templateName = uploadMinio(bos, "整车信息安全检验临时模板");
assert bos != null; // // 创建一个空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()); return uploadMinio(bos, "整车信息安全检验报告-" + getReportName());
} }
/**
* minio对象存储查看图片地址
* @return
*/
private String minioUrl() {
return minioEndpoint + "/" + bucketName + "/";
}
private String getReportName() { private String getReportName() {
return DateUtils.parseDateToStr("yyyyMMddHHmm", new Date()); return DateUtils.parseDateToStr("yyyyMMddHHmm", new Date());
} }
......
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