Created
August 8, 2023 23:52
-
-
Save sueszli/8f856e331adcdb4acdd8c20785fb1f05 to your computer and use it in GitHub Desktop.
insert a png image into a pdf file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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