Photo by iMattSmart on Unsplash

How to populate fillable PDF’s with Python

Andriy Andruhovski
3 min readMay 30, 2020

--

PDF forms are widely used in business and government organizations. For some reason, it can be useful to fill the form programmatically.

This sample shows how to do that with Aspose.PDF Cloud API.

In this example, we use the demo form with student personal info. The data for this form will be generated with Faker.

To fill out the form, we should create an asposepdfcloud.models.Fields object and fill the list property with appropriate values. Each item in list the property should be anasposepdfcloud.models.Field object. In the last object, we set name, value and type property. Usually, we use a asposepdfcloud.models.FieldType.TEXT type and set the string value. In the case of the Checkbox field, we should use a asposepdfcloud.models.FieldType.BOOLEAN type and pass a boolean value as a string.

To update the form we call self.pdf_api.put_update_fields(...).

The sample contains two classes: Student - for demo data and PdfCloudPyDemo for the form filling.

import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi
from asposepdfcloud.rest import ApiException
import os
import random
import shutil
from faker import Fakerclass Student(object):
def __init__(self):
fake = Faker()
self.firstName = fake.first_name()
self.lastName = fake.last_name()
self.grade = random.choice(["elementary", "middle", "high"])
self.dateOfBirth = "1998-12-12"
self.address = fake.last_name()
self.city = fake.city()
self.state = fake.state()
self.zipCode = fake.zipcode_plus4()
self.phone = fake.phone_number()
self.altPhone = fake.phone_number()
self.email = fake.email()
class PdfCloudPyDemo(object):
def __init__(self):
self.storageName = "First Storage"
self.folderName = "demos"
self.formName = "Student Info Form.pdf"
self.localFolder = "C:\\tmp\\"
# Get App key and App SID
# from https://dashboard.aspose.cloud/#/apps
self.pdf_api_client = asposepdfcloud.api_client.ApiClient(
app_key='*',
app_sid='*')
self.pdf_api = PdfApi(self.pdf_api_client)
def UpdateField(self, formName, student):
fields = asposepdfcloud.models.Fields()
fields.list = [
asposepdfcloud.models.Field(
name = 'First Name',
values = [student.firstName],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Last Name',
values = [student.lastName],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Grade',
values = [student.grade],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Date of Birth',
values = [student.dateOfBirth],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Address',
values = [student.address],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'City',
values = [student.city],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'State',
values = [student.state],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Zip',
values = [student.zipCode],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Phone',
values = [student.phone],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Alt Phone',
values = [student.altPhone],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Email',
values = [student.email],
type = asposepdfcloud.models.FieldType.TEXT),
asposepdfcloud.models.Field(
name = 'Volonteer1',
values = ["true"],
type = asposepdfcloud.models.FieldType.BOOLEAN),
asposepdfcloud.models.Field(
name = 'Volonteer2',
values = ["false"],
type = asposepdfcloud.models.FieldType.BOOLEAN)]

opts = {
"fields": fields,
"folder": self.folderName,
"storage": self.storageName
}
return self.pdf_api.put_update_fields(formName, **opts)
def FillForm(self):
try:
fileExistResponse = self.pdf_api.storage_exists(
path=self.folderName, storage=self.storageName)
if (fileExistResponse.file_exist.is_folder == False):
createFolderResponse = self.pdf_api.put_create_folder(
path=self.folderPDF, storage=self.storageName)
print(createFolderResponse)
else:
fileName = self.folderName + "/" + self.formName
fileExistResponse = self.pdf_api.get_is_exist(
path=fileName, storage=self.storageName)
if (not fileExistResponse.file_exist.is_exist):
print(f'Uploading {fileName}')
filePath = os.path.join(self.localFolder, formName)
with open(filePath, 'rb') as fp:
fileData = fp.read()
uploadResponse = self.pdf_api.put_create(
fileName, fileData)
randomStudent = Student()
updateResponce = self.UpdateField(fileName, randomStudent)
shutil.move(self.pdf_api.get_download(
f'{fileName}'), 'c:\\tmp\\res.pdf')
except ApiException as ex:
print(ex)
fillFormDemo = PdfCloudPyDemo()
fillFormDemo.FillForm()

--

--