Stack Exchange
"The file temp.pdf could not be opened" How to get the text from a pdf with python?
I'm trying to get the text from a pdf with Python (one of my CV, pdf):
def extract_text_from_pdf_binary(pdf_binary):
    doc = fitz.open(stream=pdf_binary, filetype="pdf")
    num_pages = doc.page_count

    text = ''
    for page_number in range(num_pages):
        page = doc.load_page(page_number)
        text += page.get_text()

    doc.close()
    return text

@app.route('/', methods=['GET', 'POST'])
def index():
    form = UploadForm()
    if request.method == 'POST':
        file = request.files['file']
        binary = file.read()
        print(binary[0:300])
        text = extract_text_from_pdf_binary(binary)
        print("text: ", text)

However, I get:
b"\nendobj\n2 0 obj\n<< /Filter /FlateDecode /Length 10987 >>\nstream\nx\xda\xdd\x9d]s#U\x9an\xef\xf9\x15u31r\x04\x95\x93\xfb+?\xfa\x0e(\xe8\xa0\x07\x18\xa0\x98\xe9\x131=\x17*;\xb1\xd5\xd8r\xb5%\x17\xd4\xfc\xfa\xf3\xec\xfd\xa4\\\xe5\x02b\xbaYE\xc49\x13DP\xb6>\x96\x94\x99+\x95\x92^Y\xab\x7f\xd2\xeb\xbf\xbb\xcb\xf6O\xff\xe4\xdb?~\xd0?\xa9\xbf\xd4\x7f\x7fvN\x17\xe3\x93\xbe\xcbE\xff\x1b\xdb9\x8fN\xd0%>\xfe\xee\x83\x7f\xf9,\xceOB\xeeR\x1e\xe2\x93\xef\xbe\x7f2\x95.\x96\xf1\xc9\x98\x87n\xcc:\xe5\xe2\xc9\x7fn\xbe\xbe\x7fq\xbd;\xdf\x1ew\xb7\xfb\xc3\xd9\x7f}\xf7\xa7\xdft[\x9f~\xf7\xc1\xdf>\x08\xed\xf2\xe1\xcd\xad\xc4n\x1a\x87'\xe77\x1f\xfc\xe7\x7f\xf5O.t\xde\x9ft\xf94OO~l\x97\xbc\xd1o\xf3<\xe8\xa7\xeb'\xcf?\xf8\xe6m\xc2 BB\x84\xb1\x8byF\x84I\x84\x82\x08\xb3\x08\x91\x10f"
text:  

So it looks like there is stuff in file but extract_text_from_pdf_binary is not able to get it.
I tried with another pdf:
b'\nendobj\n17 0 obj\n<< /ProcSet [ /PDF /Text ] /ColorSpace << /Cs1 5 0 R >> /Font << /TT4 9 0 R\n/TT6 11 0 R /TT2 7 0 R /TT8 13 0 R >> >>\nendobj\n2 0 obj\n<< /Type /Pages /MediaBox [0 0 595.28 841.89] /Count 2 /Kids [ 1 0 R 15 0 R\n] >>\nendobj\n18 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n7 0 obj\n<< /T'
text:  

I tried other possibilities like saving the file and then extracting the text with pdfminer but when saving the file it seems to be invalid as I get The file “temp.pdf” could not be opened. error message when trying to open it:
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextBoxHorizontal

def extract_text_from_path(pdf_path):
    # extracted_text = preprocess_text(extracted_text)
    extracted_text = ''
    for page_layout in extract_pages(pdf_path):
        for element in page_layout:
            if isinstance(element, LTTextBoxHorizontal):
                # Convert the LTTextBoxHorizontal object to a string
                obj_str = element.get_text()
                # Remove line breaks
                # obj_str = obj_str.replace('\n', '')
                extracted_text += obj_str
    return extracted_text

@app.route('/', methods=['GET', 'POST'])
def index():
    form = UploadForm()
    if request.method == 'POST':
        file.save("temp.pdf")
        extract_text_from_path("temp.pdf")

And here I get:
[2023-06-14 12:17:12,336] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "app.py", line 108, in index
    extract_text_from_path("temp.pdf")
  File "app.py", line 69, in extract_text_from_path
    for page_layout in extract_pages(pdf_path):
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/pdfminer/high_level.py", line 208, in extract_pages
    for page in PDFPage.get_pages(
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/pdfminer/pdfpage.py", line 151, in get_pages
    doc = PDFDocument(parser, password=password, caching=caching)
  File "/Users/remplacement/PycharmProjects/flask-login/venv/lib/python3.8/site-packages/pdfminer/pdfdocument.py", line 752, in __init__
    raise PDFSyntaxError("No /Root object! - Is this really a PDF?")
pdfminer.pdfparser.PDFSyntaxError: No /Root object! - Is this really a PDF?
127.0.0.1 - - [14/Jun/2023 12:17:12] "POST / HTTP/1.1" 500 -

So it looks like there is stuff in file but extract_text_from_pdf_binary is not able to get it.
Here is the screenshot of the pdf:


are you reading the PDF file as binary before passing it to your function?

PDF isn't a document format. It's a file containing print instructions. It doesn't even have tables or paragraphs, those are emulated through print instructions that move to different parts of the page and drawing lines. In the binary output you showed /Length 10987 >>\nstream\nx\xda... seems to be a binary object, probably one of the images shown in the picture.

Yes @Obaskly ! And it doesn't work as I get a message that says the binary is invalid even if I send a pdf I was able to read (You can see it above). I also tried to save the pdf but I can't read the uploaded pdf (I get the "The file temp.pdf could not be opened" error message)

Text reading libraries try the best they can to convert whatever seems like consecutive text runs into paragraphs and text. That can easily fail. Everything depends on the application that generated the PDF file.

You're trying to solve two different problems at the same time: saving an uploaded file and extracting text from PDF. Solve one of them at a time. First try to retrieve and store the uploaded file. Make sure it's stored where you thing it's stored and is usable.  Then try to parse an existing PDF file on disk. Only once you solve both problems try to parse the uploaded file

Thanks for your comment @PanagiotisKanavos In the case of the one above the culprit is overleaf. But I also tried with another file for which I'm adding a screenshot in a minute. Anyway. When saving the pdf it seems to become unreadable. Is there anyalternative to file.save(...) ?

 file.read() can easily fail, as the file may still be in transit. Check the Uploading Files page in the Flask docs. The code there checks if the file is valid first and then saves it to disk in a known location.

Is there anyalternative to file.save(...) you didn't post any valid code that uses file.save to a known location. The first snippet uses read. The second doesn't retrieve the file from request.files and calls file.save with a relative location. Try the Flask docs first. If file.save didn't work, every Flask developer would have noticed
