PDFBox COSStream 使用前关闭
PDFBox COSStream closed before use
我们的 pdf 生成器出现间歇性异常,运行 位于云端的 docker 容器中。生成器的一部分处理获取 SVG 文档并将其加载到 pdf 中。每 100ish 调用它都会抛出以下异常
importPageAsForm(tmpSVGPdf, 0).
java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
我们无法在本地重现此问题。
首先我们构建将包含加载的 svg 的 pdf:
PDDocument pdf = new PDDocument();
PDPage page = new PDPage(new PDRectangle(width, height));
pdf.addPage(page);
然后我们为 svg 转码器打开一个 PDF 流和一个输出流。
try(PDPageContentStream stream = new PDPageContentStream(pdf, page, PDPageContentStream.AppendMode.APPEND,false, true))
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream())
当我们点击下面的 importPageAsForm 时,我们传入了临时 SVG 文档,并且在该函数的某处它点击了一个关闭的 COSStream。我们 运行 在本地使用相同数据的函数,它总是工作正常。
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);
好的,所以在我最初的 post 中,我实际上有 'tmpSVGPdf.close()' 这条特定的线路在 posting 时未经测试,这是我的错。事实证明,这就是问题所在。我们没有关闭 tempSVG 并且出于某种原因导致了问题,尽管关闭发生在异常抛出之后。我们在 importPageAsForm() 调用之后插入了 close() 并且问题不再出现。去图吧!
我们的 pdf 生成器出现间歇性异常,运行 位于云端的 docker 容器中。生成器的一部分处理获取 SVG 文档并将其加载到 pdf 中。每 100ish 调用它都会抛出以下异常 importPageAsForm(tmpSVGPdf, 0).
java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
我们无法在本地重现此问题。
首先我们构建将包含加载的 svg 的 pdf:
PDDocument pdf = new PDDocument();
PDPage page = new PDPage(new PDRectangle(width, height));
pdf.addPage(page);
然后我们为 svg 转码器打开一个 PDF 流和一个输出流。
try(PDPageContentStream stream = new PDPageContentStream(pdf, page, PDPageContentStream.AppendMode.APPEND,false, true))
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream())
当我们点击下面的 importPageAsForm 时,我们传入了临时 SVG 文档,并且在该函数的某处它点击了一个关闭的 COSStream。我们 运行 在本地使用相同数据的函数,它总是工作正常。
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);
好的,所以在我最初的 post 中,我实际上有 'tmpSVGPdf.close()' 这条特定的线路在 posting 时未经测试,这是我的错。事实证明,这就是问题所在。我们没有关闭 tempSVG 并且出于某种原因导致了问题,尽管关闭发生在异常抛出之后。我们在 importPageAsForm() 调用之后插入了 close() 并且问题不再出现。去图吧!