Generate PDF with Barcode in Python using PDF.co Web API

Sep 2, 2024·4 Minutes Read

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 are images of the sample HTML, Code 128 barcode, output PDF, and PDF with barcode.

Sample HTML
Sample HTML
Code 128 barcode
Code 128 barcode
Output PDF
Output PDF
PDF with Barcode
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 .

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

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

You just witnessed one of the many functionalities of PDF.co Barcode Generator Web API. It can also help you generate PDF invoices with barcodes, as well as other sensitive business documents

Related Tutorials

See Related Tutorials