|
@@ -1,42 +1,156 @@
|
|
|
package com.miniframe;
|
|
|
|
|
|
-import com.miniframe.mdo.service.LogService;
|
|
|
+
|
|
|
+import com.itextpdf.html2pdf.ConverterProperties;
|
|
|
+import com.itextpdf.html2pdf.HtmlConverter;
|
|
|
+import com.itextpdf.io.font.PdfEncodings;
|
|
|
+import com.itextpdf.layout.font.FontProvider;
|
|
|
import com.miniframe.modo.temp.TemplateGenerator;
|
|
|
-import com.miniframe.modo.xml.input.Optimizer_GA_Input;
|
|
|
-import com.miniframe.tools.XiJsonUtil;
|
|
|
import freemarker.template.Configuration;
|
|
|
import freemarker.template.Template;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|
|
-
|
|
|
-import javax.xml.bind.JAXBContext;
|
|
|
-import javax.xml.bind.Marshaller;
|
|
|
-import javax.xml.bind.Unmarshaller;
|
|
|
-import java.io.*;
|
|
|
+
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.io.StringWriter;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public class PdfTest {
|
|
|
|
|
|
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
+
|
|
|
+ public static <PdfOptions> void main(String[] args) throws Exception {
|
|
|
+// fillPdfTemplate();
|
|
|
+// Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
|
|
|
+// // 设置模板所在目录
|
|
|
+// cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
|
|
|
+// // 获取模板对象
|
|
|
+// Template template = cfg.getTemplate("report.xml");
|
|
|
+// // 定义数据模型(Map)
|
|
|
+// Map<String, Object> dataModel = new HashMap<>();
|
|
|
+//
|
|
|
+// dataModel.put("report","这是动态添加的内容");
|
|
|
+// //dataModel.put("geoFilePath", BPATH + "/" + aid + "/" + jid + "/Geometry");
|
|
|
+// // 将数据模型传入模板进行处理
|
|
|
+// StringWriter writer = new StringWriter();
|
|
|
+// template.process(dataModel, writer);
|
|
|
+// FileWriter fileWriter = new FileWriter("D://output2.pdf");
|
|
|
+// fileWriter.write(writer.toString());
|
|
|
+// fileWriter.close();
|
|
|
+
|
|
|
+// try {
|
|
|
+// convert("C:\\Users\\Administrator\\Desktop\\17b702fa0110426db067b030af9fe1d7.docx", "path/to/your/output.pdf");
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// String htmlFilePath = "C:\\Users\\Administrator\\Desktop\\17b702fa0110426db067b030af9fe1d7.docx";
|
|
|
+// String pdfFilePath = "C:\\Users\\Administrator\\Desktop\\output.pdf";
|
|
|
+// convert2(htmlFilePath,pdfFilePath);
|
|
|
+ OutputStream baos =new FileOutputStream("D://1.pdf");
|
|
|
Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
|
|
|
// 设置模板所在目录
|
|
|
cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
|
|
|
// 获取模板对象
|
|
|
- Template template = cfg.getTemplate("report.xml");
|
|
|
+ Template template = cfg.getTemplate("report.html");
|
|
|
// 定义数据模型(Map)
|
|
|
Map<String, Object> dataModel = new HashMap<>();
|
|
|
-
|
|
|
- dataModel.put("report","这是动态添加的内容");
|
|
|
- //dataModel.put("geoFilePath", BPATH + "/" + aid + "/" + jid + "/Geometry");
|
|
|
- // 将数据模型传入模板进行处理
|
|
|
StringWriter writer = new StringWriter();
|
|
|
+ dataModel.put("report","中午呢啊手动阀");
|
|
|
template.process(dataModel, writer);
|
|
|
- FileWriter fileWriter = new FileWriter("D://output2.pdf");
|
|
|
- fileWriter.write(writer.toString());
|
|
|
- fileWriter.close();
|
|
|
+
|
|
|
+ ConverterProperties props = new ConverterProperties();
|
|
|
+ FontProvider fontProvider = new FontProvider();
|
|
|
+ fontProvider.addFont("templates/msyh.ttf", PdfEncodings.IDENTITY_H); // 添加微软雅黑字体
|
|
|
+ props.setFontProvider(fontProvider);
|
|
|
+
|
|
|
+ HtmlConverter.convertToPdf(writer.toString(), baos,props);
|
|
|
+ // 关闭文档和写入器以释放资源
|
|
|
+ System.out.println("PDF created successfully.");
|
|
|
+ baos.close();
|
|
|
+
|
|
|
+// convert(htmlFilePath,pdfFilePath);
|
|
|
+// PdfWriter writer = new PdfWriter(pdfFilePath);
|
|
|
+// PdfDocument pdf = new PdfDocument(writer);
|
|
|
+ // 设置字体提供者,确保中文能够正确显示
|
|
|
+
|
|
|
+ // 这里假设你有一个中文字体文件,如NotoSansCJK.ttc,放在项目的resources目录下
|
|
|
+ // 你需要根据实际情况调整字体文件的路径
|
|
|
+
|
|
|
+// String fontPath = "F:\\airopt\\airoptserver\\src\\main\\resources\\templates\\msyh.ttf"; // 替换为实际的字体文件路径
|
|
|
+//// properties.setFontProvider(new FontProvider()
|
|
|
+//// .addStandardPdfFonts()
|
|
|
+//// .addExternalFont(fontPath, FontConstants.IDENTITY_H, true));
|
|
|
+// FontFactory.register(fontPath);
|
|
|
+// DefaultFontProvider d = new DefaultFontProvider(false,false,false);
|
|
|
+// PdfFont microsoft = PdfFontFactory.createFont(FontProgramFactory.createFont(fontPath));
|
|
|
+// d.addFont(microsoft.getFontProgram(), PdfEncodings.IDENTITY_H);
|
|
|
+//
|
|
|
+// ConverterProperties converterProps = new ConverterProperties();
|
|
|
+// converterProps.setFontProvider(d);
|
|
|
+// // 调用HtmlConverter类的convertToPdf函数,将HTML字符串转换为PDF文件
|
|
|
+// converterProps.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
|
|
|
+// converterProps.setCssApplierFactory(new DefaultCssApplierFactory());
|
|
|
+// HtmlConverter.convertToPdf(new FileInputStream(htmlFilePath), pdf,converterProps);
|
|
|
+// pdf.close();
|
|
|
+// System.out.println("PDF created successfully.");
|
|
|
}
|
|
|
+// public static void convert(String wordFilePath, String pdfFilePath) throws Exception {
|
|
|
+// try (InputStream is = new FileInputStream(wordFilePath);
|
|
|
+// XWPFDocument document = new XWPFDocument(is);
|
|
|
+// PdfWriter writer = new PdfWriter(pdfFilePath);
|
|
|
+// PdfDocument pdf = new PdfDocument(writer);
|
|
|
+// Document doc = new Document(pdf)) {
|
|
|
+//
|
|
|
+// for (XWPFParagraph paragraph : document.getParagraphs()) {
|
|
|
+// Paragraph p = new Paragraph(paragraph.getText()).setFont(PdfFontFactory.createFont());
|
|
|
+// doc.add(p);
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// public static void convert(String htmlPath, String pdfPath) {
|
|
|
+// Document document = new Document();
|
|
|
+// try {
|
|
|
+// PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
|
|
|
+// document.open();
|
|
|
+//
|
|
|
+// // 读取HTML文件
|
|
|
+// FileInputStream fis = new FileInputStream(htmlPath);
|
|
|
+// byte[] data = new byte[fis.available()];
|
|
|
+// fis.read(data);
|
|
|
+// fis.close();
|
|
|
+// String html = new String(data);
|
|
|
+//
|
|
|
+// // 转换HTML为PDF
|
|
|
+// HTMLWorker htmlWorker = new HTMLWorker(document);
|
|
|
+// htmlWorker.parse(new StringReader(html));
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// } finally {
|
|
|
+// document.close();
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 核心代码
|
|
|
+// public static void fillPdfTemplate() throws Exception {
|
|
|
+// PdfReader reader = new PdfReader("C:\\Users\\Administrator\\Desktop\\report.pdf");
|
|
|
+// PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:\\Users\\Administrator\\Desktop\\output.pdf"));
|
|
|
+// AcroFields form = stamper.getAcroFields();
|
|
|
+// // 填充文本数据
|
|
|
+// form.setField("report", "围殴代码");
|
|
|
+//
|
|
|
+// // 填充图片(如签名/公章)
|
|
|
+//// Image seal = Image.getInstance("seal.png");
|
|
|
+//// form.getFieldPositions("signature").forEach(pos -> {
|
|
|
+//// PdfContentByte canvas = stamper.getOverContent(pos.page);
|
|
|
+//// canvas.addImage(seal, seal.getWidth(), 0, 0, seal.getHeight(), pos.position.getLeft(), pos.position.getBottom());
|
|
|
+//// });
|
|
|
+//
|
|
|
+// stamper.setFormFlattening(true); // 锁定表单
|
|
|
+// stamper.close();
|
|
|
+// reader.close();
|
|
|
+// }
|
|
|
+
|
|
|
|
|
|
}
|