A *service case study: Generating a PDF in your program with APITemplate.io

Users have reached out to us asking if it is possible to dynamically generate a customized PDF that they can share with the participants of their survey. While GuidedTrack does not have that functionality, you can easily integrate with external services via REST API to do so.

There are a number of solutions available, such as APITemplate.io, PDF Generator API, and PDF Monkey. All three of them are paid services, although APITemplate.io and PDF Monkey both offer a free plan for that allows you to generate a small number of PDFs a month for free.

On this case study we will show you how to configure and write a program that generates a PDF using APITemplate.io.

Let’s say you have written the following program for a quick happiness assessment:

-- Questionnaire
*question: Enter your name
	*save: name
	
*question: How happy are you today?
	*type: slider
	*answers: [1,2,3,4,5]
	*before: Not happy
	*after: Super happy
	*save: happinessScore
	
*if: happinessScore >= 3
	>>happinessEval = "you are quite happy"
	>>motivationalPhrase = "Keep it up!"
	
*if: happinessScore < 3
	>>happinessEval = "you are not very happy"
	>>motivationalPhrase = "Cheer up!"

As you can see, four variables are generated: name, happinessScore, happinessEval and motivationalPhrase. We want to use the value of these variables to fill in a pdf that the users of our assessment can download. Here is how you would do it!

Generating the PDF template

  • Create an account and log into APITemplate.io
  • Go to Manage templates and select New PDF Template to go to the template editor
  • Choose to use the Visual Editor to create your template if the design of your pdf is simple or you don’t know how to write html code. For a more complex layout, create an HTML template
  • Give your template a name, choose the sample that resembles most what you need and click on create
  • At this point, you will automatically return to your templates dashboard and you will find there the template you just created:

Click on Launch WYSIWYG Editor to edit the template. Use it as a word processor, and surround with double curly brackets the name of any variable that will be received from your program. For example, our template below has four variables: name, happinessScore, happinessEval and motivationalPhrase:

Go to the JSON tab to generate a JSON object with some test values so that you can see the template in action. This is the JSON object we have created for our example:

{
    "name": "Willie",
  	"happinessScore": "3",
  	"happinessEval" : "you are quite happy",
   	"motivationalPhrase" : "Keep it up"
 }

And this is the PDF it generates:

Now that you have a template ready, you can do the integration with GuidedTrack! Before you leave the APITemplate.io site, there are two things that you will need. One is your template’s id, which can be found in the tab Manage templates next to the template’s name. Then go to the API integration tab to locate your API key.

Configuring the service

Go to the settings>services tab in your program and create a new service with the following data:

Name:

URL:

Click on + Add header and fill it up like this (using your own API key):

Key:

Value:

X-API-KEY

801aNjA5NjozMTE0Sk9YaG9JH3BYanIwVkVzTGa

Writing your program

Here is the code to add to the program, after the variables have been generated, to connect with APITemplate.io:

>>template_id = "d5e77b2b2a3b8d2c"

-- Create pdf
*service: pdfGenerator
	*path: /create-pdf?template_id={template_id}
	*method: POST
	*send: {"name" -> name,"happinessScore" -> happinessScore, "happinessEval" -> happinessEval, "motivationalPhrase" -> motivationalPhrase}
	*success
		>> pdfUrl = it["download_url"]
		>> transactionRef = it["transaction_ref"]
		>> status = it["status"]
	*error
		Sorry, we encountered an error while generating your pdf: {it}
		*set: pdfGenError

-- Display pdf URL
*if: not pdfGenError
	You can download your pdf here: {pdfUrl}

First, we have created a variable named template_id that we initialize with id of the template we created in APITemplate.io.

Then we call the pdfGenerator service, sending an association with the data to fill in the template.

If all goes well, the object it will return the URL that the pdf can be downloaded from. Otherwise, it will contain an error message.