Generate PDF with Barcode – Step-by-Step Guide

  1. Make Folder for Files
  2. Install the Request Module
  3. Add Your API Key
  4. Generate Barcode
  5. Convert HTML to PDF
  6. Add Barcode to PDF
  7. Run the Program
  8. Demo

In this demonstration, we will generate a PDF from HTML and add a barcode to the output PDF. We will use the following endpoints to accomplish these tasks:

Below is the image of the sample HTML, Code 128 barcode, output PDF, and PDF with barcode.

Source File Sample and Generated PDF with Barcode
Source File Sample and Generated PDF with Barcode

Step 1: Make Folder for files

First, let’s add our sample HTML to our Python program folder. You can download our sample file and Python code here to follow along.

Step 2: Install the Requests Module

Next, if you don’t have the requests module yet, type in python -m pip install requests in your command line and it will install the requests module.

Step 3: Add your API Key

Now, open the Python sample code and add your API Key in line 7. You can get your API Key in your PDF.co dashboard.

PDF.co API Key

Step 4: Generate Barcode

To generate a Code 128, let’s specify the type and its value.

  • In line 15, type your result file name.
  • In line 17, enter the barcode type you’d like to use.
  • In line 19, enter the value of your barcode.

Barcode Generator Configuration

Step 5: Convert HTML to PDF

In line 122, add the HTML file that you want to convert to PDF.

HTML Sample File

Step 6: Add Barcode to PDF

Now, let’s configure the image param to add the Code 128 barcode to PDF.

  • In lines 209 and 210, enter the x and y coordinates where you want to add the barcode in the PDF.
  • In lines 211 and 212, enter your desired barcode height and width.

Add Barcode to PDF

Step 7: Run the Program

We are now ready to run the Python program. Check the folder to see the PDF output.

Result Program

Step 8: Demo

Let’s see the Python code in action.

PDF.co Barcode Generator Web API Demo in Python
PDF.co Web API Demo in Python

In this tutorial, you learned how to use the Barcode Generator Web API to generate a Code 128 barcode. You learned how to convert an HTML file to PDF using the HTML to PDF Web API. You also learned how to put all this together and add the Code 128 barcode to the generated PDF.

Related Pages: