Created
August 6, 2024 08:13
-
-
Save caueb/85ac1ace12036ef39b29fd22983ad199 to your computer and use it in GitHub Desktop.
Creates a PDF with a hyperlink covering the whole document.
This file contains hidden or 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
# Usage: | |
# python create-pdf.py -i lock.png -t "Confidental" -u https://localhost/malware.exe -x "This document is locked, click anywhere to open." -o mypdf.pdf | |
import argparse | |
from reportlab.lib.pagesizes import A4 | |
from reportlab.pdfgen import canvas | |
from reportlab.lib.utils import ImageReader | |
def create_pdf_with_image(image_path, output_path, title, url, text): | |
c = canvas.Canvas(output_path, pagesize=A4) | |
width, height = A4 | |
# Set the document properties and metadata | |
c.setTitle(title) | |
c.setCreator("Adobe Acrobat Pro") | |
c.setProducer("Adobe Acrobat Pro") | |
c.setSubject("Confidential document") | |
c.setAuthor("John Doe") | |
# Set the background color | |
c.setFillColorRGB(21/255, 22/255, 42/255) | |
c.rect(0, 0, width, height, fill=1) | |
# Load the image | |
image = ImageReader(image_path) | |
img_width, img_height = image.getSize() | |
# Calculate image position | |
img_x = (width - img_width) / 2 | |
img_y = height - img_height - 20 | |
# Draw the image at the top of the page | |
c.drawImage(image, img_x, img_y, width=img_width, height=img_height) | |
# Add the specified text below the image, if provided | |
if text: | |
c.setFont("Helvetica", 11) | |
# Set text color to gray | |
c.setFillColorRGB(220/255, 220/255, 220/255) | |
text_width = c.stringWidth(text, "Helvetica", 11) | |
c.drawString((width - text_width) / 2, img_y - 30, text) | |
# Add a hyperlink covering the whole page | |
c.linkURL(url, (0, 0, width, height), relative=1) | |
c.showPage() | |
c.save() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Create a PDF with a single image, title, URL, and optional text.') | |
parser.add_argument('--image', '-i', type=str, required=True, help='Path to the image file') | |
parser.add_argument('--title', '-t', type=str, required=True, help='Title of the PDF document') | |
parser.add_argument('--url', '-u', type=str, required=True, help='URL to link in the PDF document') | |
parser.add_argument('--text', '-x', type=str, help='Optional text to display below the image', default="") | |
parser.add_argument('--output', '-o', type=str, required=True, help='Path to the output PDF file') | |
args = parser.parse_args() | |
create_pdf_with_image(args.image, args.output, args.title, args.url, args.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment