Commit 2d015e58 authored by 盖献康's avatar 盖献康

原始记录PDF - 第一页、页眉页脚、h5导入cell

parent 9db3176b
......@@ -61,6 +61,11 @@
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-framework</artifactId>
......@@ -70,6 +75,12 @@
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<!-- markdown->h5 -->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.15.2</version>
</dependency>
</dependencies>
</project>
......@@ -27,7 +27,8 @@ public interface PdfTemplateManagementService {
* 原始记录PDF下载
* @param taskId
* @return
* @throws Exception
*/
String generateOriginalRecord(Long taskId);
String generateOriginalRecord(Long taskId) throws Exception;
}
......@@ -2,6 +2,9 @@ package com.ruoyi.service.impl;
import cn.hutool.core.util.StrUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
......@@ -18,14 +21,14 @@ import io.minio.MinioClient;
import io.minio.ObjectWriteArgs;
import io.minio.PutObjectArgs;
import lombok.SneakyThrows;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.*;
......@@ -265,31 +268,134 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
* @return
*/
@Override
public String generateOriginalRecord(Long taskId) {
return null;
public String generateOriginalRecord(Long taskId) throws Exception{
// 创建一张PDF文件
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfBaseWriter document = new PdfBaseWriter(outputStream);
document.setMargins(50, 50, 80, 40);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
OriginalRecordHeaderFooter headerFooter = new OriginalRecordHeaderFooter();
writer.setPageEvent(headerFooter);
// open
document.open();
// 内容
// 1.检验依据
PdfPTable reportNumberTable = new PdfPTable(new float[]{30, 15});
reportNumberTable.setWidthPercentage(100);
reportNumberTable.setSpacingBefore(40);
document.writeNoBorderCell("报告编号:", Element.ALIGN_RIGHT, 1, 1, reportNumberTable);
document.writeBottomCell("", Element.ALIGN_RIGHT, 1, 1, reportNumberTable);
document.addContent(reportNumberTable);
document.setParagraph("1.检验依据", 10f, Element.ALIGN_LEFT, 0,10f, 10f);
document.setParagraph("口《汽车整车信息安全技术要求》", 10f, Element.ALIGN_LEFT, 14,0f, 0f);
// 2.样品情况
document.setParagraph("2.样品情况", 10f, Element.ALIGN_LEFT, 0,10f, 10f);
PdfPTable sampleConditionTable = new PdfPTable(new float[]{30, 20, 30, 20});
sampleConditionTable.setWidthPercentage(100);
document.writeCell("样品名称", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("xxxxxxx", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("生产企业", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("xxxxxxx", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("样品型号", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("xxxxxxx", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("样品编号", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("xxxxxxx", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("样品数量", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("xxxxxxx", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("商标", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("xxxxxxx", Element.ALIGN_CENTER, 1, 1, sampleConditionTable);
document.writeCell("备注:", Element.ALIGN_LEFT, 4, 1, sampleConditionTable);
document.addContent(sampleConditionTable);
// 3.仪器设备信息
document.setParagraph("3.仪器设备信息", 10f, Element.ALIGN_LEFT, 0,10f, 0f);
List<String> equipmentList = Arrays.asList("设备编号", "设备名称", "溯源有效期");
float[] columnWidths = {20, 20, 60};
PdfPTable equipmentTable = document.createWithHeaderTable(equipmentList, columnWidths);
document.writeCell("xxxx", Element.ALIGN_CENTER, 1, 1, equipmentTable);
document.writeCell("xxxxxxxxx", Element.ALIGN_CENTER, 1, 1, equipmentTable);
document.writeCell("xxxx-xx-xx", Element.ALIGN_CENTER, 1, 1, equipmentTable);
document.add(equipmentTable);
// 第一页下方信息
PdfPTable inspectorTable = new PdfPTable(new float[]{20, 20, 10, 40});
inspectorTable.setWidthPercentage(100);
inspectorTable.setTotalWidth(PageSize.A4.getWidth() - 80);
document.writeNoBorderCell("检验人员:", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeNoBorderCell("", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeNoBorderCell("检验日期:", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeNoBorderCell("", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeBottomCell("检验负责人:", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeBottomCell("", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeBottomCell("检验地点:", Element.ALIGN_LEFT, 1, 1, inspectorTable);
document.writeBottomCell("口中汽研软件测评有限公司 口其他:", Element.ALIGN_LEFT, 1, 1, inspectorTable);
inspectorTable.writeSelectedRows(0, -1, document.left(), document.bottom() + 60, writer.getDirectContent());
// close
document.close();
writer.close();
return uploadMinio(outputStream, "原始记录");
}
public static class OriginalRecordHeaderFooter extends PdfPageEventHelper {
// 一页加载完成触发,写入页眉和页脚
/**
* 一页加载完成触发,写入页眉和页脚
*/
@Override
@SneakyThrows
public void onEndPage(PdfWriter writer, Document document) {
BaseFont font = BaseFont.createFont("/fonts/STSong.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfPTable table = new PdfPTable(2);
try {
table.setTotalWidth(PageSize.A4.getWidth() - 80);
table.setWidths(new int[] { 40, 40 });
table.setWidths(new int[] { 60, 40 });
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(-10);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.getDefaultCell().setBorderWidth(0.6f);
BaseFont font = BaseFont.createFont("/fonts/STSong.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
table.addCell(new Paragraph(headerText, new Font(font)));// 可以直接使用addCell(str),不过不能指定字体,中文无法显示
table.getDefaultCell().setFixedHeight(30);
table.getDefaultCell().setBorder(Rectangle.BOX);
table.getDefaultCell().setBorderWidth(0.5f);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setRowspan(1);
table.getDefaultCell().setColspan(2);
table.addCell(new Paragraph("中汽研软件测评有限公司", new Font(font)));
table.getDefaultCell().setRowspan(1);
table.getDefaultCell().setColspan(1);
table.addCell(new Paragraph("检验记录", new Font(font)));
table.getDefaultCell().setRowspan(1);
table.getDefaultCell().setColspan(1);
table.addCell(new Paragraph("第 " + writer.getPageNumber() + " 页", new Font(font)));
// 将页眉写到document中,位置可以指定,指定到下面就是页脚
table.writeSelectedRows(0, -1, 40, PageSize.A4.getHeight() - 20, writer.getDirectContent());
} catch (Exception de) {
throw new ExceptionConverter(de);
// 页脚
PdfPTable bottomTable = new PdfPTable(1);
bottomTable.setTotalWidth(PageSize.A4.getWidth() - 80);
bottomTable.setWidths(new int[] { 50});
bottomTable.setLockedWidth(true);
bottomTable.getDefaultCell().setFixedHeight(-10);
bottomTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
bottomTable.getDefaultCell().setBorderWidth(0.5f);
bottomTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
bottomTable.addCell(new Paragraph(String.valueOf(writer.getPageNumber()), new Font(font)));
bottomTable.writeSelectedRows(0, -1, 40, 40, writer.getDirectContent());
}
}
/**
*
* @param markdownContent
* @return
*/
public static String convertMarkdownToHtml(String markdownContent) {
// 创建 Markdown 解析器
Parser parser = Parser.builder().build();
// 解析 Markdown
Node document = parser.parse(markdownContent);
// 创建 HTML 渲染器
HtmlRenderer renderer = HtmlRenderer.builder().build();
// 渲染 HTML
return renderer.render(document);
}
private static String headerText;
......
......@@ -101,7 +101,7 @@ public class PdfTemplateManagementController {
if (StrUtil.isBlank(url)) {
// TODO 生成车型
url = pdfTemplateManagementService.generateOriginalRecord(request.getTaskId());
taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getModelTestTaskId, url));
taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getModelTestUrl, url));
}
break;
default:
......@@ -139,7 +139,7 @@ public class PdfTemplateManagementController {
case 2:
// 更新
url = pdfTemplateManagementService.generateOriginalRecord(request.getTaskId());
taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getModelTestTaskId, url));
taskService.update(new UpdateWrapper<Task>().lambda().eq(Task::getId, request.getTaskId()).set(Task::getModelTestUrl, url));
break;
default:
break;
......
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