Contract Generation Software

Template-Based PDF Document Generation in Python

Overview

Template-based PDF document creation is the process of using ready-made templates and variable data to create professional, standardized documents. This document creation method is commonly used in business environments to streamline and automate the creation of documents such as invoices, bills, reports, etc.


Python is a programming language that is known for its flexibility and effectiveness in automating tasks. EDocGen allows users to generate PDF documents in Python using templates. With eDocGen, businesses can quickly and easily generate thousands of documents from various data sources such as JSON, XML, Excel, databases, and business applications. Generated documents can be distributed through diverse channels such as email, e-signature, etc. to provide a personalized and end-to-end experience to customers.



This article describes eDocGen's possibilities for the template-based generation of PDF documents using Python.


Prerequisites

Before you start using the platform for document generation using Python, ensure that your system has the latest version of Python installed. You can check this by running the following command:


``` python --version ```

This will show the version of Python currently installed on your system. If it is not the latest version, you may need to update it before proceeding.

After that, we will create a python file and name it "eDocGenapi.py" or any name you wish of your choice. At the top of this file, we need to include the following import statement:

```


from email import header
import requests
import uuid
import os.path
import json 

```


This will allow us to access the functions and features of the system API within our Python code. Once you have added the import statement, we can start using the platform to generate documents using templates using python.



Automated Document Generation Using Python


Edocgen's API makes it easy for developers to automate document generation with just a few lines of code. It supports various modes such as

  1. On-demand Generation
  2. Bulk generation
  3. Using a Database ( SQL or NoSql) etc.

Its flexibility makes it suitable for use in any document generation scenario within an enterprise. We will go through each use case step by step. To generate documents using the EDocGen REST API, you will need to follow these steps:

Authentication


eDocGen API uses a JWT-based authentication token for all the API interactions. You can register and obtain an access token by passing your username and password to the /login endpoint. You will need to use this token for every API call.


We will create a init method in our python code to fetch the token as below:

```


class eDocGenapi:
    token = ""
    def __init__(self):
        base_url = "https://app.eDocGen.com"
        url = "%s/login" % base_url
 
        payload = "{\n\t\"username\":\"\",\n\t\"password\": \"\"\n}"
        headers = {
            'content-type': "application/json",
            'cache-control': "no-cache",
            }
 
        response = requests.request("POST", url, data=payload, headers=headers)
        x_access_token = response.json()['token']
        eDocGenapi.token = x_access_token
        print(x_access_token)
    

```

In this method, we will be requesting the server to provide an authentication token and store it as a variable so we can use it later.

Once the token is obtained the sequence for generating a document using the API is as follows:

Prepare Template


The system enables users to customize and upload their templates. Therefore, the first step is to create a new template document.

This template can either be created in a pdf or a Microsoft word document. . You can simply create the document with dynamic fields enclosed by curly braces {}. In the system terminology, these dynamic fields are known as tags “Tags”.

