سؤال

I'd like to generate an A5 page-size PDF document from a given HTML template using the iText Java library.

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). What am I doing wrong?

A sample java code follows.

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! As documented, the PdfDocument class is for internal use only.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top