How to convert pdf bytes files to normal files?

265 views Asked by At

I have this function written in pyhon. It takes a list of .pdf file objects and merges them into one .pdf file.

My problem is, that some of the files in the list are regular files and the merge fine, but some are byte files and they won't merge.

This is the code creating the bytes files from image objects and adding them to the list.

with open('file.pdf','wb') as f:
    f.write(img2pdf.convert(img))
    pdf_filer.append(f)
    img.close() 

I can't see a way to convert the bytes files into ordinary files. How is that done?

def merge_pdfs(paths, output):
    pdf_writer = PdfFileWriter()

    for path in paths:
        pdf_reader = PdfFileReader(path)
        for page in range(pdf_reader.getNumPages()):
            # Add each page to the writer object
            pdf_writer.addPage(pdf_reader.getPage(page))

    # Write out the merged PDF
    with open(output, 'wb') as out:
        pdf_writer.write(out)
0

There are 0 answers