1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| @Slf4j public class TestHtmlToPdf { public final static String TEMP = "D:\\lot\\code\\study\\springboot-restful-starter\\src\\main\\resources\\templates\\";
public static Map<String, Object> getContent() { Map input = new HashMap(); input.put("title", "测试title"); input.put("logo", ""); input.put("accountId", "MC" + UUID.randomUUID(false)); input.put("accountName", "MC" + UUID.randomUUID(false)); input.put("dateOfIssue", DateUtil.now()); List<Map<String, String>> tableData = new ArrayList<>(); for (int i = 0; i < 10; i++) { Map<String, String> rowData = new HashMap<>(); rowData.put("tableData1", "这是第" + (i + 1) + "行的第一个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据"); rowData.put("tableData2", "这是第" + (i + 1) + "行的第二个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据行的第一个表格数据"); tableData.add(rowData); } input.put("tableData", tableData);
input.put("accountId", UUID.randomUUID()); input.put("DateOfIssue", DateUtil.today());
String[] firstNames = {"James", "John", "Robert", "Michael", "William"}; String[] lastNames = {"Smith", "Johnson", "Williams", "Jones", "Brown"};
String randomFirstName = RandomUtil.randomEle(firstNames); String randomLastName = RandomUtil.randomEle(lastNames);
String fullName = randomFirstName + " " + randomLastName; input.put("accountName", fullName);
List<Map<String, Object>> rows = new ArrayList<>(); double y = 0; for (int i = 0; i < 500; i++) { Random r = new Random(); DecimalFormat df = new DecimalFormat("#.##"); String[] currencyArr = {"USD", "CNY", "EURO"}; String[] platformArr = {"Shopify", "WooCommerce", "Magento"}; String[] shipArr = {"AFL SHIPPING", "BFL SHIPPING", "CFL SHIPPING"}; String[] typeArr = {"Deposit", "Withdraw", "Refund"}; int minAmount = 20; int maxAmount = 1000; String date = "2024-04-" + r.nextInt(30); String demo1 = "DemoDemoDemoDemoDemoDemoDemo" + RandomUtil.randomNumbers(10); String demo2 = "Demo" + RandomUtil.randomNumbers(Integer.valueOf(RandomUtil.randomNumbers(1))); String accNumber = String.valueOf(r.nextLong()).substring(1, 14); String currency = currencyArr[r.nextInt(currencyArr.length)]; String platform = platformArr[r.nextInt(platformArr.length)]; String name = "Ma Wang" + RandomUtil.randomNumbers(10); String ship = shipArr[r.nextInt(shipArr.length)]; String quantity = String.valueOf(r.nextInt(30)); String type = typeArr[r.nextInt(typeArr.length)]; String amount = df.format(minAmount + (maxAmount - minAmount) * r.nextDouble());
Map<String, Object> row = new HashMap<>(); row.put("y", y); row.put("part1", date); row.put("part2", demo1); row.put("part3", demo2); row.put("part4", accNumber); row.put("part5", currency); row.put("part6", platform); row.put("part7", name); row.put("part8", ship); row.put("part10", currency); row.put("part11", quantity); row.put("part12", currency); row.put("part13", amount); row.put("part9", type); rows.add(row); y += 15;
} input.put("rows", rows);
return input; }
public static void main(String[] args) throws IOException { extracted("thread-12"); }
private static void extracted(String filename) throws IOException { long startTime = System.currentTimeMillis(); String temp = "test"; FreeMarkerUtils.genteratorFile(getContent(), TEMP, temp, TEMP, temp + ".html");
String htmlFile = temp + ".html"; String pdfFile = filename + ".pdf"; String waterMarkText = "JYX"; InputStream inputStream = new FileInputStream(TEMP + htmlFile); OutputStream outputStream = new FileOutputStream(TEMP + pdfFile); String FONT_TTF_PATH = "D:\\lot\\code\\springboot-restful-starter\\src\\main\\resources\\fonts\\HARMONYOS_SANS_SC_REGULAR.TTF";
HtmlToPdfUtils.convertToPdf(inputStream, waterMarkText, FONT_TTF_PATH, outputStream); log.info("转换结束,耗时:{}ms", System.currentTimeMillis() - startTime); } }
|