Error when rendering shapes to existing pdf #1168
-
Please provide all mandatory information! Describe the bug (mandatory)When I commit a set of shapes to a blank pdf, the result is as expected. However, when I try to commit them to an existing pdf, they are turned 90 degrees (upside down), rescaled and sometimes the text does not appear anymore. To Reproduce (mandatory)Consider an input as "original_document.pdf" (attached), where shapes and text coexist. Fitz can successfully extract the text and the shapes, and when I render them separately it works as expected (see only_shapes.pdf and only_text.pdf).
SHAPES EXTRACTION
THE ISSUE: COMMITING SHAPES TO AN EXISTING DOCUMENT
I open the document "only_text.pdf" and define its first page as outpage :
text_and_shapes__same_file.pdf It has to be mentioned that if I create a new page to an existing document, it correctly commits the shapes to this new page (however, this is not what I want). Expected behavior (optional)What I want is to be able to extract text and shapes from a pdf and generate them in a new document. Screenshots (optional)I am able to generate the shapes in a new page: However, when I try to commit them to an existing document, they appear upside down and rescaled (and in some cases, like this one, the text disappears): I assume that I am doing something wrong when I define outpage as an existing pdf page. Your configuration (mandatory)
For example, the output of PyMuPDF 1.18.14: Python bindings for the MuPDF 1.18.0 library. Additional context (optional)Thanks a lot!! |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
My compliments for this detailed issue description! The error occurs b/o a global geometry change in the target PDF that has not been wrapped propertly in protecting / localizing stacking commands "q" / "Q". See here for an explanation and how to solve: https://pymupdf.readthedocs.io/en/latest/faq.html#misplaced-item-insertions-on-pdf-pages. To detect / prevent this from happening,, check |
Beta Was this translation helpful? Give feedback.
-
It worked regarding the position of the shapes, now they are correctly rendered. Thank you very much! |
Beta Was this translation helpful? Give feedback.
-
Hm, the only thing I can think of now is that (some of) the shapes have a fill color, which then of course hides text underneath. |
Beta Was this translation helpful? Give feedback.
-
That would put the shape creating instructions before those painting the text ... |
Beta Was this translation helpful? Give feedback.
-
This approach would also have resolved your ominous problem with the "global geometry change". |
Beta Was this translation helpful? Give feedback.
-
Another option is of course simply not setting the shape fill color. |
Beta Was this translation helpful? Give feedback.
-
I set fill to None and shape.commit(overlay=False) and it completely worked: |
Beta Was this translation helpful? Give feedback.
Another option is of course simply not setting the shape fill color.
Or using some transparency (opacity) value < 1 ...