使用PdfMerger iText7将PdfDocument转换为byte [] [英] PdfDocument to byte[] using PdfMerger iText7

查看:160
本文介绍了使用PdfMerger iText7将PdfDocument转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我要在其中使用iText 7.1.11生成不同的pdf'.我正在使用PdfMerger即时合并所有pdf.我能够在本地系统上成功生成pdf,但是应用程序需要发送bye[]作为响应.我找到的解决方案

I've a requirement, where I am generating different pdf' using iText 7.1.11. I am using PdfMerger to merge all pdf's on the fly. I am able to generate pdf successfully at my local system, but the application needs to send bye[] in response. The solution I found here and here . but the problem is PdfMerger does not accept Document object, and I am not sure if i revert my code to use Document instead of PdfDocument will it work or not. Below is the code, with what I tried.

 public static void createPdf(List<String> src, String dest, PageSize pageSize, boolean rotate, String baseUri) throws IOException {
    ConverterProperties properties = new ConverterProperties();
    properties.setBaseUri(baseUri);
    FontProvider fontProvider = new DefaultFontProvider(false,false,true);
    properties.setFontProvider(fontProvider);
    
    /** tried this to make return byte[] in response
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfDocument pdf = new PdfDocument(new PdfWriter(byteArrayOutputStream));
    Document doc = new Document(pdfDoc); **/

    ///////////// Working on Local/////
    PdfWriter writer = new PdfWriter(dest); // 'dest' is local file system path 
    PdfDocument pdf = new PdfDocument(writer);
    PdfMerger merger = new PdfMerger(pdf);

    for (String html : src) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfDocument temp = new PdfDocument(new PdfWriter(baos));
        if(rotate) {
            temp.setDefaultPageSize(pageSize.rotate()); /** Page Size and Orientation */
        } else {
            temp.setDefaultPageSize(pageSize); /** Page Size and Orientation */
        }
        HtmlConverter.convertToPdf(html, temp, properties);
        temp = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
        merger.merge(temp, 1, temp.getNumberOfPages());
        temp.close();
    }
    pdf.close();}

请帮助我,因为这个简单的事情似乎很难实现

Please help me, as this simple thing seems difficult to achieve

推荐答案

这是初始化PdfMerger的方式:

PdfWriter writer = new PdfWriter(dest); // 'dest' is local file system path 
PdfDocument pdf = new PdfDocument(writer);
PdfMerger merger = new PdfMerger(pdf);

即您可以显式地写入本地文件系统,甚至在注释中强调该事实.

I.e. you explicitly write to the local file system and even stress that fact in the comment.

如果您想在最后的byte[]中包含合并的PDF,为什么不在这里简单地使用ByteArrayOutputStream(因为您声称您已经尝试过几行):

If you want to have the merged PDF in a byte[] at the end, why don't you simply use a ByteArrayOutputStream here (as you claim you have tried a few lines earlier):

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(byteArrayOutputStream );
PdfDocument pdf = new PdfDocument(writer);
PdfMerger merger = new PdfMerger(pdf);
...
pdf.close();
byte[] bytes = byteArrayOutputStream.toByteArray();

这篇关于使用PdfMerger iText7将PdfDocument转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