添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to read a PDF and insert an image at bottom(Footer) of each page in PDF. I've tried with PyMuPDF library.

Problem: Whatever the Rect (height, width) I give, it doesn't appear in the bottom, image keeps appearing only on the top half of the page (PDF page).

My code:

from fitz import fitz, Rect
doc = fitz.open("test_pdf2.pdf")
def add_footer(pdf):
    img = open("footer4.jpg", "rb").read()
    rect = Rect(0, 0, 570, 1230)
    for i in range(0, pdf.pageCount):
        page = pdf[i]
        if not page._isWrapped:
            page._wrapContents()
        page.insertImage(rect, stream=img)
add_footer(doc)
doc.save('test_pdf5.pdf')

Processed_image with footer image in the middle of the page: https://i.stack.imgur.com/HK9mm.png

Footer image: https://i.stack.imgur.com/FRQYE.jpg

Please help!

Please let me know if this can be achieved by using any other library.

@BigDataGeek Yup, fortunately I was able to figure-out. Posted the same in the answers section. – Rohit Nimmala Jun 21, 2020 at 14:55

Some pages internally change the standard PDF geometry (which uses the bottom-left page point as (0, 0)) to something else, and at the same time do not encapsulate this doing properly as they should.

If you suspect this, use PyMuPDF methods to encapsulate the current page /Contents before you insert your items (text, images, etc.).

The easiest way is Page._wrapContents() which is also the friendly way to keep incremental PDF saves small. The MuPDF provided Page.cleanContents() does this and a lot more cleaning stuff, but bloats the incremental save delta, if this is a concern.

thanks for the answer, but it wasn't about encapsulation. I figured out the answer. posted the same. – Rohit Nimmala Jun 21, 2020 at 14:55

After doing little trial and error I was able to figure out the problem. I was missing out on proper dimensions of Rect, I had to give 0.85*h in second parameter. below is the code:

from fitz import fitz, Rect
doc = fitz.open("test_pdf2.pdf")
w = 595
h = 842
def add_footer(pdf):
    img = open("footer4.jpg", "rb").read()
    rect = fitz.Rect(0, 0.85*h, w, h)
    for i in range(0, pdf.pageCount):
        page = pdf[i]
        if not page._isWrapped:
            page._wrapContents()
        page.insertImage(rect, stream=img)
add_footer(doc)
doc.save('test_pdf5.pdf')  

wrapContents – Page.wrap_contents()

https://pymupdf.readthedocs.io/en/latest/znames.html?highlight=wrapContents

from fitz import fitz, Rect
doc = fitz.open("example.pdf")
w = 250
h = 400
def add_footer(pdf):
    img = open("firmaJG.png", "rb").read()
    rect = fitz.Rect(0, 0.85*h, w, h)
    for i in range(0, pdf.pageCount):
        page = pdf[i]
        if not page._isWrapped:
            page.wrap_contents()
        page.insertImage(rect, stream=img)
add_footer(doc)
doc.save('test_pdf5.pdf') 
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.