Replies: 3 comments
-
Replacing: new_pdf = pymupdf.open()
new_pdf.insert_pdf(docsrc=pdf, from_page=page_index, to_page=page_index)
pdf_page: pymupdf.Page = new_pdf[0] with: pdf_page: pymupdf.Page = pdf.reload_page(page=pdf[page_index]) # or pdf.load_page(page_id=page_index) fixes the issue when changing to a new page, but the issue still persists when going back to a page that the function has already been called on. Being able to go back to an already seen page is a requirement of mine. |
Beta Was this translation helpful? Give feedback.
-
Ok, I have found a working solution by using Any better solution to this would still be appreciated, as well as a solution to the original problem of widgets being rendered in the image on top of annotations which were added after the widgets. def generate_annotated_page_image(pdf: pymupdf.Document, page_index: int) -> bytes:
"""Generate an annotated page image from a PyMuPDF document page."""
pdf.fullcopy_page(pno=page_index)
pdf_page_copy = pdf[-1]
for annotation in pdf_page_copy.annots():
# TODO: only delete ones created by us?
pdf_page_copy.delete_annot(annot=annotation)
widgets = list(pdf_page_copy.widgets())
for widget_index, widget in enumerate(iterable=widgets):
_annotate_field(pdf_page=pdf_page_copy, widget=widget, widget_index=widget_index)
pdf_page_copy.delete_widget(widget=widget)
page_image_bytes = pdf_page_copy.get_pixmap(matrix=pymupdf.Matrix(2, 2)).tobytes()
pdf.delete_page(pno=-1)
return page_image_bytes |
Beta Was this translation helpful? Give feedback.
-
This is not a bug. I am going to move this to "Discussions" for possible comments anyone may have. |
Beta Was this translation helpful? Give feedback.
-
Description of the bug
I have created the reproducible code below for my issue.
In a nutshell, I have a UI that allows a user to edit the form fields (
pymupdf.Widget
) on a PDF. How they edit them is not important, but what is needed is each widget to be annotated with it's widget index on the page. Each page is converted to an image and then rendered on the UI with all of the widgets annotated with each ones index inside a red box.With some PDFs I use, somehow the image shows with the widgets on top of the annotations on the z-axis, causing the widgets to cover the annotations, even though the widgets have always existed on the PDF, and the annotations were added after.
Example:
To overcome this first issue, what I decided to do was:
This works as expected for the first page. However, strangely, every page after this first page that I attempt to create the image for in the same way, the newly copied page suddenly has 0 widgets on it, despite the page in the original PDF having widgets.
After calling the function once (expected results):
After calling the function again on different, and the same pages (unexpected results - all widgets and hence all annotations missing):
I have been trying to find a solution to either of these problems for the last 2 days, but cannot.
Output:
How to reproduce the bug
Run the script above, replacing
pdf_path = "/path/to/pdf/with/widgets.pdf"
with the path to a PDF with widgets on at least 2 pages.PyMuPDF version
1.25.3
Operating system
MacOS
Python version
3.12
Beta Was this translation helpful? Give feedback.
All reactions