简体   繁体   中英

PyMuPDF insert image at bottom

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.

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.

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')  

use:

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') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM