PDFBox COSStream closed before use












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.



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);


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())


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);









share|improve this question




















  • 2




    I suspect you're closing tmpSVGPdf too early, maybe some objects are used in the destination doc, although importPageAsForm should make a proper clone. Also make sure you're using the latest PDFBox version, which one were you using?
    – Tilman Hausherr
    Nov 14 '18 at 5:11


















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.



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);


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())


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);









share|improve this question




















  • 2




    I suspect you're closing tmpSVGPdf too early, maybe some objects are used in the destination doc, although importPageAsForm should make a proper clone. Also make sure you're using the latest PDFBox version, which one were you using?
    – Tilman Hausherr
    Nov 14 '18 at 5:11
















0












0








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.



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);


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())


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);









share|improve this question















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.



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);


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())


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);






java svg pdfbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:51







Liam

















asked Nov 14 '18 at 3:49









LiamLiam

376




376








  • 2




    I suspect you're closing tmpSVGPdf too early, maybe some objects are used in the destination doc, although importPageAsForm should make a proper clone. Also make sure you're using the latest PDFBox version, which one were you using?
    – Tilman Hausherr
    Nov 14 '18 at 5:11
















  • 2




    I suspect you're closing tmpSVGPdf too early, maybe some objects are used in the destination doc, although importPageAsForm should make a proper clone. Also make sure you're using the latest PDFBox version, which one were you using?
    – Tilman Hausherr
    Nov 14 '18 at 5:11










2




2




I suspect you're closing tmpSVGPdf too early, maybe some objects are used in the destination doc, although importPageAsForm should make a proper clone. Also make sure you're using the latest PDFBox version, which one were you using?
– Tilman Hausherr
Nov 14 '18 at 5:11






I suspect you're closing tmpSVGPdf too early, maybe some objects are used in the destination doc, although importPageAsForm should make a proper clone. Also make sure you're using the latest PDFBox version, which one were you using?
– Tilman Hausherr
Nov 14 '18 at 5:11














1 Answer
1






active

oldest

votes


















0














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!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292933%2fpdfbox-cosstream-closed-before-use%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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!






    share|improve this answer


























      0














      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!






      share|improve this answer
























        0












        0








        0






        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!






        share|improve this answer












        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 22:55









        LiamLiam

        376




        376






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292933%2fpdfbox-cosstream-closed-before-use%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)