Add Barcodes in HTML to PDF

Python developers can add barcodes to their PDF documents with IronPDF for Python in two ways, as illustrated above:

Method 1: Add Barcodes Using the ChromePdfRenderer

Use this method to create barcodes that contain text-based information:

  1. Create a string variable that holds the following HTML elements:
    • A link element that references a barcode web font like this one.
    • An element containing the text that you want encoded in a barcode.
  2. Create a new ChromePdfRender object.
  3. Call the RenderHtmlAsPdf method on the new object with the string variable as the argument.
  4. Save the resulting PdfDocument object to a file.

Method 2: Add Barcodes Using the BarcodeStamper

Use this approach when you want more control over how the barcode appears (e.g, width, height, page positioning, etc.):

  1. Create a PdfDocument object, as shown below.

    # Create from existing PDF file on the file system
    existing_pdf = PdfDocument("existing.pdf")
    
    # Create from HTML!
    new_pdf = ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!</h1>")
    PYTHON
  2. Create a BarcodeStamper object, specifying the text to encode along with a target Barcode Format in the parameter list (width and height are optional).
  3. Call the applyStamp method on the PdfDocument object.
  4. Save the changes.

Use the IronBarcode C# Library for even more control over barcode creation. (And then stamp them onto your PDFs using IronPDF for Python's HtmlStamper!)