Skip to content

Instantly share code, notes, and snippets.

@sueszli
Created August 8, 2023 23:52
Show Gist options
  • Save sueszli/8f856e331adcdb4acdd8c20785fb1f05 to your computer and use it in GitHub Desktop.
Save sueszli/8f856e331adcdb4acdd8c20785fb1f05 to your computer and use it in GitHub Desktop.
insert a png image into a pdf file
from reportlab.pdfgen import canvas
from PyPDF2 import PdfFileWriter, PdfFileReader
import webbrowser
inputPath = "input.pdf"
outputPath = "output.pdf"
imgPath = "logo.png"
tempPath = "temp.pdf"
imgSize = 70
x = 500
y = 760
c = canvas.Canvas(tempPath)
c.drawImage(imgPath, x, y, width=imgSize, height=imgSize)
c.save()
tempFile = PdfFileReader(open(tempPath, "rb")).getPage(0)
outputFile = PdfFileWriter()
inputFile = PdfFileReader(open(inputPath, "rb"))
for i in range(inputFile.getNumPages()):
page = inputFile.getPage(i)
page.mergePage(tempFile)
outputFile.addPage(page)
with open(outputPath, "wb") as out:
outputFile.write(out)
webbrowser.open(outputPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment