Skip to content
Advertisement

Error occurred while using PyPdf2 PdfFileMerger in Python

I have been creating a Python program using PyPdf2 to merge multiple pdf files.

Here is the code

import os
from PyPDF2 import PdfFileMerger

source_dir = os.getcwd()

merger = PdfFileMerger()

for item in os.listdir(source_dir):
    if item.endswith('pdf'):
        merger.append(item)

merger.write('completed_file.pdf')
merger.close()

while running the code i encountered the following error:-

Traceback (most recent call last):
  File "F:Python folderPdf_Mergermain.py", line 10, in <module>
    merger.append(item)
  File "F:Python folderPdf_Mergervenvlibsite-packagesPyPDF2merger.py", line 203, in append
    self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
  File "F:Python folderPdf_Mergervenvlibsite-packagesPyPDF2merger.py", line 151, in merge
    outline = pdfr.getOutlines()
  File "F:Python folderPdf_Mergervenvlibsite-packagesPyPDF2pdf.py", line 1362, in getOutlines
    outline = self._buildOutline(node)
  File "F:Python folderPdf_Mergervenvlibsite-packagesPyPDF2pdf.py", line 1444, in _buildOutline
    outline = self._buildDestination(title, dest)
  File "F:Python folderPdf_Mergervenvlibsite-packagesPyPDF2pdf.py", line 1425, in _buildDestination
    return Destination(title, page, typ, *array)
  File "F:Python folderPdf_Mergervenvlibsite-packagesPyPDF2generic.py", line 1065, in __init__
    raise utils.PdfReadError("Unknown Destination Type: %r" % typ)
PyPDF2.utils.PdfReadError: Unknown Destination Type: 0

Process finished with exit code 1

Note – I ensured that none of the pdf file is protected with password.

Advertisement

Answer

Update for July 2022

This was fixed and will be in the next release of PyPDF2!


Original Answer

It seems this is caused by bad destination syntax in the outline of one of the PDFs you’re trying to combine.

If you don’t care about the outline, you should be able to get around this by updating import_bookmarks kwarg to False in PdfFileMerger.append, like this:

import os
from PyPDF2 import PdfFileMerger

source_dir = os.getcwd()

merger = PdfFileMerger()

for item in os.listdir(source_dir):
    if item.endswith('pdf'):
        merger.append(item, import_bookmarks=False)

merger.write('completed_file.pdf')
merger.close()

More detail

PdfFileMerger.append calls PdfFileMerger.merge and passes the import_bookmarks kwarg to it. By default this is set to True.

In PyPDF2.generic, the Destination class is raising this error during initialization. The Merger is trying to build destinations into the new outline by reading them from the original outlines.

def __init__(self, title, page, typ, *args):
    DictionaryObject.__init__(self)
    self[NameObject("/Title")] = title
    self[NameObject("/Page")] = page
    self[NameObject("/Type")] = typ

    # from table 8.2 of the PDF 1.7 reference.
    if typ == "/XYZ":
        (self[NameObject("/Left")], self[NameObject("/Top")],
            self[NameObject("/Zoom")]) = args
    elif typ == "/FitR":
        (self[NameObject("/Left")], self[NameObject("/Bottom")],
            self[NameObject("/Right")], self[NameObject("/Top")]) = args
    elif typ in ["/FitH", "/FitBH"]:
        self[NameObject("/Top")], = args
    elif typ in ["/FitV", "/FitBV"]:
        self[NameObject("/Left")], = args
    elif typ in ["/Fit", "/FitB"]:
        pass
    else:
        raise utils.PdfReadError("Unknown Destination Type: %r" % typ)

Since the destination type “0” isn’t a valid type according to PDF Reference 1.7, it raises an error.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement