英文:
How to generate multi-page PDF from several html templates?
问题
我需要从几个 HTML 字符串生成 PDF 文件,每个字符串放在文档的单独页面上。我编写了以下方法:
public Optional<File> generatePdf(String[] htmlPages) {
try {
ConverterProperties properties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
FontProgram fontProgram = FontProgramFactory.createFont();
fontProvider.addFont(fontProgram);
File pdfFile = File.createTempFile("pdfdata", null);
PdfWriter pdfWriter = new PdfWriter(pdfFile);
PdfDocument pdf = new PdfDocument(pdfWriter);
PdfMerger merger = new PdfMerger(pdf);
for (int page = 0; page < htmlPages.length; page++) {
String htmlPage = htmlPages[page];
File pageFile = File.createTempFile("pagepdfdata", null);
PdfDocument temp = new PdfDocument(new PdfWriter(pageFile));
temp.setDefaultPageSize(new PageSize(795, 1042));
HtmlConverter.convertToPdf(htmlPage, temp, properties);
temp = new PdfDocument(new PdfReader(new FileInputStream(pageFile)));
merger.merge(temp, 1, temp.getNumberOfPages());
pageFile.deleteOnExit();
temp.close();
}
pdfFile.deleteOnExit();
return Optional.of(pdfFile);
} catch (IOException e) {
e.printStackTrace();
}
return Optional.empty();
}
然而,尽管每个页面的 PDF 文件被正确创建,但生成的 PDF 文件长度为 0 字节,无法打开。你能帮我解决这个问题吗?
英文:
I need to generate pdf file from several html strings, each of them placed on the separate page of the document. I wrote the following method:
public Optional<File> generatePdf(String[] htmlPages) {
try {
ConverterProperties properties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
FontProgram fontProgram = FontProgramFactory.createFont();
fontProvider.addFont(fontProgram);
properties.setFontProvider(fontProvider);
File pdfFile = File.createTempFile("pdfdata", null);
PdfWriter pdfWriter = new PdfWriter(pdfFile);
PdfDocument pdf = new PdfDocument(pdfWriter);
PdfMerger merger = new PdfMerger(pdf);
for(int page = 0; page < htmlPages.length; page++) {
String htmlPage = htmlPages[page];
File pageFile = File.createTempFile("pagepdfdata", null);
PdfDocument temp = new PdfDocument(new PdfWriter(pageFile));
temp.setDefaultPageSize(new PageSize(795, 1042));
HtmlConverter.convertToPdf(htmlPage, temp, properties);
temp = new PdfDocument(new PdfReader(new FileInputStream(pageFile)));
merger.merge(temp, 1, temp.getNumberOfPages());
pageFile.deleteOnExit();
temp.close();
}
pdfFile.deleteOnExit();
return Optional.of(pdfFile);
} catch (IOException e) {
e.printStackTrace();
}
return Optional.empty();
}
However, although pdf file for each page is created correctly, the resulting pdf file has 0 byte length and can not be opened. Could you please help me to resolve this problem?
答案1
得分: 0
我成功地通过以下方式重新编写代码来解决了这个问题:
public byte[] generatePdf(List<String> htmlPages) {
try {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(arrayOutputStream);
ConverterProperties properties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
FontProgram fontProgram = FontProgramFactory.createFont();
fontProvider.addFont(fontProgram);
properties.setFontProvider(fontProvider);
List<PdfDocument> docs = new ArrayList<>();
PdfDocument pdf = new PdfDocument(writer);
for(String htmlPage: htmlPages) {
File pageFile = File.createTempFile("pagepdfdata", null);
PdfDocument temp = new PdfDocument(new PdfWriter(pageFile));
temp.setDefaultPageSize(new PageSize(795, 1042));
HtmlConverter.convertToPdf(htmlPage, temp, properties);
temp = new PdfDocument(new PdfReader(new FileInputStream(pageFile)));
temp.copyPagesTo(1, temp.getNumberOfPages(), pdf);
pageFile.deleteOnExit();
docs.add(temp);
}
docs.forEach(PdfDocument::close);
pdf.close();
return arrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
英文:
I managed to solve this only by rewriting the code in the following way:
public byte[] generatePdf(List<String> htmlPages) {
try {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(arrayOutputStream);
ConverterProperties properties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
FontProgram fontProgram = FontProgramFactory.createFont();
fontProvider.addFont(fontProgram);
properties.setFontProvider(fontProvider);
List<PdfDocument> docs = new ArrayList<>();
PdfDocument pdf = new PdfDocument(writer);
for(String htmlPage: htmlPages) {
File pageFile = File.createTempFile("pagepdfdata", null);
PdfDocument temp = new PdfDocument(new PdfWriter(pageFile));
temp.setDefaultPageSize(new PageSize(795, 1042));
HtmlConverter.convertToPdf(htmlPage, temp, properties);
temp = new PdfDocument(new PdfReader(new FileInputStream(pageFile)));
temp.copyPagesTo(1, temp.getNumberOfPages(), pdf);
pageFile.deleteOnExit();
docs.add(temp);
}
docs.forEach(PdfDocument::close);
pdf.close();
return arrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论