Skip to main content

Generate QR code using a URL link

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Generating a QR code for a PDF file is not a matter of embedding the entire file's data into the QR code. QR codes have a strict data limit (typically up to about 4,000 characters), which is far too small for most PDF documents 1.

The correct, industry-standard approach is to host the PDF online and encode the resulting direct URL into the QR code. When a user scans the code, their device opens the URL, which automatically downloads or displays the PDF.

Here is the guide for generating the QR code using a URL link.

Prerequisites and setup

You will use the familiar qrcode library with the Pillow extension for advanced image handling.

pip install qrcode[pil]

Hosting the PDF

Before running the code, you must upload your PDF to a publicly accessible location, such as:

  • A dedicated blog server (e.g., hrekov.com/blog/mypdf.pdf).
  • Cloud storage (Google Drive, Dropbox, AWS S3) set to public access.
  • A specialized file hosting service.

The resulting link (URL) is the data you will encode.


Python code example: encode PDF URL

We will use the modern StyledPilImage method to create a professional-looking QR code for your PDF URL, including custom colors and an optional logo for branding.

import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer

# --- Configuration ---
# 1. The public URL to your hosted PDF file
PDF_URL = "https://example.com/documents/whitepaper-2024.pdf"
OUTPUT_FILE = "pdf_download_qr.png"
LOGO_PATH = "logo.png" # Optional: file path for your brand logo

# --- Generation ---

# Initialize QR code with high error correction
# ERROR_CORRECT_H is recommended to tolerate minor imperfections,
# though Q-level is often sufficient for clean URLs 2.
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)

# Add the PDF URL data
qr.add_data(PDF_URL)
qr.make(fit=True)

# Create the final image with styling
qr_img = qr.make_image(
image_factory=StyledPilImage,
embedded_image_path=LOGO_PATH, # Embed the logo (optional)
module_drawer=RoundedModuleDrawer(), # Style the modules
fill_color="darkblue", # Code color
back_color="white" # Background color
)

# Save the final QR code image
qr_img.save(OUTPUT_FILE)
print(f"Successfully generated QR code for PDF URL: {OUTPUT_FILE}")

  • Use a static URL: Ensure the URL you encode will never change or expire. If the link breaks, every printed QR code becomes useless.
  • Keep the URL short: A shorter URL generates a simpler, less dense QR code, making it faster and easier to scan, especially if you are using a low-quality printer or smaller physical codes. Consider using a URL shortening service if your PDF link is very long 3.
  • Test on multiple devices: After generating the image, always test the QR code with several different scanners (iOS, Android) to ensure the PDF starts downloading or opens correctly.
  • Host on HTTPS: Modern browsers and scanners may flag or fail to open content linked via unencrypted http:// links. Always use secure https:// links.

Sources and further reading