Commit d8222a66 authored by 王飞's avatar 王飞

Merge branch 'gaixiankang' into 'dev'

PDF - 企业留档文件(上传到minio, 返回路径地址)、表格数据暂时为假

See merge request !124
parents c1cb6e17 cb447d85
......@@ -14,4 +14,11 @@ public interface PdfTemplateManagementService {
* @throws Exception
*/
void generateInspectionReport(HttpServletResponse response) throws Exception;
/**
* 企业留档文件PDF下载
* @return
* @throws Exception
*/
String generateRetentionFile() throws Exception;
}
package com.ruoyi.service.impl;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PdfBaseWriter;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.vo.PhotoStorageVO;
import com.ruoyi.service.PdfTemplateManagementService;
import io.minio.MinioClient;
import io.minio.ObjectWriteArgs;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.*;
import java.util.List;
/**
* PDF模板管理Service业务层处理
......@@ -24,6 +31,18 @@ import java.util.Map;
@Transactional
public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementService {
@Value("${minio.bucketName}")
private String bucketName;
@Value("${minio.url}")
private String minioEndpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
/**
* 检验报告PDF下载
* @param response
......@@ -88,6 +107,96 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
}
}
/**
* 企业留档文件PDF下载
* @return
*/
@Override
public String generateRetentionFile() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 新建
PdfBaseWriter document = new PdfBaseWriter(outputStream);
// 打开
document.open();
// 内容
// 封面
document.setParagraph("检验中心名称", 12f, 0, 60,15f, 120f);
document.setParagraph("企业留档文件", 30f, Element.ALIGN_CENTER, 0,15f, 180f);
PdfPTable customTable = new PdfPTable(new float[]{15, 50});
customTable.setWidthPercentage(70);
document.writeNoBorderCell("检验依据:", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeBottomCell("GB xxxx-xxxx 汽车整车信息安全技术要求", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeNoBorderCell("检验项目:", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeBottomCell("汽车信息安全管理要求、车辆信息安全一般要求、车辆信息安全技术要求", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeNoBorderCell("创建时间:", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeBottomCell("2024-07-01", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeNoBorderCell("委托单位名称:", Element.ALIGN_LEFT, 1, 1, customTable);
document.writeBottomCell("xxxxxxxxxxxxxxxxxxx", Element.ALIGN_LEFT, 1, 1, customTable);
document.addContent(customTable);
document.setParagraph("检验中心名称", 20f, Element.ALIGN_CENTER, 0, 100f, 0f);
// 下一页
document.newPage();
document.setParagraph("检验中心名称", 12f, 0, 30,15f, 20f);
// 创建表格
List<String> list = Arrays.asList("文件名称", "文件目录", "标准章节", "标准要求", "审查要点");
float[] columnWidths = {20, 20, 20, 70, 70};
PdfPTable table = document.createWithHeaderTable(list, columnWidths);
document.writeCell("A文件", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("B文件", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("C文件", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("车辆制造商应具备车辆全生命周期的汽车信息安全管理体系", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("E文件",Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("F文件", Element.ALIGN_LEFT,1, 2, table);
document.writeCell("1.1", Element.ALIGN_LEFT,1, 1, table);
document.writeCell("5.1", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("要求1", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("要点1", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("1.2", Element.ALIGN_LEFT,1, 1, table);
document.writeCell("5.2", Element.ALIGN_LEFT, 1, 1, table);
document.writeCell("要求2", Element.ALIGN_LEFT,1, 1, table);
document.writeCell("要点2", Element.ALIGN_LEFT,1, 1, table);
document.addContent(table);
// 关闭
document.close();
// 上传到Minio
return uploadMinio(outputStream, "企业留档文件");
}
/**
* 上传到minio
* @param outputStream 输出流
* @param fileName 文件名
* @return minio存储地址
*/
private String uploadMinio(ByteArrayOutputStream outputStream, String fileName) throws Exception {
// 1.连接Minio
MinioClient minioClient =MinioClient.builder()
.endpoint(minioEndpoint)
.credentials(accessKey, secretKey)
.build();
// 2.上传
// 把os流转为is流
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
String newFileName = assemblyPdfName(fileName);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.object(newFileName)
.contentType("application/pdf")
.stream(inputStream, inputStream.available(), ObjectWriteArgs.MIN_MULTIPART_SIZE).build();
minioClient.putObject(putObjectArgs);
return newFileName;
}
/**
* 拼接pdf文件名
* @param name
* @return
*/
public String assemblyPdfName(String name) {
return StringUtils.format("{}/{}.pdf", DateUtils.datePath(), name);
}
/**
* 渲染数据
* @param fields
......
package com.ruoyi.web;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.service.PdfTemplateManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
......@@ -39,4 +40,22 @@ public class PdfTemplateManagementController {
}
}
/**
* 企业留档文件PDF下载
* @param response
*/
@Anonymous
@ApiOperation("企业留档文件PDF下载")
@GetMapping(value = "/retentionFile", produces = "application/json")
public R<String> downloadRetentionFile() {
try {
// demo-模板
String url = pdfTemplateManagementService.generateRetentionFile();
return R.ok(url);
} catch (Exception e) {
e.printStackTrace();
return R.fail();
}
}
}
package com.ruoyi.common.utils;
import com.google.common.collect.ListMultimap;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
......@@ -39,6 +38,11 @@ public class PdfBaseWriter extends Document{
* 浅黄色
*/
public final BaseColor LIGHT_YELLOW_COLOR = new BaseColor(255, 255, 153);
/**
* 灰色
*/
public final BaseColor LIGHT_GRAY_COLOR = new BaseColor(241, 241, 241);
/**
* 浅蓝色
*/
......@@ -81,7 +85,7 @@ public class PdfBaseWriter extends Document{
}
// 根据写入数据宽度创建表格
List<String> headers = new ArrayList<String>(rows.get(0).keySet());
PdfPTable table = createWithHeaderTable(headers);
PdfPTable table = createWithHeaderTable(headers, null);
for (LinkedHashMap<String, Object> row : rows) {
for (String k : headers) {
......@@ -92,20 +96,28 @@ public class PdfBaseWriter extends Document{
addContent(table);
return table;
}
/**
* 创建带表头的表格 {@link PdfPTable}
* @param headers 表头数据集
* @param columnWidths 每一列的宽度
* @return 表格 {@link PdfPTable}
*/
public PdfPTable createWithHeaderTable(List<String> headers) {
int numColumns = calculateColumnNumber(headers);
PdfPTable headerTable = new PdfPTable(numColumns);
public PdfPTable createWithHeaderTable(List<String> headers, float[] columnWidths) {
PdfPTable headerTable = null;
if (columnWidths == null) {
int numColumns = calculateColumnNumber(headers);
headerTable = new PdfPTable(numColumns);
} else {
headerTable = new PdfPTable(columnWidths);
}
headerTable.setWidthPercentage(100);
headerTable.setSpacingBefore(10);
for (String text : headers) {
writeCell(text, Element.ALIGN_CENTER, calculateColumnNumber(text), 1, headerTable, defaultFont(), 0f, Rectangle.BOX, LIGHT_BLUE_COLOR);
if (columnWidths == null) {
writeCell(text, Element.ALIGN_CENTER, calculateColumnNumber(text), 1, headerTable, defaultFont(), 0f, Rectangle.BOX, LIGHT_BLUE_COLOR);
} else {
writeCell(text, Element.ALIGN_CENTER, 1, 1, headerTable, defaultFont(), 0f, Rectangle.BOX, LIGHT_GRAY_COLOR);
}
}
return headerTable;
}
......@@ -151,6 +163,32 @@ public class PdfBaseWriter extends Document{
return writeCell(text, Element.ALIGN_CENTER, colspan, rowspan, table, defaultFont(), 0f, Rectangle.BOX, BaseColor.WHITE);
}
/**
* 写入单元格 {@link PdfPCell}
* @param text 内容
* @param align 对齐方式
* @param colspan 所占列数
* @param rowspan 所占行数
* @param table 表 {@link PdfPTable}
* @return 单元格 {@link PdfPCell}
*/
public PdfPCell writeCell(String text, int align, int colspan, int rowspan, PdfPTable table) {
return writeCell(text, align, colspan, rowspan, table, defaultFont(), 0f, Rectangle.BOX, BaseColor.WHITE);
}
/**
* 写入单元格 {@link PdfPCell}
* @param text 内容
* @param align 对齐方式
* @param colspan 所占列数
* @param rowspan 所占行数
* @param table 表 {@link PdfPTable}
* @return 单元格 {@link PdfPCell}
*/
public PdfPCell writeBottomCell(String text, int align, int colspan, int rowspan, PdfPTable table) {
return writeCell(text, align, colspan, rowspan, table, defaultFont(), 0f, Rectangle.BOTTOM, BaseColor.WHITE);
}
/**
* 写入单元格 {@link PdfPCell}
* @param text 内容
......@@ -228,6 +266,10 @@ public class PdfBaseWriter extends Document{
if (paddingLeft != null) {
cell.setPaddingLeft(paddingLeft);
}
if (align == Element.ALIGN_LEFT) {
cell.setPaddingLeft(4);
}
cell.setPaddingBottom(10);
if (isSet) {
table.addCell(cell);
}
......@@ -439,6 +481,27 @@ public class PdfBaseWriter extends Document{
}
}
/**
* 添加key-value似的内容
* @param frontText 前内容
* @param behindText 后内容
* @param size 大小
* @param firstLineIndent 第一行缩进
* @param spacingAfter 段间距
*/
public void writeUnderlineText(String frontText, String behindText, Float size, int firstLineIndent, Float spacingAfter) {
Paragraph paragraph = new Paragraph();
paragraph.setFirstLineIndent(firstLineIndent);
paragraph.setSpacingAfter(spacingAfter);
Phrase frontPhrase = new Phrase(frontText, defaultFont(size));
Phrase behindPhrase = new Phrase();
Chunk chunk = new Chunk(behindText, defaultFont(size));
chunk.setUnderline(01.f, -5f);
behindPhrase.add(chunk);
paragraph.add(frontPhrase);
paragraph.add(behindPhrase);
addContent(paragraph);
}
// /**
// * 标题
......@@ -563,8 +626,29 @@ public class PdfBaseWriter extends Document{
}
/**
*
* 设置自定义文本
* @param text 文本内容
* @param size 文本字号
* @param alignment 对齐方式
* @param firstLineIndex 首行是否进行缩进
* @param spacingBefore 段落上空白
* @param spacingAfter 段落下空白
* @return
*/
public void setParagraph(String text, Float size, int alignment, int firstLineIndex, Float spacingBefore, Float spacingAfter) {
Paragraph paragraph = new Paragraph(text, defaultFont(size));
if (alignment != 0) {
paragraph.setAlignment(alignment);
}
paragraph.setFirstLineIndent(firstLineIndex);
paragraph.setSpacingBefore(spacingBefore);
paragraph.setSpacingAfter(spacingAfter);
addContent(paragraph);
}
/**
* 添加 PDF 内容
* 新增工具类方法时,把想生成的内容先调用此方法
* @param element
* @return 如果添加了元素,则为true ,否则为false
*/
......@@ -643,6 +727,15 @@ public class PdfBaseWriter extends Document{
return chineseFont(10.5f, Font.NORMAL, BaseColor.BLACK);
}
/**
* 自定义字体的默认中文字体
* @param size
* @return
*/
public Font defaultFont(Float size) {
return chineseFont(size, Font.NORMAL, BaseColor.BLACK);
}
/**
* 默认中文字体
* @return 中文字体 {@link BaseFont}
......
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