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

Error while saving doc - MuPDF error: format error: object out of range (179 0 R); xref size 178 #3952

Closed
Prabakaran-psl opened this issue Oct 16, 2024 · 2 comments
Labels
not a bug not a bug / user error / unable to reproduce

Comments

@Prabakaran-psl
Copy link

Description of the bug

I'm adding some text to each page of pdf and saving the doc to new file. The below error is seen in the console while saving the doc
MuPDF error: format error: object out of range (179 0 R); xref size 178

How to reproduce the bug

`
doc = fitz.open(input_pdf)
total_pages = doc.page_count

    for page_num in range(total_pages):
        page = doc[page_num]
        page_width = page.rect.width
        page_height = page.rect.height
        text = f"Page {page_num + 1}"

        # Font settings
        font_size = 12
        font_name = "helv"  # Or another available font.  Fall back if not found.


        # Text box dimensions (add padding for background rectangle)
        text_width = fitz.get_text_length(text, fontname=font_name, fontsize=font_size)
        text_height = font_size * 1.2 # Add some extra height


        # Create white background rectangles
        rect_padding = 1 # Padding around text
        rect_points = [
            (10-rect_padding, rect_padding, 10 + text_width + rect_padding, text_height + rect_padding),       #Top-Left
            (page_width - 150 - rect_padding, rect_padding, page_width - 150 + text_width + rect_padding, text_height + rect_padding), #Top-Right
            (10 - rect_padding, page_height - 3 - text_height - rect_padding, 10 + text_width + rect_padding, page_height - 3 - rect_padding),  #Bottom-Left
            (page_width - 150 - rect_padding, page_height - 3 - text_height - rect_padding, page_width - 150 + text_width + rect_padding, page_height - 3 - rect_padding) # Bottom-Right
        ]

        for rect_point in rect_points:
          page.draw_rect(fitz.Rect(rect_point), color=(1,1,1), fill=(1,1,1)) #White rectangle

        # Add text (use calculated dimensions for better positioning)
        page.insert_text((10, 10), text, fontsize=font_size, fontname=font_name, color=(0, 0, 0)) #Top-Left
        page.insert_text((page_width - 150, 10), text, fontsize=font_size, fontname=font_name, color=(0, 0, 0)) # Top-Right
        page.insert_text((10, page_height - 10), text, fontsize=font_size, fontname=font_name, color=(0, 0, 0)) #Bottom-Left
        page.insert_text((page_width - 150, page_height - 10), text, fontsize=font_size, fontname=font_name, color=(0, 0, 0)) # Bottom-Right

    doc.save(output_pdf)

`

PyMuPDF version

1.24.11

Operating system

Linux

Python version

3.10

@JorjMcKie JorjMcKie added the not a bug not a bug / user error / unable to reproduce label Oct 16, 2024
@JorjMcKie
Copy link
Collaborator

  1. You did not provide a file showing the problem. Without this, no dealing with the problem is possible.
  2. This is very probably a damaged file according to the messages. The PDF contains references to non-existent objects.

Because of this (point 2.) I am going to close this post for now.
As we know nothing about your input file, our only advice is to check for any errors directly after open (e.g. doc.is_repaired and more).

@JorjMcKie
Copy link
Collaborator

If you can share the file, please feel free to reopen this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug not a bug / user error / unable to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants