Place image over PDF

后端 未结 7 1691
忘了有多久
忘了有多久 2020-12-01 02:04

How can I place an image over an existing PDF file at an specific coordinate location. The pdf represents a drawing sheet with one page. The image will be scaled. I\'m check

相关标签:
7条回答
  • 2020-12-01 02:58

    This is quite easy to do with PyMuPDF without merging two PDFs:

    import fitz
    
    src_pdf_filename = 'source.pdf'
    dst_pdf_filename = 'destination.pdf'
    img_filename = 'barcode.jpg'
    
    # http://pymupdf.readthedocs.io/en/latest/rect/
    # Set position and size according to your needs
    img_rect = fitz.Rect(100, 100, 120, 120)
    
    document = fitz.open(src_pdf_filename)
    
    # We'll put image on first page only but you could put it elsewhere
    page = document[0]
    page.insertImage(img_rect, filename=img_filename)
    
    # See http://pymupdf.readthedocs.io/en/latest/document/#Document.save and
    # http://pymupdf.readthedocs.io/en/latest/document/#Document.saveIncr for
    # additional parameters, especially if you want to overwrite existing PDF
    # instead of writing new PDF
    document.save(dst_pdf_filename)
    
    document.close()
    
    0 讨论(0)
提交回复
热议问题