简体   繁体   中英

iText html2pdf change size of the output PDF

How to change output PDF page size when converting it from HTML with iText html2pdf library? Tried this code:

ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription description = MediaDeviceDescription.createDefault();
description.setHeight(1024);
description.setWidth(1024);
properties.setMediaDeviceDescription(description);
HtmlConverter.convertToPdf(new File(htmlSource), new File("outputFile.pdf"), properties);

but, looks like it does not work, my output page is still A4

Use one of the HtmlConverter 's methods which take a PdfDocument instance as a parameter and set the needed page size on it with setDefaultPageSize method.

For example,

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationPath));
    pdfDoc.setDefaultPageSize(new PageSize(1500, 842));
    HtmlConverter.convertToPdf(new FileInputStream(sourcePath), pdfDoc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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