For example, in the below template, the variable {Invoice_Number} and {Invoice_Date} are configured to be replaced with text from the input data. Dynamic tables can also be created by enclosing rows in the table with {#tablename} and {/tablename} tags and so on.





These tags will be used by the server to dynamically insert a variety of content, including text, images, tables, etc as the tags from the input data file as shown below:



This is a one-time activity and all records will use this template to generate corresponding documents.

Upload the Template


Once the template is ready we can upload the created template with two options:


  1. Using system UI: Login to the portal and click on the left sidebar “Template” menu. On clicking the upload button we can upload our template.



  1. Using EdocGen API in Python:

It provides the following API which can be used to upload the template file to the system server.

Method

Endpoint

Description

POST

/api/v1/document

Upload template to the system


Parameter Type Description

documentFile

file

(formData) file to upload


This API accepts a single parameter of type "formData" called "documentFile". This parameter represents the file to be uploaded as the template.

Get Template ID Using Filename


Once our template is uploaded to the eDocGen server, then we can retrieve the template ID. This ID will be useful in performing various API operations with the given template.

eDocGen provides the following API which can provide the template ID.


Method

Endpoint

Description

GET

/api/v1/document/{filename}

Returns all template details


Parameter Type Description

filename

(path)

Filename of the template. E.g. edocden.docx


To achieve this using python code we need to do the following:

```


@classmethod
    def getTemplateIdviaFileName(self, filename):
        base_url = "https://app.eDocGen.com"
        #Generate request
        url = "%s/api/v1/document/?search_column=filename&search_value=%s" % (base_url, filename)
        headers = {
             'x-access-token': eDocGenapi.token
        }
        response = requests.get(url, headers=headers)
        output = response.json()["documents"]
        #print (output)
 
        print('Template Ids are as follow for given filename')
        for d in output:
            print(d['_id'])


```

The above code retrieves the template ID for a given file name using the eDocGen API. It makes a GET request to the API endpoint "/api/v1/document/" with the specified file name as a query parameter and processes the response to extract and print the template IDs.

Generate Document


To generate a document using the system service, the following endpoint is provided:

Method Endpoint
POST

/api/v1/document/generate/bulk


Parameter Description Type

documentId

id of the template

Form Data

format

pdf/docx (Format should be supported by the template)

Form Data

outputFileName

The file name for the output file

Form Data

inputFile

The file contains marker values. json, xlsx and xml supported

Form Data


The file can be in a variety of formats, including JSON, XLSX, and XML. By using the /api/v1/document/generate/bulk endpoint, you can quickly and easily generate a large number of documents with a single API call, saving time and simplifying the process of creating professional, customized documents with eDocGen.

This API support two modes of generation:


Synchronous Generation: This mode is useful when generating documents using a single record. Here the output is generated synchronously immediately.

Asynchronous Generation: This mode is useful when generating documents using multiple records. API can automatically compress them into a .zip file, making it easy to download the output. This feature is particularly useful when generating a large number of documents, as it allows you to efficiently download and manage the output.

Using python we can perform this easily as below:

```


 @classmethod
    def downloadBulkJson(self):
        inputFile = "JSON_Data.json"
        generated_format = "pdf"
        documentId = "62f063426844520f75344091"
        format = "pdf"
        keyToFileName = "prefix"
        base_url = "https://app.eDocGen.com"
 
        with open(inputFile, 'r') as f:
            data = json.load(f)
        #print(data)
        #print(len(data))
 
        if ( len(data) > 1):
            generated_format = "zip"
        #Generate request
        url = "%s/api/v1/document/generate/bulk" % base_url
        # the output file extension will be added automatically by the system
        outputFileName = "%s" %uuid.uuid4()
 
        inputValues = {
        'documentId': (None, documentId),
        'format': (None, format),
        'outputFileName': (None, outputFileName),
        'keyToFileName': (None, keyToFileName),
        'inputFile': (os.path.basename(inputFile), open(inputFile, 'rb'), 'application/octet-stream')
        }
 
        headers = {
             'x-access-token': eDocGenapi.token
        }
        response = requests.post(url, files=inputValues, headers=headers)
        print("Generate document: %s" % response.json())
        # add extension again due to internal bug
        outName = "%s.%s" % (response.json()['outFile'], generated_format)
        #Wait for file to get generated and download
        url = "%s/api/v1/output/name/%s" %(base_url,  outName)
        print("Fetching files with name using url: %s" %url)
 
        response = requests.get(url, headers=headers)
        output = response.json()["output"][0] if "output" in response.json() and len(response.json()["output"]) > 0 else None
        generated_file_id = None
        while(not output):
            print("waiting for file to get generated...", outName)
            response = requests.get(url, headers=headers)
            output = response.json()["output"][0] if "output" in response.json() and len(response.json()["output"]) > 0 else None
 
        generated_file_id = output["_id"]
        print("generated file id: %s" % generated_file_id)
 
        # Download the generated file
        url = '%s/api/v1/output/download/%s' %(base_url, generated_file_id)
        download_to_file = "./%s.%s" % (generated_file_id,generated_format )
        response = requests.get(url, headers=headers, allow_redirects=True)
        print("file saved via name : %s" % download_to_file)
        open(download_to_file, 'wb').write(response.content)

```

In this code, we created a method called 'downloadBulkJson', which generates and downloads the documents. The method starts by opening the input JSON file and loading the data into a Python dictionary. If the data contains more than one item, the method sets the 'generated_format' variable to 'zip', which means that the generated documents should be compressed into a .zip file. Otherwise, if there is a single document then it is downloaded in the set format.

Next, we send a POST request to the /api/v1/document/generate/bulk endpoint with the required parameters such as input file, output file name, and format. Our function waits for the output file to be generated, once the output is generated we downloads and saves it to the local file system with a file name based on the generated file UUID. This UUID makes the file's name unique at any point in time.


Generate Bulk Document From Database


The eDocGen also provides the flexibility to generate documents from records that are saved into databases such as SQL tables, MongoDB collections, etc. The endpoint API is the same as document generation using JSON but for reading records using databases we need to provide additional metadata details such as connection details, the number of records to be fetched, table name, etc.


API parameters for generating documents using SQL DB are as follows:

Method Endpoint

POST

/api/v1/document/generate/bulk



Parameter Type Description

documentId

string

(formData) id of the template

dbVendor

string

(formData) Database vendor

dbUrl

string

(formData) Database connection url

dbPassword

string

(formData) Database password

dbQuery

string

(formData) Database select query

dbLimit

string

(formData) Database limit number of rows from select query

format

string

(formData) output format docx or pdf. Default docx

outputFileName

string

(formData) file name for the output file


After that eDocGen will take care of generating the documents automatically.


Using python we can use this API as below:

```


                   
@classmethod
    def downloadBulkDB(self):
 
        format = "pdf"
        dbVendor = "mysql"
        dbUrl ="jdbc:mysql://sql6511576@sql6.freesqldatabase.com:3306/sql6511576/sdtest"
        dbPassword = "thepassword"
        dbQuery = "select * from sdtest"
        dbLimit = "100"
        documentId = "62f063426844520f75344091"
        keyToFileName = "prefix"
 
        generated_format = "zip"
        base_url = "https://app.eDocGen.com"
 
        #Generate request
        url = "%s/api/v1/document/generate/bulk" % base_url
        # the output file extension will be added automatically by the eDocGen system
        outputFileName = "%s" %uuid.uuid4()
 
        inputValues = {
        'documentId': (None, documentId),
        'format': (None, format),
        'outputFileName': (None, outputFileName),
        'dbVendor': (None, dbVendor),
        'dbUrl': (None, dbUrl),
        'dbPassword': (None, dbPassword),
        'dbQuery': (None, dbQuery),
        'dbLimit': (None, dbLimit),
        'keyToFileName': (None, keyToFileName),
        }
 
        headers = {
             'x-access-token': eDocGenapi.token
        }
        response = requests.post(url, files=inputValues, headers=headers)
        print("Generate document: %s" % response.json())
        # add extension again due to internal bug
        outName = "%s.%s" % (response.json()['outFile'], generated_format)
        #Wait for file to get generated and download
        url = "%s/api/v1/output/name/%s" %(base_url,  outName)
        print("Fetching files with name using url: %s" %url)
 
        response = requests.get(url, headers=headers)
        output = response.json()["output"][0] if "output" in response.json() and len(response.json()["output"]) > 0 else None
        generated_file_id = None
        while(not output):
            print("waiting for file to get generated...", outName)
            response = requests.get(url, headers=headers)
            output = response.json()["output"][0] if "output" in response.json() and len(response.json()["output"]) > 0 else None
 
        generated_file_id = output["_id"]
        print("generated file id: %s" % generated_file_id)
 
        # Download the generated file
        url = '%s/api/v1/output/download/%s' %(base_url, generated_file_id)
        download_to_file = "./%s.%s" % (generated_file_id,generated_format )
        response = requests.get(url, headers=headers, allow_redirects=True)
        print("file saved via name : %s" % download_to_file)
        open(download_to_file, 'wb').write(response.content)

```

The process of using this API to generate a document using data from a MySQL database is similar to the method “downloadBulkJson”, except that the data is loaded from a database instead of a JSON file.

Email the Generated Document

After generating a document with eDocGen, you may want to email it to a recipient. This can easily be done using eDocGen's email functionality. To send the generated document by email, we just need to enter the recipient's email address and the output ID that we need to send the email.


```


    @classmethod
    def sendOutputToEmail(self , outputId,emailId):
        base_url = "https://app.eDocGen.com"
        #Generate request
        url = "%s/api/v1/output/email" % base_url
        headers = {
             'x-access-token': eDocGenapi.token
        }
 
 
        inputValues = {
        'outId': (None, outputId),
        'emailId': (None, emailId),
        }
 
        response = requests.post(url, files=inputValues, headers=headers)
        output = response.json()
        print (output)

```


Using the above python code. The output will be directly sent to the recipient's email address below.





Using this feature we can send documents to multiple recipients without having to manually download and attach the file. Further, we can use the system's email API to send the generated document to multiple recipients at once, making it an efficient and convenient way to share your documents with others.


Conclusion

In summary, it is a powerful tool for generating automated documents from templates that provide support in various languages including python. It simplifies the process of creating complex documents using placeholders and data and offers a variety of formatting and layout options. Overall, EDocGen is a compelling solution for automating document generation tasks, making it a valuable resource for businesses and organizations of all sizes.





Popular Posts

.