Generate and Embed Barcodes & QR Codes in PDF via Python

Casie LiuCasie Liu
4 min read

Adding barcodes or QR codes to PDF documents can streamline document tracking and retrieval. These codes can encode machine-readable information such as document IDs, version numbers, and creation dates. In this guide, you'll learn how to generate and embed barcodes and QR codes in PDF files using Python—saving time and improving document workflow efficiency.

Before We Start: Tool Installation

To simplify the barcode generation process, this tutorial uses both Spire.PDF for Python and Spire.Barcode for Python.

Spire.PDF supports embedding 1D barcodes into PDF files, including Codabar, Code11, Code32, Code39, and Code93.

For QR code generation, we’ll use Spire.Barcode, as Spire.PDF only supports 1D barcode types.

You can install these two Python libraries using pip commands:

pip install spire.pdf and pip install spire.barcode.

How to Generate and Embed Barcodes in PDF Using Python Quickly

Spire.PDF makes it easy to generate barcodes on PDF pages. By creating specific barcode objects and customizing their properties, you can control the barcode content and appearance with flexibility. Below is a complete code example along with instructions—let’s see how it works in practice.

This code example shows how to create a new PDF and generate Codabar and Code39 barcodes on the first page:

from spire.pdf.common import *
from spire.pdf import *

# Create a PDF document
pdf = PdfDocument()
# Add a page
page = pdf.Pages.Add(PdfPageSize.A4())

y = 20.0
# Draw text on the page
font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Bold, True)
text = PdfTextWidget()
text.Font = font
text.Text = "Codabar:"
result = text.Draw(page, 0.0, y)
page = result.Page
y = result.Bounds.Bottom + 2

# Create a PdfCodabarBarcode object and draw it on the page
Codabar = PdfCodabarBarcode("1234-5678:90")
Codabar.BarcodeToTextGapHeight = 1.0
Codabar.EnableCheckDigit = True
Codabar.ShowCheckDigit = True
Codabar.TextDisplayLocation = TextLocation.Bottom
Codabar.TextColor = PdfRGBColor(Color.get_Green())
Codabar.Draw(page, PointF(0.0, y))
y = Codabar.Bounds.Bottom + 8

# Draw text on the page
text.Text = "Code39:"
result = text.Draw(page, 0.0, y)
page = result.Page
y = result.Bounds.Bottom + 2

# Create a PdfCode39Barcode object and draw it on the page
Code39 = PdfCode39Barcode("INV-2025/XYZ")
Code39.BarcodeToTextGapHeight = 1.0
Code39.TextDisplayLocation = TextLocation.Bottom
Code39.TextColor = PdfRGBColor(Color.get_Green())
Code39.Draw(page, PointF(0.0, y))

# Save the PDF document
pdf.SaveToFile("/output/GenerateBarcodeinPDF.pdf")
pdf.Close()

Result Preview:

Generate Barcode in PDF Using Python

Key steps explained:

  • Create a new PdfDocument object and add pages.

  • Draw text on the page.

  • Create a PdfCodabarBarcode instance, customize its properties, and use the Draw() method to insert the Codabar at a specific position.

  • Create a PdfCode39Barcode object, set its properties, and insert the Code39 barcode into the desired position using the Draw() method.

Generate and Embed QR Codes in PDF with Python Effortlessly

When you need to insert a QR code into a PDF, Spire.PDF alone isn't sufficient—you'll also need Spire.Barcode. Use the Python library to generate the QR code image, and then embed it into the PDF page using the DrawImage() method provided by Spire.PDF.

Below is a complete code example that you can copy and use directly. Just make sure to update the file paths as needed.

The code example below shows how to generate a QR code and embed it into a PDF:

from spire.pdf.common import *
from spire.pdf import *
from spire.barcode import *

# Create a BarcodeSettings object
settings = BarcodeSettings()

# Set the barcode type to QRCode
settings.Type = BarCodeType.QRCode
# Set barcode data, size, error correction level, etc.
settings.Data = "ABCD12345"
settings.Data2D = "ABCD12345"
settings.X = 2
settings.QRCodeECL = QRCodeECL.M
settings.ShowTextOnBottom = True

# Generate QR code image
barCodeGenerator = BarCodeGenerator(settings)
QRimage = barCodeGenerator.GenerateImage()

# Save the QR code image as a PNG file
with open("/output/QRCode.png", "wb") as file:
    file.write(QRimage)

# Create a PDF document
pdf = PdfDocument()
# Add a page
page = pdf.Pages.Add()

# Draw the QR code image onto the PDF page
pdfImage = PdfImage.FromFile("/output/QRCode.png")
page.Canvas.DrawImage(pdfImage, 0.0, 20.0)

# Save the PDF document
pdf.SaveToFile("/output/GenerateQRCodeinPDF.pdf")
pdf.Close()

Result Preview:

Generate QR Code in PDF with Python

Detailed steps explained:

  • Create a BarcodeSettings object and set its Type property to QRCode to specify the barcode type.

  • Configure the QR code’s data, module width (X), error correction level, and whether to display text.

  • Create a BarCodeGenerator object based on the settings, and use its GenerateImage() method to generate the QR code image.

  • Save the generated QR code as a PNG image file.

  • Create a PDF document and add a new page.

  • Load the QR code image and use the DrawImage() method to draw it onto the PDF page at a specified position.

  • Save the PDF document.

The Bottom Line

With the help of Spire.PDF and Spire.Barcode for Python, you’ve seen how to generate and embed both 1D barcodes and QR codes into PDF documents. This automated approach can significantly improve document traceability and reduce manual effort. If you're working with PDF files that require identifiers, version tracking, or quick scanning, integrating barcodes into your workflow can be a smart and efficient solution.

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu