如何从多个HTML模板生成多页PDF?

huangapple go评论65阅读模式
英文:

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&lt;File&gt; 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(&quot;pdfdata&quot;, null);
        PdfWriter pdfWriter = new PdfWriter(pdfFile);
        PdfDocument pdf = new PdfDocument(pdfWriter);
        PdfMerger merger = new PdfMerger(pdf);
        for(int page = 0; page &lt; htmlPages.length; page++) {
            String htmlPage = htmlPages[page];
            File pageFile = File.createTempFile(&quot;pagepdfdata&quot;, 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&lt;String&gt; 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&lt;PdfDocument&gt; docs = new ArrayList&lt;&gt;();
        PdfDocument pdf = new PdfDocument(writer);
        for(String htmlPage: htmlPages) {
            File pageFile = File.createTempFile(&quot;pagepdfdata&quot;, 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;
}

huangapple
  • 本文由 发表于 2020年9月10日 00:07:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定