Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't open PDF file with PyPDF2

I'm using Python 3.8.5. I'm trying to write a short script that concatenates PDF files and learning from this Stack Overflow question, I'm trying to use PyPDF2. Unfortunately, I can't seem to even create a PyPDF2.PdfFileReader instance without crashing.

My code looks like this:

import pathlib
import PyPDF2

pdf_path = pathlib.Path('1.pdf')
with pdf_path.open('rb') as pdf_file:
    reader = PyPDF2.PdfFileReader(pdf_file, strict=False)

When I try to run it, I get the following traceback:

Traceback (most recent call last):
  File "C:\...\pdf\open_pdf.py", line 6, in <module>
    reader = PyPDF2.PdfFileReader(pdf_file, strict=False)
  File "C:\...\.virtualenvs\pdf-j0HnXL2B\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__
    self.read(stream)
  File "C:\...\.virtualenvs\pdf-j0HnXL2B\lib\site-packages\PyPDF2\pdf.py", line 1883, in read
    stream.seek(-11, 1)
OSError: [Errno 22] Invalid argument

To help reproduce the problem, I created this GitHub repo with the above code and a sample PDF file.

What am I doing wrong?

like image 404
Amir Rachum Avatar asked Sep 26 '20 14:09

Amir Rachum


People also ask

Why is my PDF attachment not opening?

Here are some of the most common culprits to consider: Your laptop doesn't have a PDF reader installed. Your PDF reader or preferred program is out of date and needs an update. Your PDF application is potentially damaged or needs to be rebooted.

Can we open PDF in Vim?

in vim's official website, the definition of vim is clear: vim the editor It is not pdf reader, it is not MS-Word reader. You can of course write a pdf reader with other language, e.g. Java with itext lib.

How do I open a PDF in Visual Studio?

If you've got a PDF file in a project you can right-click in the solution explorer and choose "Open with..". Now you can choose which editor you want to use. If Acrobat isn't already listed you can add it and after that click the button "Set as default".


1 Answers

It seems like your 1.pdf file fails validation, checked here: https://www.pdf-online.com/osa/validate.aspx

I tried with another pdf file of version 1.7 and it worked, so it's not about pdf version, you just have a bad 1.pdf file

like image 112
GProst Avatar answered Sep 20 '22 01:09

GProst