| | |
| | | package com.ruoyi.common.utils; |
| | | |
| | | import com.itextpdf.text.DocumentException; |
| | | import com.itextpdf.text.Image; |
| | | import com.itextpdf.text.pdf.PdfWriter; |
| | | import com.itextpdf.text.pdf.*; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.spire.doc.Document; |
| | | import com.spire.doc.FileFormat; |
| | | import com.spire.pdf.PdfDocument; |
| | | import com.spire.pdf.PdfDocumentBase; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.http.MediaType; |
| | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.util.List; |
| | | |
| | | @Configuration |
| | | @Slf4j |
| | | public class MergeFilesToPDFUtils { |
| | | |
| | | @Value("${ruoyi.archived}") |
| | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | /** |
| | | * pdf合并 |
| | | */ |
| | | public Boolean mergePDF(String[] pdfFiles, String fileName, HttpServletResponse response) { |
| | | try { |
| | | public void mergePdfFiles(List<String> pdfFiles, String fileName) throws IOException, DocumentException { |
| | | 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.open(); |
| | | |
| | | |
| | | PdfDocumentBase pdf = PdfDocument.mergeFiles(pdfFiles); |
| | | //Save the result to a PDF file |
| | | |
| | | pdf.save(archived + fileName, com.spire.pdf.FileFormat.PDF); |
| | | String filePath = archived + fileName; |
| | | |
| | | response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); |
| | | FileUtils.setAttachmentResponseHeader(response, fileName); |
| | | FileUtils.writeBytes(filePath, response.getOutputStream()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return false; |
| | | for (String pdfFile : pdfFiles) { |
| | | PdfReader reader = new PdfReader(pdfFile); |
| | | int totalPages = reader.getNumberOfPages(); |
| | | for (int page = 1; page <= totalPages; page++) { |
| | | PdfImportedPage importedPage = copy.getImportedPage(reader, page); |
| | | copy.addPage(importedPage); |
| | | } |
| | | reader.close(); |
| | | } |
| | | return true; |
| | | document.close(); |
| | | } |
| | | |
| | | } |