title: PyPDF2: Stream has ended unexpectedly
body: <p>I have a Python script which uses PyPDF2 to reverse the order of pages of a PDF.</p>
  
  <pre><code>from  PyPDF2 import PdfFileWriter, PdfFileReader
  
  output = PdfFileWriter()
  rpage = []
  name = input("What's the file called?")
  
  filename = name.split('.', 1)
  
  input1 = PdfFileReader(open(name,'rb'), strict = False)
  
  pages = list(range(1,input1.getNumPages() + 1))
  
  for i in range(0, (input1.getNumPages())):
      rpage.append(pages[input1.getNumPages() - i -1])
  for i in rpage:
      output.addPage(input1.getPage(i-1))
  
  outputpath = filename[0] + '-reversed.pdf'
  
  outputStream = open(outputpath, "wb")
  output.write(outputStream)
  </code></pre>
  
  <p>Which functions as intended up until trying to write the output stream, where it returns this error:</p>
  
  <pre><code>PdfReadWarning: Invalid stream (index 59) within object 108 0: Stream has ended unexpectedly [pdf.py:1573]
  Traceback (most recent call last):
    File "D:\Documents\Google Drive\Programming\Python\PDF Scripts\reverse pdf.py", line 22, in &lt;module&gt;
  output.write(outputStream)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 482, in write
  self._sweepIndirectReferences(externalReferenceMap, self._root)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
  self._sweepIndirectReferences(externMap, realdata)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
  value = self._sweepIndirectReferences(externMap, value)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
  self._sweepIndirectReferences(externMap, realdata)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, value)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, data[i])
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
      self._sweepIndirectReferences(externMap, realdata)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, value)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 577, in _sweepIndirectReferences
      newobj = data.pdf.getObject(data)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1611, in getObject
      retval = readObject(self.stream, self)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\generic.py", line 66, in readObject
      return DictionaryObject.readFromStream(stream, pdf)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\generic.py", line 611, in readFromStream
      data["__streamdata__"] = stream.read(length)
  TypeError: integer argument expected, got 'NullObject'
  </code></pre>
  
  <p>The code does create a PDF file but it has a size of 0KB and is, therefore, unreadable. I have tested a sample script to merge three PDFs found <a href="https://github.com/mstamy2/PyPDF2/blob/master/Sample_Code/basic_merging.py" rel="nofollow noreferrer">here</a> which produces another empty file and results in this error:</p>
  
  <pre><code>PdfReadWarning: Invalid stream (index 59) within object 108 0: Stream has ended unexpectedly [pdf.py:1573]
  Traceback (most recent call last):
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1567, in _getObjectFromStream
      obj = readObject(streamData, self)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\generic.py", line 98, in readObject
      return NumberObject.readFromStream(stream)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\generic.py", line 269, in readFromStream
      num = utils.readUntilRegex(stream, NumberObject.NumberPattern)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\utils.py", line 134, in readUntilRegex
      raise PdfStreamError("Stream has ended unexpectedly")
  PyPDF2.utils.PdfStreamError: Stream has ended unexpectedly
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "D:\Documents\Google Drive\Programming\Python\PDF Scripts\untitled1.py", line 27, in &lt;module&gt;
      merger.write(output)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\merger.py", line 230, in write
      self.output.write(fileobj)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 482, in write
      self._sweepIndirectReferences(externalReferenceMap, self._root)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
      self._sweepIndirectReferences(externMap, realdata)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, value)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
      self._sweepIndirectReferences(externMap, realdata)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, value)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 556, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, data[i])
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 571, in _sweepIndirectReferences
      self._sweepIndirectReferences(externMap, realdata)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 547, in _sweepIndirectReferences
      value = self._sweepIndirectReferences(externMap, value)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 577, in _sweepIndirectReferences
      newobj = data.pdf.getObject(data)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1611, in getObject
      retval = readObject(self.stream, self)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\generic.py", line 66, in readObject
      return DictionaryObject.readFromStream(stream, pdf)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\generic.py", line 609, in readFromStream
      length = pdf.getObject(length)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1593, in getObject
      retval = self._getObjectFromStream(indirectReference)
    File "C:\Users\Charles\Anaconda3\lib\site-packages\PyPDF2\pdf.py", line 1576, in _getObjectFromStream
      raise utils.PdfReadError("Can't read object stream: %s"%e)
  PyPDF2.utils.PdfReadError: Can't read object stream: Stream has ended unexpectedly
  </code></pre>
  
  <p>The previous error is also outputted when this script is used to split a PDF into its constituent pages:</p>
  
  <pre><code>from PyPDF2 import PdfFileWriter, PdfFileReader 
  infile = PdfFileReader(open('test.pdf', 'rb'))
  
  for i in range(infile.getNumPages()):
      p = infile.getPage(i)
      outfile = PdfFileWriter()
      outfile.addPage(p)
      with open('page-%02d.pdf' % i, 'wb') as f:
          outfile.write(f)
  </code></pre>
  
  <p>The above code produces (n-1) readable PDFs but with nth PDF is an empty file. Any idea how I can fix this?</p>