Sunday, May 1, 2016

PDF Library in Python – How To Create PDFs with Python

Using ReportLab with Python CSV module to Create PDF Files

Hi,
Today am going to work you through the process of generating let’s say:  “Course Completion Certificate” for any number of participants. This technique will work perfectly from one participant to hundreds and thousands of participants.

Let’s imagine a situation where you have to generate 2000 similar certificate with just few details/items changing from the template. That is to say details like; name, course title and grade will need to be unique for each generated certificate.

We are going to use the Python PDF library named: ReportLab.  It is not distributed with that standard library, so you’ll need to download it if you want to run the examples in this tutorial.
Reportlab supports most of the normal Python library installation methods. You have the option of using "pip install reporlab" or downloading the source and running “python setup.py install” or running a binary installer (on Windows).

ReportLab is used by companies such as Wikipedia, Hewlett Packard, and NASA.

Creating a Simple PDF

Let’s see how to actually create a basic pdf file! In Reportlab, the lowest-level component that’s used regularly is the canvas object from the pdfgen package which allow you to “paint” a document with your text, images, lines or whatever.

from reportlab.pdfgen import canvas
 
c = canvas.Canvas("fileName.pdf")
c.drawString(100,750,"This is my first PDF file in Reportlab!")
c.save()



The code above creates and save a .pdf file in the same working directory with the name "fileName.pdf". The "drawString" method draw text wherever you tell it to, in this case is at coordinate position (100,750). We can as well replace the content text "This is my first PDF file in Reportlab!" with a dynamic variable as we will see later.
This the very basic code to generate a pdf file with reportlab python library. You can find more tutorials and code snippet on reportlab website.


Generating Course Completion Certificate

Now let’s face the task at hand (to generate pdf certificates) as seen on the final outcome below.


Here, I have a list (saved in csv file) of participants that completed various computer programming courses at "ABC Computer Institute".



So, I want to generate a pdf certificate for all the participants on the fly against the traditional process of editing each certificate manually one-by-one.

Create a "pdf_script.py" file and "data.csv" file in the same folder location. Copy the code below and save it in the "pdf_script.py" file.

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.pagesizes import landscape
from reportlab.platypus import Image

import csv
import os

data_file = "data.csv"
pdfFolder = 'pdfFolder/'
if not os.path.exists(pdfFolder):
 os.makedirs(pdfFolder)

def main(data_file):
 attendee_data = csv.reader(open(data_file, 'rb'))
 for row in attendee_data:
  name = row[0]
  course_title = row[1]
  pdf_file_name = pdfFolder + name + '.pdf'
  generate_certificate(name, course_title, pdf_file_name)

def generate_certificate(name, course_title, pdf_file_name):
 c = canvas.Canvas(pdf_file_name, pagesize=landscape(letter))

 # Add image border template`
 image_path = 'background.jpg'
 c.drawImage(image_path, 0, 10, width=None, height=None)

 # Header Text
 c.setFont("Helvetica-Bold", 40, leading=None)
 c.drawCentredString(400, 500, 'Certificate of Completion')

 # Body Text
 c.setFont("Helvetica", 24, leading=None)
 c.drawCentredString(400, 420, 'This certifies that')

 # Participant Name Text
 c.setFont("Helvetica-Bold", 30, leading=None)
 c.drawCentredString(400, 340, name)

 # Draw line under the name - line(x,y, x,y)
 c.line(200,320, 600,320)

 # More body Text ...
 c.setFont("Helvetica", 24, leading=None)
 c.drawCentredString(400, 260, 'Has completed the ' + course_title + ' programming training course at')

 # School Name Text
 c.setFont("Helvetica-Bold", 24, leading=None)
 c.drawCentredString(400, 180, 'ABC Computer Institute')


 c.showPage()
 c.save()

main(data_file)


We started the sript by importing reportlab packages. All the ReportLab packages imported at the beginning of the script are located at: C:\Python27\Lib\site-packages\reportlab

The code above has two functions main() and generate_certificate(). While the main() function opens the csv file, fetch it row data and sets the path for created pdf files, the generate_certificate() function does the actual creation of the pdf files using ReportLab library.

For each created pdf file, only the name and course_title differs dynamically. The created files will be saved in a folder named "pdfFolder".


Note: You will have to play around with the sizes and coordinates positioning to arrive at a perfect result. In my case after playing around for 30 minutes, I settled for background image (background.jpg) of size 788x590 pixels.

The data don’t necessarily have to be in a .csv file, it can be in text file or excel file or a database.

Conclusion

Now you know the basics for creating PDFs in Python using Reportlab. There are a lot more that you can do with this excellent library, be sure you checkout the case studies, User Guide and tutorials on their website for more.

Thanks for reading



No comments:

Post a Comment