Generate PDF with Barcode – Step-by-Step Guide
- Make Folder for Files
- Install the Request Module
- Add Your API Key
- Generate Barcode
- Convert HTML to PDF
- Add Barcode to PDF
- Run the Program
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.
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.
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.
Step 5: Convert HTML to PDF
In line 122, add the HTML file that you want to convert to PDF.
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.
Step 7: Run the Program
We are now ready to run the Python program. Check the folder to see the PDF output.
Step 8: Demo
Let’s see the Python code in action.
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.