我尝试使用itext7将多个生成的pdf文件放入servlet的zip文件中,我已经成功地将一个pdf文件放入zip文件中,但没有更多。代码如下:
private void printMore(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
String masterPath = req.getServletContext().getRealPath("/assets/template/templateStatement.pdf");
try (PdfReader reader = new PdfReader(masterPath);
ZipOutputStream zipFile = new ZipOutputStream(resp.getOutputStream());
PdfWriter writer = new PdfWriter(zipFile);
PdfDocument pdf = new PdfDocument(reader, writer);
Document doc = new Document(pdf)) {
List<Student> studentList = getFactoryDAO().getStatementDAO().selectStudentHasBalance();
for (Student student : studentList){
// Generate PDF for the student
PdfPage page = pdf.getPage(1);
PdfCanvas canvas = new PdfCanvas(page);
FontProgram fontProgram = FontProgramFactory.createFont();
PdfFont font = PdfFontFactory.createFont(fontProgram, "utf-8", true);
canvas.setFontAndSize(font, 10);
canvas.beginText();
canvas.setTextMatrix(178, 650); // student code
canvas.showText(student.getS_Code());
canvas.setTextMatrix(200, 610); // Date of Statement
canvas.showText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
canvas.endText();
float[] pointsWidth = {60f,120f,70f,70f};
Table table = new Table(pointsWidth);
table.setMarginTop(280);
table.setMarginLeft(70);
table.setFont(font);
table.setFontSize(10);
table.setTextAlignment(TextAlignment.CENTER);
//Header Table
table.addCell(new Cell().add("Date Inscription"));
table.addCell(new Cell().add("Name"));
table.addCell(new Cell().add("Fees"));
table.addCell(new Cell().add("Observation"));
//Detail Table
table.addCell(new Cell().add(student.getTxnDate()));
table.addCell(new Cell().add(student.getS_FullName));
table.addCell(new Cell().add(student.getFees));
table.addCell(new Cell().add(student.getObservation));
doc.add(table);
ZipEntry zipEntry = new ZipEntry(student.getS_Code() + "_" + student.getS_LName() + ".pdf");
zipFile.putNextEntry(zipEntry);
//zipFile.write(); Shall I use it?
}
} catch (IOException e) {
e.printStackTrace();
}
resp.setHeader("Content-disposition","attachement; filename=test.zip");
resp.setContentType("application/zip");
}
我已经根据这篇文章和这篇文章,但不工作。我已经查看了更多类似的帖子,但是itext7的版本没有前面提到的pdfwriter.getinstance。我试过更多的东西,但都没能做得更好。
更新时间:
在enterman建议之后,我更新了它如下:
String masterPath = req.getServletContext().getRealPath("/assets/template/templateStatement.pdf");
try (PdfReader reader = new PdfReader(masterPath);
ZipOutputStream zipFile = new ZipOutputStream(resp.getOutputStream());
PdfWriter writer = new PdfWriter(zipFile);
PdfDocument pdf = new PdfDocument(reader, writer)
) {
List<Student> studentList = getFactoryDAO().getStatementDAO().selectStudentHasBalance();
for (Student student : studentList){
try (Document doc = new Document(pdf)){
// Generate PDF for the student
PdfPage page = pdf.getPage(1);
PdfCanvas canvas = new PdfCanvas(page);
FontProgram fontProgram = FontProgramFactory.createFont();
PdfFont font = PdfFontFactory.createFont(fontProgram, "utf-8", true);
canvas.setFontAndSize(font, 10);
canvas.beginText();
canvas.setTextMatrix(178, 650); // student code
canvas.showText(student.getS_Code());
canvas.setTextMatrix(200, 610); // Date of Statement
canvas.showText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
canvas.endText();
float[] pointsWidth = {60f,120f,70f,70f};
Table table = new Table(pointsWidth);
table.setMarginTop(280);
table.setMarginLeft(70);
table.setFont(font);
table.setFontSize(10);
table.setTextAlignment(TextAlignment.CENTER);
//Header Table
table.addCell(new Cell().add("Date Inscription"));
table.addCell(new Cell().add("Name"));
table.addCell(new Cell().add("Fees"));
table.addCell(new Cell().add("Observation"));
//Detail Table
table.addCell(new Cell().add(student.getTxnDate()));
table.addCell(new Cell().add(student.getS_FullName));
table.addCell(new Cell().add(student.getFees));
table.addCell(new Cell().add(student.getObservation));
doc.add(table);
ZipEntry zipEntry = new ZipEntry(student.getS_Code() + "_" + student.getS_LName() + ".pdf");
zipFile.putNextEntry(zipEntry);
//zipFile.write(); Shall I use it?
}
}
} catch (IOException e) {
e.printStackTrace();
}
resp.setHeader("Content-disposition","attachement; filename=test.zip");
resp.setContentType("application/zip");
但还是没有运气。
欢迎你的帮助。
2条答案
按热度按时间nxowjjhe1#
特别感谢benjamind、enterman和stack社区的支持。
我把这个答案贴在那些和我面临同样问题的人身上(我已经被困了4天了使用itext 7和Servlet生成多个pdf“
正如enterman所说:“需要新的文档示例”。此外还需要PDF文件。正如本雅明所说:“先生成一个pdf文件,然后把它们放进zip”。
它将把zip放在一个zip中(可以直接使用fileoutpustream将它放在一个zip中)。
再次感谢你。
cunj1qz12#
您应该尝试使用fileinputstream,如下所示(我自己的代码,在生产中运行良好)