Skip to content

Problem to delete pages on PDF #4790

@Dayof

Description

@Dayof

Description of the bug

I tried to delete pages using delete_pages() but it seems to not have effect when calling tobytes().
I cannot use insert_pdf() to create a new document without the pages as well because of the problem below:
#3315

How to reproduce the bug

>>> import pymupdf
>>> with open("Marco_Aurelio_Meditaciones_02_pages.pdf", "rb") as f:
...     pdf_content = f.read()
... 
>>> file_copy = pymupdf.open("pdf", pdf_content)
>>> page_to_keep = 0
>>> pages_to_delete = [i for i in range(len(file_copy)) if i != page_to_keep]
>>> file_copy.delete_pages(pages_to_delete)
>>> print(f"After deletion: {len(file_copy)} pages")
After deletion: 1 pages
>>> result_bytes = file_copy.tobytes()
>>> result_doc = pymupdf.open("pdf", result_bytes)
>>> print(f"Result document has {len(result_doc)} pages")
Result document has 2 pages

This is the document:
Marco_Aurelio_Meditaciones_02_pages.pdf

PyMuPDF version

1.26.3

Operating system

Linux

Python version

3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions