图像数据不足(PDF 文件生成) [英] Insufficient Data For An Image (PDF File Generation)

查看:64
本文介绍了图像数据不足(PDF 文件生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PDFBox 生成 PDF 文件,但是当我尝试绘制从字节数组接收的图像时,出现以下错误:

<块引用>

图像数据不足

这是我的代码的基本结构:

 public ByteArrayOutputStream generatePDF() {.. 变量声明//创建文档文档 = 新的 PDDocument();//创建页面for(int i = 0; i < arrayVar.length; i++) {//添加页面到文档页 = 新的 PDPage();//创建字体属性fontNormal = PDType1Font.HELVETICA;fontBold = PDType1Font.HELVETICA_BOLD;//建筑前线 &发票背面图片singleImageMap =//从 Web 服务调用获取带有字节数组的映射;if(singleImageMap != null && !singleImageMap.isEmpty()) {arrayFront = Utils.readImage((byte[]) singleImageMap.get(Constants.WS_IMAGE_FRONT));arrayBack = Utils.readImage((byte[]) singleImageMap.get(Constants.WS_IMAGE_BACK));fileFront = new ByteArrayInputStream(arrayFront);fileBack = new ByteArrayInputStream(arrayBack);bufferedImageFront = ImageIO.read(fileFront);bufferedImageBack = ImageIO.read(fileBack);rescaledFrontImg = Scalr.resize(bufferedImageFront, 500);rescaledBackImg = Scalr.resize(bufferedImageBack, 500);front = new PDJpeg(document, rescaledFrontImg);back = new PDJpeg(document, rescaledBackImg);}//接下来我们开始一个新的内容流,它将保存"要创建的内容.contentStream = new PDPageContentStream(document, page);//让我们定义内容流contentStream.beginText();contentStream.setFont(fontNormal, 8);contentStream.moveTextPositionByAmount(200, 740);contentStream.drawString("NAME: " + arrayVar[i].getParameter(Constants.NAME));contentStream.endText();如果(前面!= null && 后面!= null){contentStream.drawImage(front, 55, 500);contentStream.drawImage(back, 55, 260);}//添加页面document.addPage(page);//让我们关闭内容流contentStream.close();}//让我们创建OutputStream对象输出 = 新的 ByteArrayOutputStream();//最后让我们保存 PDF文件.保存(输出);文档.close();返回输出;}

<块引用>

因为我从 Web 服务收到一个 PNG 文件,所以我使用以下方法将其转换为 JPG:

 public static byte[] readImage(byte[] file) 抛出异常 {ImageInputStream is = ImageIO.createImageInputStream(new ByteArrayInputStream(file));BufferedImage originalImage = ImageIO.read(is);ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(originalImage, "jpg", baos );byte[] imageInByte = baos.toByteArray();返回 imageInByte;}

根据此链接:

<块引用>

https://issues.apache.org/jira/browse/PDFBOX-849

指出错误是因为应该在创建contentStream之前创建PDJepg对象,但这就是我在代码中所做的.

我不确定我的代码结构是否有问题,或者我处理从 Web 服务调用获得的图像字节的方式是否有问题.

有人知道可能是什么问题吗?

更新

我做了 Zelter Ady 的事情,确实我从 Web 服务获得的图像是有效的,因为我能够用它生成一个物理文件,所以问题应该出在图像的处理上,这件事我不知道我错过了什么.

解决方案

好在经过大量调试后发现问题出在这里:

front = new PDJpeg(document, rescaledFrontImg);back = new PDJpeg(document, rescaledBackImg);

PDJpeg 类有两个构造函数:

PDJpeg(PDDocument doc, BufferedImage bi)PDJpeg(PDDocument 文档,InputStream 是)

我正在传递一个 BufferedImage 并且在某些时候我仍然无法弄清楚,我认为所有字节都没有完全发送,因此我收到消息图像数据不足".

解决方案:我传递的是 InputStream 而不是 BufferedImage.

我仍然不知道为什么我在使用 BufferedImage 时遇到了那个错误,也许我需要做某种 .push()?

I'm using PDFBox to generate PDF files, however when I try to draw an image which I receive from an array of bytes I get the following error:

Insufficient data for an image

This is the basic structure of my code:

    public ByteArrayOutputStream generatePDF() {                

        .. Variable Declaration

        // Creating Document
        document = new PDDocument();

        // Creating Pages       
        for(int i = 0; i < arrayVar.length; i++) {

            // Adding page to document
            page = new PDPage();

            // Creating FONT Attributes
            fontNormal = PDType1Font.HELVETICA;
            fontBold = PDType1Font.HELVETICA_BOLD;

            // Building Front & Back Invoice Images         
            singleImageMap = // Getting Map With Array Of Bytes from Web Service Call;

            if(singleImageMap != null && !singleImageMap.isEmpty()) {

                            arrayFront = Utils.readImage((byte[]) singleImageMap.get(Constants.WS_IMAGE_FRONT));
            arrayBack = Utils.readImage((byte[]) singleImageMap.get(Constants.WS_IMAGE_BACK));

            fileFront = new ByteArrayInputStream(arrayFront);
            fileBack = new ByteArrayInputStream(arrayBack);                 
                bufferedImageFront = ImageIO.read(fileFront);
                bufferedImageBack = ImageIO.read(fileBack);                 

                rescaledFrontImg = Scalr.resize(bufferedImageFront, 500);
                rescaledBackImg = Scalr.resize(bufferedImageBack, 500);

                front = new PDJpeg(document, rescaledFrontImg);
                back = new PDJpeg(document, rescaledBackImg);

            }

            // Next we start a new content stream which will "hold" the to be created content.
            contentStream = new PDPageContentStream(document, page);                

            // Let's define the content stream
            contentStream.beginText();
            contentStream.setFont(fontNormal, 8);
            contentStream.moveTextPositionByAmount(200, 740);
            contentStream.drawString("NAME: " + arrayVar[i].getParameter(Constants.NAME));
            contentStream.endText();

            if(front != null && back != null) {
                contentStream.drawImage(front, 55, 500);
                contentStream.drawImage(back, 55, 260); 
            }           

            // Add Page
            document.addPage(page);

            // Let's close the content stream       
            contentStream.close();

        }

        // Let's create OutputStream object
        output = new ByteArrayOutputStream(); 

        // Finally Let's save the PDF
        document.save(output);
        document.close();

        return output;
    }

Since I receive a PNG file from the Web Service I do the conversion to JPG with the following method:

    public static byte[] readImage(byte[] file) throws Exception {

    ImageInputStream is = ImageIO.createImageInputStream(new ByteArrayInputStream(file));

    BufferedImage originalImage = ImageIO.read(is);         
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ImageIO.write(originalImage, "jpg", baos ); 

    byte[] imageInByte = baos.toByteArray();

    return imageInByte;
}

As per this link:

https://issues.apache.org/jira/browse/PDFBOX-849

It points out that the error is because the PDJepg object should be created before the creation of the contentStream, but that's what I do in my code.

I'm not sure if there is a problem with the structure of my code, or that maybe there is an error in the way I'm handling the image bytes I'm getting from the Web Service call.

Does anyone has an idea of what could be the problem?

UPDATE

I did what Zelter Ady and indeed the image that I'm getting from the Web Service is valid since I was able to generate a physical file with it, so the problem should be somewhere around the manipulation of the image, the thing is I don't know what I'm missing.

解决方案

Well after a lot of debugging I found that the problem was here:

front = new PDJpeg(document, rescaledFrontImg);
back = new PDJpeg(document, rescaledBackImg);

The PDJpeg class has two constructors:

PDJpeg(PDDocument doc, BufferedImage bi)
PDJpeg(PDDocument doc, InputStream is)

I was passing a BufferedImage and at some point that I still can't figure out, I assume all the bytes were not being completely sent thus I got the message "Insufficient Data For An Image".

Solution: I passed an InputStream instead of a BufferedImage.

I still don't know why I got that error using a BufferedImage maybe I needed to do some sort of .push()?

这篇关于图像数据不足(PDF 文件生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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