AttributeError: 'bytes' object has no attribute 'getPage'

Question:

Ive done some coding. Html and flask. Accepting pdf file from a form and watermark it using flask.

Here is my html coding:

<!DOCTYPE html>
<html>
<body>

<p>Click on the "Choose File" button to upload a file:</p>

<form action="/upload_file" method="POST" enctype = "multipart/form-data">
  <input type="file" name="file">
  <input type="submit">
</form>

</body>
</html>

Here is my flask coding:

@app.route('/upload_file', methods=['POST'])
def upload_file():

pdf_file = request.files['file']
watermark = "watermark.pdf"
merged_file = "merged.pdf"

input_pdf= pdf_file.read()
#input_pdf = PyPDF2.PdfFileReader(open(pdf_file,'rb'))

watermark_file = open(watermark,'rb')
watermark_pdf = PyPDF2.PdfFileReader(watermark_file)

pdf_page = input_pdf.getPage(0)

watermark_page = watermark_pdf.getPage(0)

pdf_page.mergePage(watermark_page)

output = PyPDF2.PdfFileWriter()

output.addPage(pdf_page)

merged_file = open(merged_file,'wb')
output.write(merged_file)

merged_file.close()
watermark_file.close()
input_pdf.close()

return "Success"

Traceback:

Traceback (most recent call last):
  File "C:UsersUserAppDataLocalProgramsPythonPython39Libsite-packagesflaskapp.py", line 2088, in 
__call__
    return self.wsgi_app(environ, start_response)
  File "C:UsersUserAppDataLocalProgramsPythonPython39Libsite-packagesflaskapp.py", line 2073, in 
wsgi_app
    response = self.handle_exception(e)
  File "C:UsersUserAppDataLocalProgramsPythonPython39Libsite-packagesflaskapp.py", line 2070, in 
wsgi_app
    response = self.full_dispatch_request()
  File "C:UsersUserAppDataLocalProgramsPythonPython39Libsite-packagesflaskapp.py", line 1515, in 
full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:UsersUserAppDataLocalProgramsPythonPython39Libsite-packagesflaskapp.py", line 1513, in 
full_dispatch_request
    rv = self.dispatch_request()
  File "C:UsersUserAppDataLocalProgramsPythonPython39Libsite-packagesflaskapp.py", line 1499, in 
dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:UsersUserDesktopabctest.py", line 57, in upload_file
    pdf_page = input_pdf.getPage(0)
AttributeError: 'bytes' object has no attribute 'getPage'

How to fix the AttributeError: ‘bytes’ object has no attribute ‘getPage’ error and also AttributeError: ‘bytes’ object has no attribute ‘mergePage’ error?

I need help. thank you.

Asked By: m.aiman syfq

||

Answers:

The relevant code is:

input_pdf= pdf_file.read()
pdf_page = input_pdf.getPage(0)

From a commented-out line (input_pdf = PyPDF2.PdfFileReader(open(pdf_file,'rb'))), it looks like you probably want to create a PdfFileReader object based on pdf_file. Since pdf_file is a file, try:

input_pdf = PyPDF2.PdfFileReader(pdf_file) # changed from pdf_file.read()
pdf_page = input_pdf.getPage(0)
Answered By: metaperture
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.