sfneal / PyPDF3

A utility to read and write PDFs with Python
https://pythonhosted.org/PyPDF2/
Other
72 stars 15 forks source link

AttributeError: 'PdfFileWriter' object has no attribute 'stream' #9

Closed KARLSZP closed 3 years ago

KARLSZP commented 3 years ago

When I dump a pdf file using PdfFileWriter.write(FILESTREAM) like this:

def saveFiles(self, pages, path=None):
    resultFile = PyPDF3.PdfFileWriter()
    for page in pages:
        resultFile.addPage(page)
    if path:
        with open(path, "wb") as resultFileStream:
            resultFile.write(resultFileStream)
    else:
        with open(self.tmpPreviewFile, "wb") as resultFileStream:
            resultFile.write(resultFileStream)

First time, it appeared to succeed but something went wrong when I tried to save more pdfs.

Here's the error stack:

Traceback (most recent call last):
  File "...", line 104, in saveFiles
    resultFile.write(resultFileStream)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 502, in write
    self._sweepIndirectReferences(externalReferenceMap, self._root)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 621, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 597, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 621, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 597, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 606, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, data[i])
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 621, in _sweepIndirectReferences
    self._sweepIndirectReferences(externMap, realdata)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 597, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 597, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 597, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 638, in _sweepIndirectReferences
    newobj = self._sweepIndirectReferences(externMap, newobj)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 597, in _sweepIndirectReferences
    value = self._sweepIndirectReferences(externMap, value)
  File "C:\Program Files\Python38\lib\site-packages\PyPDF3\pdf.py", line 624, in _sweepIndirectReferences
    if data.pdf.stream.closed:
AttributeError: 'PdfFileWriter' object has no attribute 'stream'

I tried to modify the source code by removing the if phase: if data.pdf.stream.closed: ..., everything seems to be fine.

So is it a BUG or I was working incorrectly? Thanks in advance.

sfneal commented 3 years ago

Hmm it's hard to tell without knowing what PDF's caused the issue. Is it is possible to send me one that seemed to work and another that caused errors so I can further investigate? You may be right that the if data.pdf.stream.closed: statement can be removed.

sfneal commented 3 years ago

Closing this for now, feel free to reopen or submit a pull request