Feature

PDF Generator API

Marketplace Product

PDF remains a ubiquitous format for offline data. Both mobile and web applications often need to create PDF documents for various user workflows. The PDF Generator Plugin integrates with the pdfgeneratorapi.com service.

This integration enables generation of PDF documents based on the data of your application. Using the API provided by the plugin, your application can generate a PDF from the frontend app or in the backend logic. The frontend app can be created with UI Builder, native code, or with any other no-code UI solution.

PDF Generator API Automated PDF Generation Plugin

1. Login to Backendless Console and select your app. Open the Marketplace screen, select the API Services section, and install the “PDF Generator API” plugin from the Backendless Marketplace.

2. When you install the plugin, you will be asked to configure the service. Configuration of the PDF service takes a few minutes and it is a straightforward process.

  • In a separate browser window (or tab), register on the PDF Generator API website.
  • Log into your account and from the left menu, click the Account Settings.
    PDF Generator API Account Settings
  • In the API Credentials section, copy the API Key to your clipboard (you will need to repeat the same process for Secret as well).
    PDF Generator API Credentials and API Key
  • In the Service Configuration pop-up window paste the PDF Generator API key.
  • Repeat the process for the Secret value.
  • In the remaining fields in the Service Configuration window. enter:
    • A directory name from your Backendless application file storage. This is the directory where the generated PDF documents will be stored.
    • An email address you used to register on the PDF Generator API website

Click the Save button.

PDF Generator API Service Configuration in Backendless

3. To verify the installation, click the Cloud Code icon in Backendless Console and check if the “PDF Generator API” plugin appears in the list of services.

PDF Generator API Service Deployed in Backendless

4. If you plan to use the PDF generation service from a native application, select a programming language of your choice to download the client SDK generated specifically for the PDF Generator API service.

Download PDF Generator API Service SDK

In case a language you would like to use does not show up in the list, you can still use the plugin using its REST API.

PDF Templates

To generate a PDF file, you need to have a preconfigured template. A template is a combination of text fields, images, logos, QR codes, charts and other elements put in a specific order or location. In the API section, you will discover how to send information to these elements to build a PDF document.

Template Editor

To open the Editor, click the Templates button in the left menu.

PDF Generator API Templates

To open a specific template in the Editor, select the desired template from the Name column. 

Every template has а corresponding unique identifier which is stored within the ID column. IDs are used in the API invocation to select a specific template.

PDF Generator API Template IDs

To open a template for editing, click its name. The editor lets you add components to your template. When template editing is finished, make sure to save it.

It is recommended to read the documentation to learn about PDF components, so that you can build templates for any purpose.

Basic Data Fields

Many components of the PDF Generator API expect data which is sent as a JSON object when the API is invoked. For every component that accepts data, there is a corresponding data field that has a unique identifier. These unique identifiers are used by the service to map data correctly in the template when a JSON object is received. 

Below is an invoice template example. As you can see, there are multiple unique field names that accept data from an API invocation. You can find data fields within the curly braces. A data field must use the following pattern:

{fieldName}

PDF Generator API Template Example

You can set the name of any data field to meet your needs. To generate a PDF document, you must send a JSON object containing the name of the data field and the corresponding data that will be rendered in the template.

Fields in square brackets, such as $[EXPRESSION], represent a formula that does an automatic calculation based on parameters used.  You can link a few data fields that expect integer values to calculate a specific result. For more information about expressions, see the PDF Generator documentation.

Sending Data to Basic Data Fields

For demonstration purposes, a very simple template is used that contains only 4 basic data fields that expect data:

  • {invoice_number}
  • {first_name}
  • {last_name}
  • {address}

Consider the template below:

PDF Generator API Template Fields

To generate a PDF document and populate the template fields with data, a JSON object with template field names and the corresponding values must be provided as shown below:

[
   {
      "first_name": "John",
      "last_name": "Smith",
      "address": "13th Street. 47 W 13th St, New York, NY 10011",
      "invoice_number": "INV38379"
   }
]

The JSON object is used in an API call. The API service generates the document and returns its URL to the requesting application. Once the server receives the JSON object, all fields within the template get rendered and the PDF document is generated:

PDF Generator API Template PDF Generated

Table Fields

The PDF Generator service supports tabular data rendering (i.e. the table component). The table component provides great benefits when you need to populate multiple rows without adding them manually to the template. Hence, you can just send one JSON object with data, but the service will populate table rows automatically with the received data. 

Below is the table component of the PDF Generator service:

PDF Generator API Template Table Component

Sending Data to Table Fields

It may seem that such table fields as {description}, {quantity} and {price} work the same way as basic data fields, but it is not the case. To send data to these fields you must first import a JSON object in the Editor and establish data binding. To better understand how tables work in the PDF templates, let’s review the structure of a JSON object that is used to send data to the table fields.

As you can see in the image above, the table contains three table fields. Since these fields are used in a single row, they must be grouped into one JSON object.  The grouping is done with a JSON array that contains one or more objects with the same structure. 

The JSON sample below has the “line_items” array that contains an object with three table fields. The name of this array can be whatever you want, it is not critical.

[
   {
     "line_items": [
         {
             "description": "Magic T-Shirt, L",
             "quantity": 1,
             "price": "50"
         }
      ]
   }
]

