Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PdfMerger does not merge outlines of pdf documents with single pages #2597

Closed
fhg-isi opened this issue Apr 15, 2024 · 2 comments
Closed

PdfMerger does not merge outlines of pdf documents with single pages #2597

fhg-isi opened this issue Apr 15, 2024 · 2 comments

Comments

@fhg-isi
Copy link

fhg-isi commented Apr 15, 2024

In the following example, I would expect result.pdf to contain outline items. However, the outline is empty, due to

outline = self._trim_outline(reader, outline, pages)

The individual files first.pdf and second.pdf contain the expected outline entries.

import os

import matplotlib.pyplot as plt
from pypdf import PdfMerger, PdfReader


def main():

    x = [1, 2, 3, 4, 5]
    y = [2, 4, 6, 8, 10]

    fig, ax = plt.subplots()
    ax.plot(x, y)

    save_pdf_with_outline_title(fig, 'first.pdf', 'first_outline_title')

    save_pdf_with_outline_title(fig, 'second.pdf', 'second_outline_title')

    merger = PdfMerger()
    first_pdf = PdfReader('first.pdf')
    merger.append(first_pdf, import_outline=True)

    second_pdf = PdfReader('second.pdf')
    merger.append(second_pdf, import_outline=True)

    merger.write('result.pdf')


def save_pdf_with_outline_title(graph, file_path, outline_title):
    temp_file_path = file_path.replace('.pdf', '_temp.pdf')
    graph.savefig(temp_file_path, bbox_inches='tight')

    merger = PdfMerger()
    temp_pdf = PdfReader(temp_file_path)
    merger.append(temp_pdf, outline_item=outline_title)

    merger.write(file_path)
    os.remove(temp_file_path)


if __name__ == '__main__':
    main()
@stefan6419846
Copy link
Collaborator

PyPDF2 and PdfMerger are deprecated, please move to pypdf.PdfWriter. If the issue still persists, please update your code accordingly.

@fhg-isi fhg-isi changed the title PdfMerger does not merge outlines PdfMerger does not merge outlines of pdf document with single pages Apr 15, 2024
@fhg-isi fhg-isi changed the title PdfMerger does not merge outlines of pdf document with single pages PdfMerger does not merge outlines of pdf documents with single pages Apr 15, 2024
@fhg-isi
Copy link
Author

fhg-isi commented Apr 15, 2024

Switching to PdfWriter did the trick. (Switching from pypdf2 to pypdf and updating pypdf from 3.15.2 to 4.2.0 did not help.)

import os

import matplotlib.pyplot as plt
from pypdf import PdfWriter, PdfReader


def main():

    x = [1, 2, 3, 4, 5]
    y = [2, 4, 6, 8, 10]

    fig, ax = plt.subplots()
    ax.plot(x, y)

    save_pdf_with_outline_title(fig, 'first.pdf', 'first_outline_title')

    save_pdf_with_outline_title(fig, 'second.pdf', 'second_outline_title')

    writer = PdfWriter()
    first_pdf = PdfReader('first.pdf')
    writer.append(first_pdf, import_outline=True)

    second_pdf = PdfReader('second.pdf')
    writer.append(second_pdf, import_outline=True)

    writer.write('result.pdf')


def save_pdf_with_outline_title(graph, file_path, outline_title):
    temp_file_path = file_path.replace('.pdf', '_temp.pdf')
    graph.savefig(temp_file_path, bbox_inches='tight')

    writer = PdfWriter()
    temp_pdf = PdfReader(temp_file_path)
    writer.append(temp_pdf, outline_item=outline_title)

    writer.write(file_path)
    os.remove(temp_file_path)


if __name__ == '__main__':
    main()

@fhg-isi fhg-isi closed this as completed Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants