| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.BufferedWriter; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | | import java.io.Writer; |
| | | import java.io.*; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | } |
| | | |
| | | |
| | | public String generatePdf(String basePackagePath, String templateFileName, Map<String,Object> templateParam, String fileName, String saveDirectory) { |
| | | public String generatePdf(String basePackagePath, String templateFileName, Map<String,Object> templateParam, String fileName, String saveDirectory,String url) { |
| | | try { |
| | | |
| | | fillTemplate(basePackagePath+templateFileName, saveDirectory+fileName+".docx", templateParam); |
| | | fillTemplate(basePackagePath+templateFileName, saveDirectory+fileName+".docx", templateParam,url); |
| | | |
| | | // 创建 Freemarker 的 Configuration 对象,设置默认的不兼容改进选项 |
| | | // Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); |
| | |
| | | } |
| | | } |
| | | |
| | | public static void fillTemplate(String templatePath, String outputPath,Map<String, Object> dataMap) { |
| | | public static void fillTemplate(String templatePath, String outputPath,Map<String, Object> dataMap,String url) { |
| | | try (FileInputStream fis = new FileInputStream(templatePath)) { |
| | | // 设置默认编码为UTF-8 |
| | | System.setProperty("file.encoding", "UTF-8"); |
| | |
| | | XWPFDocument document = new XWPFDocument(fis); |
| | | XWPFParagraph pic = document.createParagraph(); |
| | | XWPFRun picRun = pic.createRun(); |
| | | Map<String, Object> dataMap1 = new HashMap<>(); |
| | | // dataMap.put("${picture}", picRun.addPicture( |
| | | // new FileInputStream("/usr/local/project/file/1.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, |
| | | // "/usr/local/project/file/1.jpg", |
| | | // Units.toEMU(60), |
| | | // Units.toEMU(30) |
| | | // )); |
| | | Base64.Decoder decoder = Base64.getDecoder(); |
| | | byte[] imageByte = decoder.decode(url); |
| | | InputStream stream = new ByteArrayInputStream(imageByte); |
| | | File tempFile = File.createTempFile("/usr/local/project/file/temp", ".jpg"); |
| | | tempFile.deleteOnExit(); // 程序结束时删除文件 |
| | | |
| | | try (OutputStream out = new FileOutputStream(tempFile); |
| | | InputStream in = stream) { |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | // 从原始流读取数据并写入临时文件 |
| | | while ((length = in.read(buffer)) > 0) { |
| | | out.write(buffer, 0, length); |
| | | } |
| | | } |
| | | dataMap.put("${picture}", picRun.addPicture( |
| | | new FileInputStream(tempFile), XWPFDocument.PICTURE_TYPE_JPEG, |
| | | "/usr/local/project/file/sign.jpg", |
| | | Units.toEMU(60), |
| | | Units.toEMU(30) |
| | | )); |
| | | // 处理段落 |
| | | for (XWPFParagraph paragraph : document.getParagraphs()) { |
| | | replaceParagraph(paragraph, dataMap); |
| | | replaceParagraph1(paragraph); |
| | | } |
| | | |
| | | // 处理表格 |
| | |
| | | |
| | | for (ReplacementInfo info : replacements) { |
| | | replaceRunRange(paragraph, info); |
| | | } |
| | | } |
| | | private static void replaceParagraph1(XWPFParagraph paragraph) throws IOException, InvalidFormatException { |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | for (XWPFRun r : runs) { |
| | | String text = r.getText(0); |
| | | if (text != null && text.contains("{{image}}")) { |
| | | r.setText("", 0); |
| | | FileInputStream is = new FileInputStream("/usr/local/project/file/1.jpg"); |
| | | r.addBreak(); |
| | | r.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, "/usr/local/project/file/1.jpg", Units.toEMU(60), Units.toEMU(30)); |
| | | is.close(); |
| | | } |
| | | } |
| | | } |
| | | |