Notice the “line_items” property contains an array of a single object that contains “description”, “quantity” and “price”. These properties will be used for two purposes:

  1. Create a mapping in the PDF Generator API Editor between JSON and template fields.
  2. Contain values for the actual data when the same JSON is used in the API.

To establish the data binding between the table fields and a JSON object, navigate to the Editor, click the Insert menu and then click the Data button:

Insert data in PDF Generator API template

The pop-up window provides several ways to import your JSON file. The screenshot below shows the Paste from Clipboard approach:

Add JSON data structure to test PDF Generator API template

Once the JSON structure is uploaded, table-based template fields can be mapped to the elements from the JSON structure. The screenshot below shows the process for the {description} table field – select the template field and click the INSERT DATA FIELD button:

Insert data fields in PDF Generator API template

When you click the button, the Data popup window appears where you can select a property from the imported JSON to be mapped to the template field.

PDF Generator API data popup

After you map all the table template fields, the PDF template can be used to create tabular content with an API call. Consider the following  JSON object. As you can see, it contains two items which will be used to generate a table (the items are JSON objects in the "line_items" array):

[
   {
     "line_items": [
         {
             "description" "Magic T-Shirt, L",
             "quantity": 1,
             "price": "50"
         },
         {
             "description": "Military Boots - Brown - US 11",
             "quantity": 1,
             "price": "100"
         }
      ]
   }
]

Once a PDF is generated, there will be a table containing two rows with the data from the provided JSON object.

Test data table in PDF Generator API template

Sourcing Data From the Backendless Database

Instead of sending actual data values in the JSON document, the Backendless plugin supports an alternative way of obtaining data. You can configure a query in the JSON object which will fetch data from your Backendless Database and send the data to the PDF Generator API service.

For demonstration purposes, consider the following data table schema:

Example table schema in Backendless Database

Suppose the name of the data table is “invoices”. To source data from the data table, the following JSON object can be used:

{
   "data": {
      "line_items": {
         "tableName": "invoices",
         "where" : "objectId in ('0F2BBB9F-C91D-431E-895D-1A0F7119C624', '294E49DC-1DC6-4146-97C5-FE1D979EBC1C')",
         "properties" : ["objectId", "ownerId"],
         "excludeProps" : ["created"]
      }
   }
}

The tableName is a mandatory parameter used to reference the data table in Backendless Database.

The where parameter sets a query to select objects/records from the table that match the specified condition. When using this approach, the names of the columns in the referenced table must match the names of the template fields configured in the PDF Generator API Editor.

The excludeProps parameter shown above instructs the server not to return the specified properties.

The properties parameter tells the server that only the specified properties should be returned.

In addition to the where property,  you can also use other properties to configure your database query.  Below is a list of all supported query properties:

   * @property {String} tableName
   * @property {boolean} findFirst // if true, returns {first object from db}; false, returns an array of selected db records
   * @property {String} [where]
   * @property {string[]} [properties]
   * @property {string[]} [excludeProps]
   * @property {string[]} [sortBy]
   * @property {boolean} [distinct]
   * @property {number} [pageSize]
   * @property {number} [offset]
   * @property {string} [having]
   * @property {string[]} [groupBy]
   * @property {string[]} [relations]
   * @property {number} [relationsDepth]

PDF Generator API

generateDocument

Generates a PDF document based on the specified template. The method uses a JSON object with the data for the template’s data fields. 

The PDF Generator API Editor makes it easier to obtain a JSON file that should be used in the API invocation. To get the JSON file for a template, login to your PDF Generator API account, click the Templates button in the left menu., then navigate to File and click Export. You can copy the contents of the JSON file and use it in the API call.

Parameters:

templateId – An integer value. An ID of a template used to generate a .PDF document. 

outputName – A string value. Identifies the name assigned to the generated PDF document.

workspaceId – A string value. This optional parameter identifies the name of the workspace to get the requested template. If the value is not specified, the API uses the master workspace identifier. This identifier is set in the PDF Generator Email API field of the API service configuration. To create a new workspace, navigate to the Admin Panel, click the Add Workspace, add the new workspace ID and click the Add button.

Add workspace in PDF Generator API

But in case you need to interact with a template located other than in the master workspace, you have to specify the desired workspace ID in the invocation.

data – A JSON object passed as a string. Contains template’s data fields and the corresponding values which will be used in the generated PDF document.

REST Example:

In the example below a PDF document is created with the name invoice_client_1610. For demonstration purposes, only a few data fields are passed to the request body. 

Returns a URL to the PDF document.

curl -X "POST" "https://xxxxx.backendless.app/api/services/PDFGeneratorAPI/473411/data-output/invoice_client_1610" \
   -H 'Content-Type: application/json' \
   -H 'Accept: application/json' \
   -d $'{
      "data": {
         "first_name": "John",
         "last_name": "Smith",
         "address": "13th Street. 47 W 13th St, New York, NY 10011",
         "invoice_number": "INV38379",
         "line_items": [
            {
               "description": "Magic T-Shirt, L",
               "quantity": 1,
               "price": "50"
            },
            {
               "description": "Military Boots - Brown - US 11",
               "quantity": 1,
               "price": "100"
            }
         ]
      },
   }'

Codeless Example:

PDF Generator API Codeless Example