根据HTML内容设置iText 7页面大小

时间:2018-08-30 05:59:27

标签: itext itext7 html2pdf

我具有以下Java程序来创建iText PDF:

//Create the PDF file
public int CreatePDF(String[] pSrc, 
                     String pDest) 
           throws IOException 
{ 

    //Initialize
    ConverterProperties vProperties = new ConverterProperties();

    //Adding the fonts 
    FontProvider vfontProvider = new DefaultFontProvider(false, false, false);
    for (String font : FONTS) 
    {
        FontProgram vfontProgram = FontProgramFactory.createFont(font);
        vfontProvider.addFont(vfontProgram);
    }
    vProperties.setFontProvider(vfontProvider);

    PdfWriter vWriter = new PdfWriter(pDest, new WriterProperties().setCompressionLevel(9));
    PdfDocument vPDF = new PdfDocument(vWriter);
    PdfMerger vMerger = new PdfMerger(vPDF);

    //Convert to PDF
    for (String vHTML : pSrc)
    {
        ByteArrayOutputStream vArrByteAOS = new ByteArrayOutputStream();
        PdfDocument vDoc = new PdfDocument(new PdfWriter(vArrByteAOS, new WriterProperties().setCompressionLevel(9)));
        vDoc.setDefaultPageSize(new PageSize(735, 1080));
        HtmlConverter.convertToPdf(vHTML, vDoc, vProperties);
        vDoc = new PdfDocument(new PdfReader(new ByteArrayInputStream(vArrByteAOS.toByteArray())));
        vMerger.merge(vDoc, 1, vDoc.getNumberOfPages());
        vDoc.close();
    }
    vPDF.close();
    return 0;
}

如何基于HTML内容(pSrc)的大小设置“页面高度”(现在已硬编码为1080),以确保它适合一页。

0 个答案:

没有答案