|
@@ -26,8 +26,11 @@ import com.itextpdf.layout.property.HorizontalAlignment;
|
|
|
import com.itextpdf.layout.property.TextAlignment;
|
|
|
import com.om.entity.dto.ControlListMap;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
@@ -45,17 +48,50 @@ import java.util.Map;
|
|
|
* iText 7是iText强大的PDF工具包的最新版本,用于PDF生成,PDF编程,处理和操作,如数字签名等。
|
|
|
* https://kb.itextpdf.com/home/it7kb/ebooks
|
|
|
*/
|
|
|
+@Component
|
|
|
public class ITextPdfUtil {
|
|
|
//字体,我这里使用系统自带的simhei黑体
|
|
|
private static final String FONT = "C:/Windows/Fonts/simhei.ttf";
|
|
|
+ //private static final String FONT = "/usr/local/docker/opera/static/simhei.ttf";
|
|
|
+
|
|
|
+ @Value("${baseStatic}")
|
|
|
+ private String baseStatic;
|
|
|
|
|
|
|
|
|
//生成简单PDF
|
|
|
- public static File MapToPDF(Integer id, List<ControlListMap> controlListMap
|
|
|
+ public File MapToPDF(Integer id, List<ControlListMap> controlListMap
|
|
|
, Map<String, String> baseMap, Map<String, String> companyMap) {
|
|
|
+ System.out.println("baseStatic11: "+baseStatic);
|
|
|
|
|
|
String yyyyMMdd = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
- try (PdfWriter writer = new PdfWriter("src\\main\\resources\\" + yyyyMMdd + "report" + id + ".pdf");
|
|
|
+ //try (PdfWriter writer = new PdfWriter("src\\main\\resources\\" + yyyyMMdd + "report" + id + ".pdf");
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ /* // 创建目录对象
|
|
|
+ File directory = new File(baseStatic);
|
|
|
+ System.out.println("目录是否存在:"+directory.exists());
|
|
|
+ // 检查目录是否存在,如果不存在则创建它
|
|
|
+ if (!directory.exists()) {
|
|
|
+ directory.mkdirs(); // 创建目录及其父目录
|
|
|
+ }*/
|
|
|
+
|
|
|
+ // 创建文件对象
|
|
|
+ File pdfFile = new File(baseStatic + yyyyMMdd + "report" + id + ".pdf");
|
|
|
+ System.out.println("文件是否存在:"+pdfFile.exists());
|
|
|
+ // 检查文件是否存在,如果不存在则创建它
|
|
|
+ if (!pdfFile.exists()) {
|
|
|
+ pdfFile.createNewFile();
|
|
|
+ }
|
|
|
+ System.out.println("文件是否存在:"+pdfFile.exists());
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 处理文件操作异常
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try (PdfWriter writer = new PdfWriter(baseStatic + yyyyMMdd + "report" + id + ".pdf");
|
|
|
//FileOutputStream out = new FileOutputStream(new File("src\\main\\resources\\outpdf\\report.pdf"))
|
|
|
PdfDocument pdf = new PdfDocument(writer);
|
|
|
Document document = new Document(pdf, PageSize.A4);
|
|
@@ -67,7 +103,7 @@ public class ITextPdfUtil {
|
|
|
pdf.getDocumentInfo().setTitle("诊断报告");
|
|
|
|
|
|
//设置字体
|
|
|
- document.setFont(ITextPdfUtil.getPdfFont());
|
|
|
+ document.setFont(this.getPdfFont());
|
|
|
|
|
|
//页边距
|
|
|
document.setMargins(10, 10, 10, 10);
|
|
@@ -90,7 +126,9 @@ public class ITextPdfUtil {
|
|
|
Table baseTable = new Table(2);
|
|
|
mapToTable(baseMap, baseTable, c1, c2);
|
|
|
|
|
|
- ImageData imageData = ImageDataFactory.create("src\\main\\resources\\template\\static\\img.png");
|
|
|
+ //ImageData imageData = ImageDataFactory.create("src/main/resources/template/static/img.png");
|
|
|
+ System.out.println("baseStatic: "+baseStatic);
|
|
|
+ ImageData imageData = ImageDataFactory.create(baseStatic+"img.png");
|
|
|
Image update = new Image(imageData);
|
|
|
update.setWidth(20);
|
|
|
update.setHeight(20);
|
|
@@ -207,7 +245,7 @@ public class ITextPdfUtil {
|
|
|
);
|
|
|
document.close();
|
|
|
System.out.println("操作完成!");
|
|
|
- File file = new File("src\\main\\resources\\" + yyyyMMdd + "report" + id + ".pdf");
|
|
|
+ File file = new File(baseStatic + yyyyMMdd + "report" + id + ".pdf");
|
|
|
|
|
|
|
|
|
return file;
|
|
@@ -220,7 +258,7 @@ public class ITextPdfUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private static void mapToTable(Map<String, String> map, Table table, Color one, Color two) {
|
|
|
+ private void mapToTable(Map<String, String> map, Table table, Color one, Color two) {
|
|
|
table.setMargin(10);
|
|
|
table.setWidth(570);
|
|
|
table.setFontSize(16);
|
|
@@ -239,14 +277,14 @@ public class ITextPdfUtil {
|
|
|
RemoveBorder(table);
|
|
|
}
|
|
|
|
|
|
- private static void RemoveBorder(Table table) {
|
|
|
+ private void RemoveBorder(Table table) {
|
|
|
for (IElement iElement : table.getChildren()) {
|
|
|
((Cell) iElement).setBorder(Border.NO_BORDER);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取统一字体
|
|
|
- public static PdfFont getPdfFont() {
|
|
|
+ public PdfFont getPdfFont() {
|
|
|
PdfFont pdfFont = null;
|
|
|
try {
|
|
|
pdfFont = PdfFontFactory.createFont(ITextPdfUtil.FONT, PdfEncodings.IDENTITY_H, true);
|
|
@@ -256,49 +294,4 @@ public class ITextPdfUtil {
|
|
|
return pdfFont;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- //测试
|
|
|
- public static void main(String[] args) {
|
|
|
- Map<String, String> baseMap = new HashMap<>();
|
|
|
- baseMap.put("车架号", "01");
|
|
|
- baseMap.put("里程", "52Km");
|
|
|
- baseMap.put("年款", "1000");
|
|
|
- baseMap.put("报告编号", "01240222242360836214099968");
|
|
|
- baseMap.put("诊断路径", "诊断>五菱新能源TEST");
|
|
|
- baseMap.put("SN", "WL01000005");
|
|
|
-
|
|
|
- Map<String, String> faultCodeMap = new HashMap<>();
|
|
|
- faultCodeMap.put("B111716", "供电电压电量过低");
|
|
|
- faultCodeMap.put("U007588", "CHA CAN网段控制器BuSOff");
|
|
|
- faultCodeMap.put("P129600", "非N或P档无法ready");
|
|
|
- faultCodeMap.put("P129700", "充电连接信号有效无法ready");
|
|
|
-
|
|
|
- Map<String, String> versionMap = new HashMap<>();
|
|
|
- versionMap.put("引导软件标识符", "BtswV1.00");
|
|
|
- versionMap.put("零件号", "0000000000");
|
|
|
- versionMap.put("定义的ECU软件编号", "G050_1HB20");
|
|
|
- versionMap.put("定义的ECU硬件编号", "G050_1HB20");
|
|
|
- versionMap.put("供应商代码", "S-00009475");
|
|
|
- versionMap.put("ECU名称", "0000000VCU");
|
|
|
- versionMap.put("供应商硬件版本号", "V005.001");
|
|
|
- versionMap.put("供应商软件版本号", "V001.000");
|
|
|
- versionMap.put("ECU序列编号", "HB00009475230425T000000");
|
|
|
- versionMap.put("车辆识别号", "LZWADAGA4LB460541");
|
|
|
- versionMap.put("SK码", "00000000000000000000000000000000");
|
|
|
-
|
|
|
-
|
|
|
- Map<String, String> companyMap = new HashMap<>();
|
|
|
- companyMap.put("公司", "XXXXXX");
|
|
|
- companyMap.put("地址", "XXXXXXXXXXXX");
|
|
|
- companyMap.put("电话", "18888888888");
|
|
|
-
|
|
|
- List<ControlListMap> controlListMap = new ArrayList<>();
|
|
|
- ControlListMap controlListMap1 = new ControlListMap();
|
|
|
- controlListMap1.setName("G050右舵车/VCU整车控制器");
|
|
|
- controlListMap1.setVersionMap(versionMap);
|
|
|
- controlListMap1.setFaultCodeMap(faultCodeMap);
|
|
|
- controlListMap.add(controlListMap1);
|
|
|
-
|
|
|
- File file = MapToPDF(1, controlListMap, baseMap, companyMap);
|
|
|
- }
|
|
|
}
|