package com.ruoyi.common.utils; import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter; import com.documents4j.job.LocalConverter; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; import java.io.*; import java.nio.charset.Charset; import java.nio.file.Files; /** * @author wangqiong * @description pdf转换工具类 * @date 2023-11-28 15:20 */ public class PdfUtils { /** * 将html字符串转为pdf * @param pdfFilePath 保存的路径 * @param htmlcontent:必须是完整的html格式,比如123 */ public static boolean htmlStringConvertToPDF(String pdfFilePath, String htmlcontent) { Document document = new Document(); PdfWriter writer = null; try { if(!new File(pdfFilePath).exists()){ new File(pdfFilePath).createNewFile(); } writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath)); // 设置底部距离60,解决重叠问题 document.setPageSize(PageSize.A4); document.setMargins(50, 45, 50, 60); document.setMarginMirroring(false); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("UTF-8")), null, Charset.forName("UTF-8")); } catch (Exception e) { return false; } finally { if (null != document) { document.close(); } if (null != writer) { writer.close(); } } return true; } /** * 根据html文件生成pdf * @param pdfFilePath pdf文件生成路径 * @param htmlFilePath html文件路径 */ public static boolean htmlFileConvertToPDF(String pdfFilePath, String htmlFilePath) { Document document = new Document(); PdfWriter writer = null; FileOutputStream fileOutputStream = null; FileInputStream fileInputStream = null; try { fileOutputStream = new FileOutputStream(pdfFilePath); writer = PdfWriter.getInstance(document, fileOutputStream); // 设置底部距离60,解决重叠问题 document.setPageSize(PageSize.A4); document.setMargins(50, 45, 50, 60); document.setMarginMirroring(false); document.open(); StringBuffer sb = new StringBuffer(); fileInputStream = new FileInputStream(htmlFilePath); BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8")); String readStr = ""; while ((readStr = br.readLine()) != null) { sb.append(readStr); } XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(sb.toString().getBytes("Utf-8")), null, Charset.forName("UTF-8")); } catch (Exception e) { e.printStackTrace(); } finally { if (null != document) { document.close(); } if (null != writer) { writer.close(); } if (null != fileInputStream) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != fileOutputStream) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /** * docx转pdf * @param pdfSaveDirectory:文件保存目录,例:D:\\pdf\\ * @param fileName:保存的文件名称,例:test.pdf * @param docxFile:需要转换的docx文件 * @return */ private static boolean docxConvertToPDF(String pdfSaveDirectory,String fileName,File docxFile) { // 不存在则新建 File directory = new File(pdfSaveDirectory); if (!directory.exists()) { directory.mkdirs(); } String pdfFilePath = pdfSaveDirectory + fileName; File outputFile = new File(pdfFilePath); try { InputStream docxInputStream = Files.newInputStream(docxFile.toPath()); OutputStream outputStream = Files.newOutputStream(outputFile.toPath()); IConverter converter = LocalConverter.builder().build(); converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); docxInputStream.close(); outputStream.close(); } catch (Exception e) { return false; } return true; } public static void main(String[] args) { try { String pdfFile = "D:\\test2.pdf"; String htmlContent1 = "

夫士大夫士大夫发

会见韩国机构行家

地方鬼地方鬼地方三个广泛的

"; String htmlContent = "" + "

夫士大夫士大夫发

会见韩国机构行家

地方鬼地方鬼地方三个广泛的

"; String htmlContent2 = "" + "

夫士大夫士大夫发

会见韩国机构行家

地方鬼地方鬼地方三个广泛的

"; htmlStringConvertToPDF(pdfFile,htmlContent); // parseHtml2PdfByFilePath(pdfFile,htmlFile); } catch (Exception e) { e.printStackTrace(); } } }