|
|
@@ -26,6 +26,10 @@ import org.springframework.util.StringUtils;
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.itextpdf.text.Document;
|
|
|
+import com.itextpdf.text.DocumentException;
|
|
|
+import com.itextpdf.text.pdf.PdfCopy;
|
|
|
+import com.itextpdf.text.pdf.PdfReader;
|
|
|
import com.uas.report.core.exception.SystemError;
|
|
|
import com.uas.report.service.PrintService;
|
|
|
import com.uas.report.service.ResourceService;
|
|
|
@@ -97,13 +101,39 @@ public class PrintServiceImpl implements PrintService {
|
|
|
fos.flush();
|
|
|
logger.info("Generated pdf file: " + pdfFile.getPath());
|
|
|
fos.close();
|
|
|
- } catch (IOException e) {
|
|
|
+ // 同时生成分页的pdf
|
|
|
+ writePagedPdfFiles(pdfFile.getPath());
|
|
|
+ } catch (IOException | DocumentException e) {
|
|
|
e.printStackTrace();
|
|
|
throw new SystemError(e.getMessage());
|
|
|
}
|
|
|
return pdfFilePath;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将给定的pdf文件生成多个分页的文件
|
|
|
+ *
|
|
|
+ * @param pdfFilePath
|
|
|
+ * 指定的pdf文件路径
|
|
|
+ * @throws IOException
|
|
|
+ * @throws DocumentException
|
|
|
+ */
|
|
|
+ private void writePagedPdfFiles(String pdfFilePath) throws IOException, DocumentException {
|
|
|
+ PdfReader pdfReader = new PdfReader(pdfFilePath);
|
|
|
+ for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
|
|
|
+ Document document = new Document();
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(pdfFilePath.replace(".pdf", "_" + i + ".pdf"));
|
|
|
+ PdfCopy pdfCopy = new PdfCopy(document, fileOutputStream);
|
|
|
+ document.open();
|
|
|
+ // 原pdf文件的每一页生成新的pdf
|
|
|
+ pdfCopy.addPage(pdfCopy.getImportedPage(pdfReader, i));
|
|
|
+ pdfCopy.close();
|
|
|
+ // 不关闭,生成的pdf无法正常打开
|
|
|
+ document.close();
|
|
|
+ fileOutputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 输出报表的数据
|
|
|
*
|