Commit 0c9b4960 authored by 王飞's avatar 王飞

Refactor

1、新增PDF-POC相关代码。

Reference N/A
parent 512bbc0e
......@@ -50,6 +50,18 @@
<artifactId>spring-test</artifactId>
</dependency>
<!-- 生成PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.ruoyi.pdf;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.pdf.report.ReportTableRow;
import java.util.List;
public class PDFUtils {
public static void drawTable(List<ReportTableRow> rows) {
}
private static void addTextToPdfCenter(AcroFields form, PdfStamper stamper, String text, String fieldName, BaseFont baseFont){
// 通过模板表单单元格名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(fieldName).get(0).page;
Rectangle signRect = form.getFieldPositions(fieldName).get(0).position;
// 获取操作的页面
PdfContentByte contentByte = stamper.getOverContent(pageNo);
//创建表单
PdfPTable table = new PdfPTable(1);
//获取当前模板表单宽度
float totalWidth = signRect.getRight() - signRect.getLeft() - 1;
//设置新表单宽度
table.setTotalWidth(totalWidth);
//设置中文格式
Font font = new Font(baseFont);
// 设置字号
font.setSize(10.56F);
//设置单元格格式
PdfPCell cell = new PdfPCell(new Phrase(text,font));
//设置单元格高度
cell.setFixedHeight(signRect.getTop()-signRect.getBottom()-1);
cell.setBorderWidth(0);
//设置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//设置水平居中
// cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// 设置文字在单元格中换行时的行间距 :最终行间距 :totalLeading= fixedLeading+fontSize(字体大小)* multipliedLeading;
cell.setLeading(0.0f,1.2f);
//添加到表单中
table.addCell(cell);
//写入pdf
table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), contentByte);
}
}
package com.ruoyi.pdf.report;
import cn.hutool.core.io.resource.ClassPathResource;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@Component
public class CreateReport {
public void create() throws IOException, DocumentException {
ClassPathResource classPathResource = new ClassPathResource("template/template.pdf");
PdfReader reader = new PdfReader(classPathResource.getStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
AcroFields acroFields = stamper.getAcroFields();
drawPage1("轿车", "SVW73025CK", "天津吉利汽车制造有限公司", acroFields);
stamper.setFormFlattening(true);
stamper.close();
bos.writeTo(new FileOutputStream("./aaa.pdf"));
}
/**
*
* @param productName 产品名称
* @param productModel 产品型号
* @param consignor 委托单位
*/
private void drawPage1(String productName, String productModel, String consignor, AcroFields acroFields) throws DocumentException, IOException {
acroFields.setField("productName", productName);
acroFields.setField("productModel", productModel);
acroFields.setField("consignor", consignor);
}
private void drawPage2() {
}
private void drawPage3() {
}
private void drawPage4() {
}
private void drawPage5() {
}
private void drawPage6() {
}
private void drawPage7() {
}
private void drawPage8() {
}
private void drawPage9() {
}
private void drawPage10() {
}
private void drawPage11() {
}
private void drawPage12() {
}
private void drawPage13() {
}
private void drawPage14() {
}
}
package com.ruoyi.pdf.report;
import lombok.Data;
@Data
public class ReportTableRow {
public String repTmpPlaceholder;
private String standardKey;
private String standardValue;
private String checkResultKey;
private String checkResultValue;
private String isPassKey;
private String isPassValue;
public String getStandardKey() {
return repTmpPlaceholder + "_standard";
}
public String getCheckResultKey() {
return repTmpPlaceholder + "_checkResult";
}
public String getIsPassKey() {
return repTmpPlaceholder + "_isPass";
}
}
......@@ -87,4 +87,13 @@ public class SystemReviewTaskController extends BaseController {
startPage(request);
return getDataTable(systemReviewTaskService.findListByStatus(SystemReviewTask.STATUS_FINISH));
}
@ApiOperation("生成PDF报告")
@Trace
@Tags({@Tag(key = "param", value = "arg[0]"), @Tag(key = "result", value = "returnedObj")})
@RequestMapping(method = RequestMethod.POST, value = "/exportPDF")
public R<SystemReviewTaskExportPDFResponse> exportPDF() {
return null;
}
}
package com.ruoyi.web.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
@Data
@Accessors(chain = true)
@ApiModel
public class SystemReviewTaskExportPDFResponse {
@ApiModelProperty("报告编号")
private String exportNo;
@ApiModelProperty("产品名称")
private String productName;
@ApiModelProperty("产品型号")
private String productModel;
@ApiModelProperty("委托单位")
private String entrustOrg;
@ApiModelProperty("委托单位电话")
private String entrustOrgPhone;
@ApiModelProperty("委托单位地址")
private String entrustOrgAddr;
@ApiModelProperty("委托单位邮编")
private String entrustOrgPostcode;
@ApiModelProperty("样品名称")
private String sampleName;
@ApiModelProperty("商标")
private String brand;
@ApiModelProperty("型号规格")
private String specification;
@ApiModelProperty("生产企业")
private String manufacturer;
@ApiModelProperty("送样者")
private String sampleSender;
@ApiModelProperty("送样日期")
private String sampleSendDate;
@ApiModelProperty("样品数量")
private String sampleCount;
@ApiModelProperty("任务编号")
private String taskNo;
@ApiModelProperty("汽车信息安全管理要求")
private List<CheckTable> safetyManagerReq;
@ApiModel
@Data
public static class CheckTable {
@ApiModelProperty("表名称")
private String tableName;
@ApiModelProperty("任务编号")
private Integer order;
@ApiModelProperty("标准要去")
private String standard;
@ApiModelProperty("检验结果")
private String checkResult;
@ApiModelProperty("审核是否通过(0未通过、1通过)")
private String pass;
public String getStandardKey() {
return tableName + "_" + order + "_standard";
}
public String getCheckResultKey() {
return tableName + "_" + order + "_checkResult";
}
public String getPassKey() {
return tableName + "_" + order + "_pass";
}
}
}
package com.ruoyi;
import cn.hutool.core.io.resource.ClassPathResource;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.ruoyi.pdf.report.CreateReport;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
public class PDFTest {
@Autowired
private CreateReport createReport;
@Test
public void export() throws IOException, DocumentException {
//设置具体字体格式编码,不设置的话中文可能会不显示
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ClassPathResource classPathResource = new ClassPathResource("template/template.pdf");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(classPathResource.getStream());
bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
Map<String, String> map = new HashMap();
map.put("system_review_01_standard", "车辆生产企业应建立车辆全生命周期的汽车信息安全管理体系。");
map.put("system_review_01_result", "车辆生产企业建立了车辆全生命周期的汽车信息安全管理体系。");
map.put("system_review_01_pass", "不符合");
this.fillPdfCellForm(map, form, stamper, baseFont);
// true代表生成的PDF文件不可编辑
stamper.setFormFlattening(true);
stamper.close();
bos.writeTo(new FileOutputStream("./aaa.pdf"));
}
private void fillPdfCellForm(Map<String, String> map, AcroFields form, PdfStamper stamper, BaseFont baseFont) throws IOException, DocumentException {
for (Map.Entry entry : map.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
// form.setField(key, value);
form.setFieldProperty(key, "setfflags",PdfFormField.FF_MULTILINE, null);
addTextToPdfCenter(form, stamper, value, key, baseFont);
}
}
private static void addTextToPdfCenter(AcroFields form, PdfStamper stamper, String text,String fieldName,BaseFont baseFont){
// 通过模板表单单元格名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(fieldName).get(0).page;
Rectangle signRect = form.getFieldPositions(fieldName).get(0).position;
// 获取操作的页面
PdfContentByte contentByte = stamper.getOverContent(pageNo);
//创建表单
PdfPTable table = new PdfPTable(1);
//获取当前模板表单宽度
float totalWidth = signRect.getRight() - signRect.getLeft() - 1;
//设置新表单宽度
table.setTotalWidth(totalWidth);
//设置中文格式
Font font = new Font(baseFont);
// 设置字号
font.setSize(10.56F);
//设置单元格格式
PdfPCell cell = new PdfPCell(new Phrase(text,font));
//设置单元格高度
cell.setFixedHeight(signRect.getTop()-signRect.getBottom()-1);
cell.setBorderWidth(0);
//设置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//设置水平居中
// cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// 设置文字在单元格中换行时的行间距 :最终行间距 :totalLeading= fixedLeading+fontSize(字体大小)* multipliedLeading;
cell.setLeading(0.0f,1.2f);
//添加到表单中
table.addCell(cell);
//写入pdf
table.writeSelectedRows(0, -1, signRect.getLeft(), signRect.getTop(), contentByte);
}
@Test
public void createTest() throws DocumentException, IOException {
createReport.create();;
}
}
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