claird / PyPDF4

A utility to read and write PDFs with Python
obsolete-https://pythonhosted.org/PyPDF2/
Other
328 stars 61 forks source link

Bookmarks in merged pdf don't have a destination #87

Open edi-w opened 3 years ago

edi-w commented 3 years ago

When merging pdf files, the bookmarks from sourcefiles are inherited correctly but they don't point to any page. Also bookmarks that are created while appending files to the merger don't have a destination. I'm using python 3.7.6.

Example: merger = PdfFileMerger() merger.append(PdfFileReader(filepath,'rb'),bookmark='test',pages=None,import_bookmarks=True) merger.write(open(outfile, 'wb'))

pubpub-zz commented 3 years ago

Hi, I'm currently preparing a new version in a fork. It seems to work for me

Can you give me your feed back with my proposed version ?(not yet released but attached in here)

pypdf4-1.27.0PPzz_1-py2.py3-none-any.whl.zip

thanks,

edi-w commented 3 years ago

Hi,

I tried that but get this:

merger.append(filepath)

File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 146, in append self.merge(None, fileobj, bookmark, pages, import_bookmarks) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 116, in merge self._copy_bookmarks(fileobj.root_object["/Outlines"], bkmark, srcpages) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 171, in _copy_bookmarks self._copybookmarks(cur, bkmark1, srcpages) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 171, in _copy_bookmarks self._copybookmarks(cur, bkmark1, srcpages) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 171, in _copy_bookmarks self._copybookmarks(cur, bkmark1, srcpages) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 160, in _copy_bookmarks if Destination("", node).pageref.idnum in srcpages: File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\generic.py", line 2205, in init pdf.get_named_destinations()[page_or_dest_or_array].get_dest_array() UnboundLocalError: local variable 'pdf' referenced before assignment

pubpub-zz commented 3 years ago

I've just made a test similar to yours successfully. Your trouble may be linked with the content of your pdf file. Is it possible to have a copy? Thks

edi-w commented 3 years ago

file1.pdf file2.pdf

Here's example files with blank pages. I get the same error with these.

edi-w commented 3 years ago

Thanks, I tried and it worked as expected with those files. However, if I append the attached file, I get the following error:

Traceback (most recent call last): File "pdflistmerge.py", line 13, in merger.append(filepath) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 146, in append self.merge(None, fileobj, bookmark, pages, import_bookmarks) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 116, in merge self._copy_bookmarks(fileobj.root_object["/Outlines"], bkmark, srcpages) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\merger.py", line 162, in _copy_bookmarks if Destination("", node).pageref.idnum in srcpages: File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\generic.py", line 2196, in init pdf = page_or_dest_or_array.rawGet("/Parent").pdf File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\pypdf\generic.py", line 409, in rawGet return dict.getitem(self, key) KeyError: '/Parent' file3.pdf

pubpub-zz commented 3 years ago

Hi Edi-w, I have to appologize, I've seen your problem just after posting the message, that's why I deleted it. This should work now: pypdf4-1.27.0PPzz_1-py2.py3-none-any.whl.zip

edi-w commented 3 years ago

Hi, as far as I can tell, that seems to be working exactly as expected. Thanks!