Commit 329f5aa5 authored by 盖献康's avatar 盖献康

原始记录PDF - 页眉完成

parent 42d30b8a
......@@ -6,8 +6,24 @@ import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import com.itextpdf.tool.xml.ElementList;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.ElementHandlerPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PdfBaseWriter;
......@@ -274,8 +290,35 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
*/
@Override
public String generateOriginalRecord(Long taskId) throws Exception{
// 创建临时文档
String templateName = temporaryDocument();
PdfReader pdfReader = new PdfReader(minioUrl() + templateName);
totalNum = pdfReader.getNumberOfPages() - 1;
// 真正文档
String currentName = temporaryDocument();
// copy - 分割
Document currentDoc = new Document();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(currentDoc, arrayOutputStream);
currentDoc.open();
PdfReader templateReader = new PdfReader(minioUrl() + currentName);
int numberOfPages = templateReader.getNumberOfPages();
for (int i = 2; i <= numberOfPages; i++) {
PdfImportedPage page = copy.getImportedPage(templateReader, i);
copy.addPage(page);
}
currentDoc.close();
templateReader.close();
copy.close();
return uploadMinio(arrayOutputStream, "原始记录");
}
// 创建一张PDF文件
/**
* 创建临时文档
* @return
* @throws Exception
*/
private String temporaryDocument() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfBaseWriter document = new PdfBaseWriter(outputStream);
document.setMargins(50, 50, 85, 40);
......@@ -365,15 +408,14 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
cell.setColspan(3);
cell.setRowspan(1);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// String htmlContent = convertMarkdownToHtml("**请依次完成以下操作:**\\n\\n1)使用电动螺丝刀拆开车机外壳,注意不要造成物理性破坏;\\n\\n2)肉眼观察板子上是否有明显的JTAG调试接口,需要注意CPU附近的点和引脚的各种信息;\\n\\n注:JTAG通常主要由以下4个针脚组成:\\n\\n①TCK ... Test Clock\\n\\n②TMS ... Test Mode Select \\n\\n③TDI ... Test Data In\\n\\n④TDO ... Test Data Out\\n\\n3)通过五倍光学放大镜仔细查看PCB板上的信息,找到带有JTAG、TCK、TMS、TDI、TDO等相关的信息的点位,比如下图:\\n\\n![](http://10.12.48.80:8089/file_server/api_v1/downloadSysFile?key=e45566a4ebf14eb299d60b180473f9c84c&ts=2024_03_05&token=7f5ac770a013f7ce74205992257f5d9b&tmp=false&inline=true)\\n\\n4)将测试过程中的 PCB 板照片等作为测试证明材料上传至平台。\\n\\n<br>\\n\\n**请根据PCB板上是否存在明显的JTAG调试接口对节点结果进行判断。**");
// CSS样式,设置默认字体
String css = "body { font-family: Arial, sans-serif; }";
String htmlContent = convertMarkdownToHtml("**请依次完成以下操作:**\\n\\n1)使用电动螺丝刀拆开车机外壳,注意不要造成物理性破坏;\\n\\n2)肉眼观察板子上是否有明显的JTAG调试接口,需要注意CPU附近的点和引脚的各种信息;\\n\\n注:JTAG通常主要由以下4个针脚组成:\\n\\n①TCK ... Test Clock\\n\\n②TMS ... Test Mode Select \\n\\n③TDI ... Test Data In\\n\\n④TDO ... Test Data Out\\n\\n3)通过五倍光学放大镜仔细查看PCB板上的信息,找到带有JTAG、TCK、TMS、TDI、TDO等相关的信息的点位,比如下图:\\n\\n![](http://49.232.167.247:22038/vehicle-quality-review-oss/2024/04/11/1712827970222_20240411173255A009.jpg)\\n\\n4)将测试过程中的 PCB 板照片等作为测试证明材料上传至平台。\\n\\n<br>\\n\\n**请根据PCB板上是否存在明显的JTAG调试接口对节点结果进行判断。**");
// h5渲染到cell
String htmlContent = "<h1 style=\"font-size: 36pt;\">Hello, World!</h1><h2>我的</h2><p style=\"color: red;\">This is some HTML content.</p><img src=\"http://49.232.167.247:22038/vehicle-quality-review-oss/2024/04/11/1712827970222_20240411173255A009.jpg\" />";
ElementList elementList = XMLWorkerHelper.parseToElementList(htmlContent, css);
for (Element element : elementList) {
cell.addElement(element);
}
// String htmlContent = "<h1 style=\"font-size: 36pt;\">Hello, World!</h1><h2>我的</h2><p style=\"color: red;\">This is some HTML content.</p><img src=\"http://49.232.167.247:22038/vehicle-quality-review-oss/2024/04/11/1712827970222_20240411173255A009.jpg\" />";
Paragraph context = new Paragraph();
// ElementList elementList = XMLWorkerHelper.parseToElementList(htmlContent, css);
ElementList elementList = parseToElementList(htmlContent, null);
context.addAll(elementList);
cell.addElement(context);
useCaseTable.addCell(cell);
document.add(useCaseTable);
// 5.附录
......@@ -383,26 +425,77 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
// close
document.close();
writer.close();
String templateName = uploadMinio(outputStream, "原始记录临时");
Document currentDoc = new Document();
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(currentDoc, arrayOutputStream);
currentDoc.open();
PdfReader templateReader = new PdfReader(minioUrl() + templateName);
PdfStamper stamper = new PdfStamper(templateReader, arrayOutputStream);
int numberOfPages = templateReader.getNumberOfPages();
for (int i = 2; i <= numberOfPages; i++) {
copy.addPage(copy.getImportedPage(templateReader, i));
return uploadMinio(outputStream, "原始记录临时");
}
/**
* html转pdf 写法
* @param html
* @param css
* @return
* @throws IOException
*/
public static ElementList parseToElementList(String html, String css) throws IOException {
// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
if (css != null) {
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(css.getBytes()));
cssResolver.addCss(cssFile);
}
// HTML
MyFontsProvider fontProvider = new MyFontsProvider();
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.autoBookmark(false);
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
// XML Worker
XMLWorker worker = new XMLWorker(cssPipeline, true);
XMLParser p = new XMLParser(worker);
html = html.replace("<br>", "").replace("<hr>", "").replace("<img>", "").replace("<param>", "").replace("<link>", "");
p.parse(new ByteArrayInputStream(html.getBytes()));
return elements;
}
static class MyFontsProvider extends XMLWorkerFontProvider {
public MyFontsProvider() {
super(null, null);
}
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) {
return getFontInf();
}
currentDoc.close();
templateReader.close();
copy.close();
return uploadMinio(arrayOutputStream, "原始记录");
}
/**
* 设置字体信息
* @return
*/
@SneakyThrows
private static Font getFontInf() {
// 字体路径
String fontPath = "/fonts/STSong.TTF";
BaseFont baseFont = null;
Font font = null;
// 设置字体路径,字体编码,是否将字体嵌入pdf(默认false)
baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 设置默认字体数据
font = new Font(baseFont, 12f, Font.NORMAL,BaseColor.BLACK);
return font;
}
private static int totalNum = 0;
public static class OriginalRecordHeaderFooter extends PdfPageEventHelper {
private int totalNum = 0;
// PdfTemplate totalPage;
......@@ -412,6 +505,16 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
// totalPage = contentByte.createTemplate(30, 16);
// }
@Override
@SneakyThrows
public void onCloseDocument(PdfWriter writer, Document document) {
// BaseFont font = BaseFont.createFont("/fonts/STSong.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// String text = " 共 " + (writer.getPageNumber()) + " 页";
// ColumnText.showTextAligned(totalPage, Element.ALIGN_CENTER, new Paragraph(String.valueOf(text), new Font(font, 8f)), 2, 4 ,0);
totalNum = writer.getPageNumber() - 1;
System.out.println(totalNum);
}
/**
* 一页加载完成触发,写入页眉和页脚
*/
......@@ -461,15 +564,6 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
bottomTable.writeSelectedRows(0, -1, 40, 40, writer.getDirectContent());
}
@Override
@SneakyThrows
public void onCloseDocument(PdfWriter writer, Document document) {
// BaseFont font = BaseFont.createFont("/fonts/STSong.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// String text = " 共 " + (writer.getPageNumber()) + " 页";
// ColumnText.showTextAligned(totalPage, Element.ALIGN_CENTER, new Paragraph(String.valueOf(text), new Font(font, 8f)), 2, 4 ,0);
totalNum = writer.getPageNumber();
System.out.println(totalNum);
}
}
......@@ -489,7 +583,7 @@ public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementSe
HtmlRenderer renderer = HtmlRenderer.builder().build();
// 渲染 HTML
org.jsoup.nodes.Document doc = Jsoup.parse(renderer.render(document).replaceAll("\\n", "<br>"));
org.jsoup.nodes.Document doc = Jsoup.parse(renderer.render(document).replaceAll("\\\\n", "<br>"));
// jsoup标准化标签,生成闭合标签
doc.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
......
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