Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3901

Merging PDF error: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.0.4, 2.0.7
    • Fix Version/s: None
    • Component/s: Utilities
    • Labels:
      None

      Description

      I am trying to merge around 2500 pdfs to one file. But I got the error below.

      Caused by: java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
      at org.apache.pdfbox.cos.COSStream.checkClosed(COSStream.java:77) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:125) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1200) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:383) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:522) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:460) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:444) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1096) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1367) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1254) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1232) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1204) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1192) ~[pdfbox-2.0.4.jar:2.0.4]
      at org.apache.pdfbox.pdmodel.PDDocument$save$0.call(Unknown Source) ~[?:?]

      My code is :

      public static void concatPdfs(List<String> sourceFiles, String dest) throws IOException {
       
              PDFMergerUtility ut = new PDFMergerUtility();
      
              PDDocument pdfDest = new PDDocument(MemoryUsageSetting.setupTempFileOnly());
      
              for(String source : sourceFiles) {
                  PDDocument pdfSource = PDDocument.load(new FileInputStream(source), MemoryUsageSetting.setupMainMemoryOnly());
                  ut.appendDocument(pdfDest, pdfSource);
                  pdfSource.close();
              }
      
              pdfDest.save(dest);
              pdfDest.close();
          }
      

      I was trying to find where COSStream is closed but it is difficult. Can anybody help to identify the root cause?

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              tzngvi Wei Zhang
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: