Search code examples
pythonpython-3.xpdfpypdf

PyPDF2 return blank page when trying to extract first page


I have got an issue with the creation of PDF.

First I open the source pdf as a PdfFileReader and get the first page, after that, I add the first page to the pagelist of the output file (PDFFileWriter).

But when I go to check the outputfile it contains only a blank page, here is my code:

with open('pdf/'+articolo['itemfilename'], 'rb') as infile:
        reader = PdfFileReader(infile)
        writer = PdfFileWriter() #writer.addPage(reader.getPage(i))
        if articolo['copertina'] == 1:    #this is just a check it works I verified
            writer.addPage(reader.getPage(0))
with open('extracted/'+articolo['itemfilename'], 'wb') as outfile:
    writer.write(outfile)

I've already checked with the debugger that the program joins every row of the code, so everything should be fine.

Let me know if you have any questions.


Solution

  • Looks like this part of the code:

    with open('extracted/'+articolo['itemfilename'], 'wb') as outfile:
        writer.write(outfile)
    

    Should be indented inside the first indent block like this:

    with open('pdf/'+articolo['itemfilename'], 'rb') as infile:
        reader = PdfFileReader(infile)
        writer = PdfFileWriter() #writer.addPage(reader.getPage(i))
        if articolo['copertina'] == 1:    #this is just a check it works I verified
            writer.addPage(reader.getPage(0))
        with open('extracted/'+articolo['itemfilename'], 'wb') as outfile:
            writer.write(outfile)
    

    This worked for me.