为了账号安全,请及时绑定邮箱和手机立即绑定

iText如何同时使用PdfWriterPipeline和PdfSmartCopy?

iText如何同时使用PdfWriterPipeline和PdfSmartCopy?

叮当猫咪 2023-07-19 16:44:51
我正在使用iText将 HTML 转换为 PDF 文件。我意识到输出文件大小太大,因此我决定使用它PdfSmartCopy来删除对象重复项。PdfSmartCopy我可以在 HTML 到 PDF 过程完成后使用,例如,我用来PdfSmartCopy从磁盘加载文件并将 PDF 转换为较小的尺寸。我的问题是,我可以简化PdfSmartCopyHTML 到 PDF 的流程吗?我发现这PdfSmartCopy是 的子类PdfWriter。所以我将代码更改为:public static void main(String[] args) throws Exception {    try (OutputStream file = new FileOutputStream(new File("output.pdf"))) {        Document document = new Document(PageSize.A4);        //PdfWriter writer = PdfWriter.getInstance(document, file); // remove this line         PdfSmartCopy pdfCopy = new PdfSmartCopy(document, file); // change to this line        pdfCopy.setInitialLeading(12.5f);        document.open();        CSSResolver cssResolver = new StyleAttrCSSResolver();        CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream("itext2\\css\\bootstrap.min.css"));        cssResolver.addCss(cssFile);        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());        PdfWriterPipeline pdf = new PdfWriterPipeline(document, pdfCopy);        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);        XMLWorker worker = new XMLWorker(css, true);        XMLParser p = new XMLParser(worker);        String fileContent = PdfTest.readFile("itext2\\template.html");        p.parse(new StringReader(fileContent));        document.close();        pdfCopy.close();        file.close();    } catch (Exception e) {        e.printStackTrace();    }}PdfSmartCopy是否可以在进行 HTML 到 PDF 渲染的同时使用?https://api.itextpdf.com/iText5/5.5.9/com/itextpdf/text/pdf/PdfSmartCopy.html#PdfSmartCopy-com.itextpdf.text.Document-java.io.OutputStream-
查看完整描述

1 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

在HTML到PDF的渲染过程中,我将其保存到内存中,稍后用于PdfSmartCopy生成slim pdf版本,这可以防止我在文件系统中生成两个不同的pdf文件(稍后无法删除)非纤薄的),如果对其他人有帮助,请粘贴以下代码:


try (ByteArrayOutputStream byteData = new ByteArrayOutputStream()) {


    Document document = new Document(PageSize.A4);


    PdfWriter writer = PdfWriter.getInstance(document, byteData);           

    writer.setInitialLeading(12.5f);


    document.open();


    CSSResolver cssResolver = new StyleAttrCSSResolver();

    CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream("itext2\\css\\bootstrap.min.css"));

    cssResolver.addCss(cssFile);


    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());


    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);

    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);

    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);


    XMLWorker worker = new XMLWorker(css, true);

    XMLParser p = new XMLParser(worker);


    String fileContent = PdfTest.readFile("itext2\\template.html");


    p.parse(new StringReader(fileContent));


    document.close();


    PdfReader reader = new PdfReader(byteData.toByteArray());

    Document document1 = new Document();

    File resultFile = new File("result.pdf");

    PdfSmartCopy pdfCopy = new PdfSmartCopy(document1, new FileOutputStream(resultFile));

    document1.open();

    PdfImportedPage page;

    for (int pageNumber = 1; pageNumber <= reader.getNumberOfPages(); pageNumber++) {

        page = pdfCopy.getImportedPage(reader, pageNumber);

        pdfCopy.addPage(page);

    }


    document1.close();

    byteData.close();

} catch (Exception e) {

    e.printStackTrace();

}


查看完整回答
反对 回复 2023-07-19
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信