Ptar
Ptar

Reputation: 374

How to correct this error PyPDF2.utils.PdfReadError: Cannot read an empty file

I have this code that returns an error

packet = io.BytesIO()

c = canvas.Canvas(packet)
packet.seek(0)
new_pdf = PdfFileReader(packet)
template = PdfFileReader(open('path_to_template'), "rb")
output = PdfFileWriter()
page = template.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
outputStream = open(output_path, "wb")
output.write(outputStream)
outputStream.close()

This is the error in details

PyPDF2.utils.PdfReadError: Cannot read an empty file

Upvotes: 3

Views: 4947

Answers (1)

Uzair Farooq
Uzair Farooq

Reputation: 21

I have faced the same error while trying to merge text on an existing pdf template. In my case the issue was that I was not saving the canvas. Try adding

c.save()

before

new_pdf = PdfFileReader(packet)

Upvotes: 2

Related Questions