亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超8個贊

在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 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號