PDFBox COSStream 使用前关闭 [英] PDFBox COSStream closed before use

查看:128
本文介绍了PDFBox COSStream 使用前关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 pdf 生成器出现间歇性异常,该生成器在云中的 docker 容器中运行.生成器的一部分处理获取 SVG 文档并将其加载到 pdf 中.每 100 次调用它都会抛出以下异常importPageAsForm(tmpSVGPdf, 0).

We're getting intermittent exceptions from our pdf generator that runs in a docker container in the cloud. One portion of the generator handles taking an SVG document and loading it into a pdf. Every 100ish calls it throws the following exception from importPageAsForm(tmpSVGPdf, 0).

java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?

我们无法在本地重现此问题.

We haven't been able to reproduce this issue locally.

首先我们构建将包含加载的 svg 的 pdf:

First we build the pdf that will contain the loaded svg:

PDDocument pdf = new PDDocument();
PDPage page = new PDPage(new PDRectangle(width, height));
pdf.addPage(page);

然后我们打开一个PDF流&svg 转码器的输出流.

Then we open a PDF stream & an output stream for the svg transcoder.

try(PDPageContentStream stream = new PDPageContentStream(pdf, page, PDPageContentStream.AppendMode.APPEND,false, true))
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream())

当我们点击下面的 importPageAsForm 时,我们传入临时 SVG 文档,并在该函数内的某处点击关闭的 COSStream.我们使用相同的数据在本地运行该函数,它始终运行良好.

When we hit importPageAsForm below we pass in the temporary SVG document and somewhere within that function it hits a COSStream that is closed. We run the function locally with the same data and it always works fine.

TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(element.getEncodedData().getBytes()));
TranscoderOutput output = new TranscoderOutput(byteStream);
pdfTranscoder.transcode(input, output);
PDDocument tmpSVGPdf = PDDocument.load(byteStream.toByteArray());

LayerUtility layerUtil = new LayerUtility(pdf);
PDFormXObject svgObj = layerUtil.importPageAsForm(tmpSVGPdf, 0);
stream.drawForm(svgObj);
return Optional.of(pdf);

推荐答案

好的,所以在我最初的帖子中,我实际上有 'tmpSVGPdf.close()' 这一特定行在发布时未经测试,这是我的错.事实证明,这就是问题所在.我们没有关闭 tempSVG 并且出于某种原因导致了问题,尽管关闭是在异常抛出之后发生的.我们在 importPageAsForm() 调用之后插入了 close() &这个问题不再出现.去图!

Ok, so in my initial post I actual had 'tmpSVGPdf.close()' This particular line was untested at time of posting which is my fault. It turns out that this was the issue. We weren't closing the tempSVG and for some reason that was causing the problem, despite the fact the close comes after where the exception throws. We inserted close() after the importPageAsForm() call & the issue no longer presents itself. Go figure!

这篇关于PDFBox COSStream 使用前关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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