简体   繁体   English

使用Java iText从HTML生成A5 PDF文档

[英]Generating an A5 PDF document from HTML using Java iText

I'd like to generate an A5 page-size PDF document from a given HTML template using the iText Java library. 我想使用iText Java库从给定的HTML模板生成A5页面大小的PDF文档。

I have been successful in generating a PDF, but rather than one A5 page, I get one A4 page (with the text on it) followed by an empty A5 page (see: the output document ). 我已经成功生成了PDF,但是得到的不是A5页,而是A4页(上面有文字),然后是空白的A5页(请参阅输出文档 )。 What am I doing wrong? 我究竟做错了什么?

A sample java code follows. 下面是一个示例Java代码。

Many thanks for help. 非常感谢您的帮助。


public final class Main {

    public static void main(String[] args) throws IOException, DocumentException {
        htmlToPdf(new StringReader("<html><head><title>Hello, World!!!</title></head><body style=\"font-family: 'Times New Roman', serif;\"><div>Hello, World!!!</div></body></html>"), new File("Test.pdf"));
    }

    private static void htmlToPdf(final StringReader htmlSource, final File pdfOutput) throws IOException, DocumentException {

        final FileOutputStream pdfOutputStream = new FileOutputStream(pdfOutput);
        final PdfDocument document = new PdfDocument();

        FontFactory.defaultEmbedding = true;

        document.setPageSize(com.itextpdf.text.PageSize.A5);

        final PdfWriter pdfWriter = PdfAWriter.getInstance(document, pdfOutputStream, PdfAConformanceLevel.PDF_A_1B);
        document.addWriter(pdfWriter);
        document.open();

        XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, htmlSource);

        document.close();
        pdfOutputStream.close();
    }
}

You're creating a PdfDocument instance instead of a Document instance! 您正在创建PdfDocument实例而不是Document实例! As documented, the PdfDocument class is for internal use only. 如所记录的, PdfDocument类仅供内部使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM