I'm working with Python and Django and want to merge several PDF's into a single one.
I've seen several answers to this question, like this and this, which I've tried to apply but I'm getting errors.
First, this is the PDF data I'm working with: In my example, I have these two PDFs:
type(pdf1) = <class 'bytes'>
type(pdf2) = <class 'bytes'>
And if I print()
them, they look like this:
b'%PDF-1.5\n%\xb5\xed\xae\xfb\n4 0 obj\n<< /Length 5 0 R\n /Filter /FlateDecode\n>>\nstream\nx\x9c\x85Vmo\xdb6\x10\xfe\xae_q(VT\x06f\x99\x94\xac7 \x0b\x90\xa6.\x96\x01u\xdbD\xdf\x82\xc1\x90%\xca\xe6&\x93.E95\x86\xfd\xf7\x92\xa2$S\xa9\x87\xc1\xb0$\x92\x0f\x1f\xde=w<\x12\x03R\xbf9V\x8fd\x89\xa188\xdf\x1c\xd4\xf5\x89\x1d,r\x04\xbb\xc6y\x9f9\xd8\xefq>\x84\x91\xe7\x87\x10\xc5^\x90.\xc3\x00Cvp\x16\xd5\x1c\xcd\x11\xa8\xef\xcaq\xffy\x0b\r\x91p\xccwd#\xa9\xac\t\xfc\x06o\xd6\\\xd2\x82\x00\xaf\xe0\x91P\xd6\xc8\\\x92\x03a\xf2\r\xbc\xfd\x17\xcc\x84Y\xf6\x97\xa3\x17\xf0p\x14\xa2 \x85\xact\xdc:o\xe4F\x90\x82\xd0\x13)7\xf9\x81\xb7Lj\xb6_B\xe4!dM\x86R\x11\x8eH\r\xc1x\x8e\xc2\xb9\x8fp\xa2a3\xe7\n{?\xb5\xd9\xf3\x97M^\x14\x9a\\\xbd%=QyV\x14R\xb4\xc4Z\xa1\x835t\xc7r\xd9\n\xb2\xd9\x93\xbc\xecAW\xc9\xcdD\xca\x8a\xba-\t\xbc\xfb@\xaa\xbc\xad%\xfc\xae\xa6\x11\xf1N\xf3j\x87S/L\xd3$\\\x1aq\xc7\x96\x8f#\xc5\xb5D\x91\x0f8\x89\xbd\x08\xc78Iz\xa1\xf1 \xf4\xb3\xbb\xfe\x9c=\xdc\xaf\xe0\xf3Gx\\=\xac\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*\xff\xca)U\x9f\'\x13\xd3\xda\xc3\x96\x88\xa9i\xac>O\xe8:\xd1U\xdb\x19\x10%7\xf6\xa4\x96\x83\x96\xee\xd12\x
When I try merging them with like this:
from PyPDF2 import PdfFileMerger
pdfs = [pdf1, pdf2]
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write("result.pdf")
merger.close()
I get this error: embedded null byte
And when I try merging them like this:
from functools import partial
with open(fpath, 'rb') as f, open(target_fpath, 'wb') as target_f:
for _bytes in iter(partial(f.read, 1024), ''):
target_f.write(_bytes)
I get this other error: 'bytes' object has no attribute 'seek'
I think these errors are caused by the format of the data I'm handling. Should I decode the binary data before being able to apply the solutions I linked?