yxh
3 天以前 68b49efb4da346edb42a5d8ebb39af47207402bd
ruoyi-common/src/main/java/com/ruoyi/common/utils/MergeFilesToPDFUtils.java
@@ -76,11 +76,16 @@
     * @param outputPath
     */
    public Boolean wordtoPdf(String inputPath, String outputPath) {
        // Spire.Doc 的 Document 持有文件句柄,必须显式关闭,否则句柄要等 finalize 才释放,且会占用源文件
        Document doc = new Document();
        try {
        //加载Word
        doc.loadFromFile(inputPath);
        //保存为PDF格式
        doc.saveToFile(outputPath, FileFormat.PDF);
        } finally {
            doc.close();
        }
        return true;
    }
@@ -91,18 +96,25 @@
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        log.info("fileName的全路径:{}", archived + "/" + fileName);
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(archived + "/" + fileName));
        // 合并过程中任何一步抛异常都必须关闭 document:document.close() 会级联关闭 PdfWriter 及其输出流,
        // 否则输出流句柄会被长时间占用(依赖 finalize 才能释放),且异常时文件内容不完整
        try {
        document.open();
        for (String pdfFile : pdfFiles) {
            PdfReader reader = new PdfReader(pdfFile);
                try {
            int totalPages = reader.getNumberOfPages();
            for (int page = 1; page <= totalPages; page++) {
                PdfImportedPage importedPage = copy.getImportedPage(reader, page);
                copy.addPage(importedPage);
            }
                } finally {
            reader.close();
        }
            }
        } finally {
        document.close();
    }
    }
}