I need to insert an image into some pages of a pdf and I use insertImage. Following the example I provide fitz.Rect(0, 0, 50, 50)
as I want to place the image in the top left corner of the page. Works perfectly for all pdfs, but one - a scanned document for which the image appears somewhere in the center of the page and the image is also 90 rotated. What might cause the difference in the result for that particular pdf and how can I solve it?
Asked
Active
Viewed 1,608 times
0

Mihail Panayotov
- 320
- 2
- 11
2 Answers
4
According to PyMuPDF documentation here, due to inconsistencies in how the PDF was created, it is possible the origin of that particular document is not the standard global origin on top-left.
The following snippet resets the geometry of the page:
if not(page._isWrapped):
page._wrapContents()
If this workaround doesn't work the best, there are other potential solutions listed on the site.

phwt
- 1,356
- 1
- 22
- 42

willing_astronomer
- 91
- 5
-
Thanks! Do you by any chance know how to fix the geometry regarding a ```fitz.Point``` which is needed for ```insertText```, text I am trying to place is in the wrong place again and none of the listed solutions work – Mihail Panayotov Dec 06 '19 at 08:43
-
I don't get your question there. Do you mean to say you're facing issues when plotting a point, but a rectangle is drawn at the correct location? Geometry is related to the document, and not the type of object you are trying to render. – willing_astronomer Dec 06 '19 at 09:46
-
Exactly, same point is rendered at different locations in different pdfs (one considers top left corner as the beginning while the other - bottom left). insertText uses Points to indicate first line of the text, hence my text appears in different places depending on the pdf – Mihail Panayotov Dec 06 '19 at 10:11
-
Sorry, the closest that I can find that is related is [this](https://github.com/pymupdf/PyMuPDF/issues/367) – willing_astronomer Dec 06 '19 at 11:28
0
I test with Python 3.7.3; PyMuPDF 1.20.2
no need to check page._isWrapped, ( actuall it will show:
AttributeError: 'Page' object has no attribute '_isWrapped'
)
only the below lines is enough:
import fitz
def test_func():
doc = fitz.open('test.pdf')
for page in doc:
page.wrap_contents()
# do some other stuff
doc.save('test-new.pdf')

Mic Ho
- 41
- 3