Hands on with Adobe Document Generation API
Tags: pdf, api, adobe, document
Published: 2021-03-29 13:14:10

This week Adobe is proud to [announce](https://blog.adobe.com/en/publish/2021/03/19/adobes-pdf-apis-expand-functionality-and-flexibility-for-developers.html#gs.wai9mu) the availability of the [Document Generation API](https://www.adobe.io/apis/documentcloud/dcsdk/doc-generation.html) as part of Adobe Document Services. This is a powerful new service that enables developers to generate both Word and PDF documents from template and dynamic data. This new API is fully supported by our [Java](https://mvnrepository.com/artifact/com.adobe.documentservices/pdftools-sdk), [.Net](https://www.nuget.org/packages/Adobe.DocumentServices.PDFTools/) and [Node](https://www.npmjs.com/package/@adobe/documentservices-pdftools-node-sdk) SDKs already and can be used in any other language via a REST API. Let's take a look at how this works.

Document generation is done by combining a Word document that acts as a template along with your data. This data could be hard-coded in a JSON file or fetched on the fly from another API. Adobe Document Generation API will take your Word document template, inject your data, and then output either a Word document or PDF as the result. How about a simple demo?

Let's begin with a Word document. I fired up Microsoft Word and did a tiny bit of typing and styling to create a header and one short sentence of text:

![Word document](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqezuhfqljjvtvef9pl6.png)

To make this a template, we need to insert tags that will be replaced by our data during the generation process. These tags can be simple variable substitutions (insert a value from our data) or more complex operations including conditionals and loops. You can even embed expressions that act much like formulas from Excel. 

You've got two ways of adding these expressions to your document. One is to simply do it by hand. For developers, this may be the simplest solution. (For me, I was very much reminded of the [Liquid](https://shopify.github.io/liquid/) template language.) 

The other method is by installing the [Adobe Document Generation Tagger "add-in"](https://www.adobe.io/apis/documentcloud/dcsdk/docs.html?view=docgen-addin) to Microsoft Word. I've been using Word for probably a few decades but this was the first time I've seen something added that was more interesting than just a button. The add-in adds an interactive editor to your editor that makes it simpler to insert the proper tokens. 

Our first demo is going to be pretty simple so we'll do it the manual way (ala, uphill, both ways, in the snow). I'm going to edit my Word document to add a name token:

![The updated Word document with literals](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aay7j77e20qgdpew1d3j.png)

In the screen shot above, you can see brackets used for both tokens: `{{name}}`. Now we have a Word document that can be used with the generation service. Let's look at a simple Node script to demonstrate creating a new Word document based on the template. (The following code is based on our [quick start](https://www.adobe.io/apis/documentcloud/dcsdk/docs.html?view=docgen-addin), be sure to check it out for examples in other languagues.)

```js
const PDFToolsSdk = require('@adobe/documentservices-pdftools-node-sdk');
const fs = require('fs');

const inputFile = './hello1.docx';
const outputFile = './helloWorld.docx';

const data = {
    name: "Raymond Camden"
};

//remove output if exists
if(fs.existsSync(outputFile)) fs.unlinkSync(outputFile);

// Initial setup, create credentials instance.
const credentials =  PDFToolsSdk.Credentials
      .serviceAccountCredentialsBuilder()
      .fromFile("pdftools-api-credentials.json")
      .build();


// Create an ExecutionContext using credentials.
const executionContext = PDFToolsSdk.ExecutionContext.create(credentials);

// Create a new DocumentMerge options instance.
const documentMerge = PDFToolsSdk.DocumentMerge,
      documentMergeOptions = documentMerge.options,
      options = new documentMergeOptions.DocumentMergeOptions(data, documentMergeOptions.OutputFormat.DOCX);

// Create a new operation instance using the options instance.
const documentMergeOperation = documentMerge.Operation.createNew(options);

// Set operation input document template from a source file.
const input = PDFToolsSdk.FileRef.createFromLocalFile(inputFile);
documentMergeOperation.setInput(input);

// Execute the operation and Save the result to the specified location.
documentMergeOperation.execute(executionContext)
.then(result => result.saveAsFile(outputFile))
.catch(err => {
    if(err instanceof PDFToolsSdk.Error.ServiceApiError
        || err instanceof PDFToolsSdk.Error.ServiceUsageError) {
        console.log('Exception encountered while executing operation', err);
    } else {
        console.log('Exception encountered while executing operation', err);
    }
});
```

The script begins by loading in our required libraries, including the Adobe PDF Tools SDK. I then define my input and output files. Next, I define by my data. It's relatively simple (and hard coded), but again this could be loaded from the file system or fetched via an API call. 

The rest of the template comes from our [Quick Start](https://www.adobe.io/apis/documentcloud/dcsdk/docs.html?view=docgen-addin), but boils down to setting up the [credentials](https://www.adobe.io/apis/documentcloud/dcsdk/gettingstarted.html), defining the type of generation we want (Word), and then performing the operation. 

Once run, we end up with this new Word doc:

![Final Word doc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pgh9hoba924oz3yzz99.png)

How about PDF support? We can simply change the name of the output file:

```js
const outputFile = './helloWorld.pdf';
```

And change the `options` variable:

```js
options = new documentMergeOptions.DocumentMergeOptions(data, documentMergeOptions.OutputFormat.PDF);
```

Once run we get:

![PDF output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cslzamotf1s02ark61p7.png)

As a quick note, the scripts above, and the source Word docs, may all be found on my GitHub repo here: <https://github.com/cfjedimaster/document-services-demos/tree/main/docgen>

Alright, so lets kick it up a notch with some data is a bit more complex. Imagine a veterinary clinic that needs to create documents for clients. These documents will include information about the client as well as information about their animals. Since the only proper pet is a cat, we will just assume they have cats. Here's how this data my look:

```
{
	"name":"Raymond Camden",
	"email":"raycamde@adobe.com",
	"address":"1313 Mockingbird Lane, Lafayette, LA",
	"cell":"337-555-5555",
	"cats":[
        {"name":"Luna", "gender": "female", "breed": "something", "weight": 4},
        {"name":"Pig", "gender": "female", "breed": "something else", "weight": 8},
        {"name":"Cracker", "gender": "male", "breed": "large", "weight": 10}
	]
}
```

We've got basic customer information on top (name, email, address, and a mobile number) and then an array of cats where each cat has a name, gender, breed, and weight. We want to create a document that has the following features:

* On top, we show the customers name, and then as much information about them as can. So for example, not every customer record may have the address. We can use conditional tags for this.
* Display a table of cats, showing their name, gender, and breeds.
* Optionally display a list of cats that are overweight.

This is somewhat more complex than our initial demo, so this would be a good time to use the Word Add-In to help with the template. I began by creating the template with static values:

![Initial Word template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99hzbm1k0zb1iw3wkwv2.png)

If you've installed the add-in, you should see this button in your top ribbon:

![Document Generation button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5js1qoihd40mr5vpo5t.png)

Clicking this will open a new panel to the right of your document:

![Document Generation sidebar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01t01m87jlom6lliln9x.png)

If you click Get Started, you will then be prompted to paste in your JSON or upload a JSON file:

![JSON entry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axofpudruy5ayi1nx6ra.png)

Notice that it's asking you for *sample* data. In our imaginary application, you could imagine our customer and pet data being stored in a database. To get started building your template, you could need to generate at least one JSON file, or output, of data to provide to the Word Add-In so it can help you design the template. I went ahead and used the JSON data earlier in the example. 

![Sample JSON data entered](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x2d2qqbx9yjy1a3nxmc.png)

If you hit Generate Tags, the Add-In will then be able to help you insert tokens. You'll notice two tabs, Basic and Advanced:

![The template data is now ready to be used](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eclynlayxli92lntle8d.png)

Let's start by adding in the simple data. In my Word document, I selected my default text, `Jane Smith`, and in the Add-In panel I selected `name` and then clicked the Insert Text button. Doing so replaced the default text with the tag.

![The name token inserted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkw2t4ed4befmlpaqki7.png)

Easy, right? I went ahead and replaced email, address, and mobile phone. 

![More tokens added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uumgnk516eig0pbji9x.png)

So what about the home number? As we said above, we expect that some customers may not have a home number (our test data did not). Some may not have a cell (or may not want to share it with their friendly neighborhood vet). Luckily we can add conditionals to our templates.

I began by selecting the entire line with "Mobile phone", and then switched to the Advanced tab. In there is a "Conditional content" section you can expand. 

![Conditional logic tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bby8vx561rcz5bwrfsrb.png)

In the "Select records" you can either type or use the drop down. I selected "cell" and then hit the "if present" button. Next I clicked Insert Condition.

![Conditional Logic added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d27m04ukc6vpy4hiof2y.png)

The `{% conditional-section cell %}` tag wraps our entire line and will only display in the final result if `cell` exists. We don't have a value for our home phone number but we can copy and paste the same tags by hand. (If I were building this with a non technical user, I would have supplied an empty value so the Add-In would recognize it as a possibility.) We will assume that if the customer has a home number, it will be provided in a value named `homenumber`.

Here's the final version of the top part of our template:

![Customer information with tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udp37ih8yc7iphh7oxx2.png)

Now let's add our list of cats. Still in the Advanced section of the Add-In, go into Tables and Lists. Table is selected by default. First you need to select the array data to iterate over. The Add-In was smart enough to look at your data and realize that `cats` was the only array therefore it's the only option in the dropdown under "Table records". Next, you need to specify which columns to use. If you begin typing in "Select column records", once again the Add-In is smart enough to suggest only the keys it found in the array. I entered name, breed, and gender.

![Table tag editing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltnuczt7ix7jw6uvce4r.png)

This time we're not going to select the existing table. Instead I ensured my cursor was right after the table and hit "Insert Table".

![New table with tags added](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nh2rswwj5o20ietazmff.png)

As you can see, it created a new table with headers. Now we just need to edit the new table to re-apply the formatting used before (a simple bold and center). I also kept out the "Cat" before each column name as that was redundant.

![Updated, formatted table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o04vy3u0ltgbq7398wyc.png)

This is a good illustration of how the Add-In can simply adding in your tags, but you still may have to do a bit of clean up, styling, and so forth, after you're done. 

Speaking of - the final feature was a list of cats that may be overweight. For that we'll switch back to editing by hand. I modified the list at the bottom with a bit of logic:

![Loop with logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po3e7a2h4ejlju39lyer.png)

How did I figure this out? Our documentation generation logic uses [JSONata](https://docs.jsonata.org/overview.html) for token parsing and I spent some time on their site looking at their docs. It's a fascinating system and may take a bit of trial and error, but you can get the hang of it pretty quickly.

With my template done, I created a new script to use it and load in my data. As it's a slight modification from my earlier script, here's all that I changed:

```js
const inputFile = './catTemplate.docx';
const outputFile = './catCustomer.pdf';

const data = require('./catowner.json');
```

Again, that `data` value would typically be loaded via an API or database call. You can find the complete file on my [GitHub repo](https://github.com/cfjedimaster/document-services-demos/blob/main/docgen/catpdf.js). And here's the beautiful dynamic result:

![PDF Cat Owner result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vn41cdt83tbdygh6vdhe.png)

### Next Steps

I hope this introduction has gotten you excited about our new Document Generation API. Remember that you can sign up for this and our [Adobe PDF Tools API](https://www.adobe.io/apis/documentcloud/dcsdk/pdf-tools.html) for a free six month trial. Also be sure to check our [docs](https://www.adobe.io/apis/documentcloud/dcsdk/docs.html) and head to the [forums](https://community.adobe.com/t5/document-services-apis/bd-p/Document-Cloud-SDK?page=1&sort=latest_replies&filter=all) with any questions you may have.