Adobe Tech Blog

News, updates, and thoughts related to Adobe, developers, and technology.

Follow publication

Totally Tubular Tabular Tools for Document Generation

--

Earlier this month the Adobe Acrobat Services team released a set of improvements to our Document Generation API. These updates are all related to table support and in this post, we’ll share some examples of what’s now possible.

Tables via HTML

As you (hopefully) know, when passing data to the Document Generation API, you can use a subset of supported HTML tags within your data. So for example:

When the bio tag is replaced in your Word document, the value will include text has the bold style applied. Developers can make use of span, br, b, strong, em, i, u, and p, as well as basic inline style declarations:

You can read more details about this support in our docs, but as of the most recent release, you can now use the table tag as well. Let's consider an example. First, an HTML table with three rows:

You’ll notice the top row is using th tags for a header. And here's our sample Word document:

Word doc with {{ cats }} template tag

When sent to the API, we get this result in our PDF:

Rendered table output in the PDF

You’ll notice no borders around the cells which matches default behavior on the web as well. Here we’ve added a border and spacing to the table via inline attributes:

And the result:

Table with styling borders.

One important caveat is that your tabular data should be in a paragraph by itself, as shown in the earlier Word screen shot. The example below will not work:

An example where the token tag for the table is inline, and will not work.

Dynamically hiding table rows

Document Generation previously supported dynamically hiding table columns, but this release now adds support to rows as well.

Unlike column support, this feature adds a conditional check to a particular cell. You can choose to add conditions to multiple cells, and if any of the conditions evaluates to true, the entire row is hidden. Let’s imagine a set of data consisting of, oh, I don’t know, how about cats?

Let’s begin by adding this to our Word document as is. Here we’ve done so with no additional formatting or logic.

Word table to display cat data

And here’s the resulting PDF:

Resulting PDF generated with the real cat data

Now let’s apply a bit of logic to our rows. First, we want to hide any row for a male cat. To do this we’re going to modify the gender cell like so:

The basic syntax is to provide an expression first (the expr portion), and then the context second (the array, cats). Here it is modified in the Word document:

Word document with gender table modified to hide non-female cats

And the result:

Resulting PDF table showing female cats only

Next, we’ll make our logic a bit more complex. Along with hiding the males we also want to hide any cats with an age less than 10. We can do that by adding the logic to the age column like so:

Together now, both conditions will be run and if any evaluate to true, the entire row is hidden. Here’s how the PDF looks now:

Table with female cats aged 10 and higher

Conditions in table cells

For the final update, we’ll cover new support for conditions within table cells. While conditions have worked outside of tables since the initial release of the Document Generation API, conditions within table cells were not possible via template tags. (As with most things in Document Generation, when something isn’t possible via tags in the template, it almost always can be handled by preprocessing your data before sending it to the API.)

As with the previous feature, the syntax is slightly different from what you would use outside of tables. Specifically with the addition of eval at the end of the tag. Here's an example of how this looks:

In the sample above, the list or array of data, arrayData, needs to be included in the expression at the end. It's a bit wordy, but this helps the API understand that we're checking a particular array item value.

Given our list of cats above, here’s an example where different output is used based on age. When the age value is less than ten, the age is output as is. When it’s ten or higher, it’s bold and red.

Table with conditional logic within the age column

And here’s the resulting PDF:

Resulting PDF with cats 10 and older having their age bolded and red.

More information

We hope these updates let you build better and more powerful dynamic templates with the API, and as always, be sure to read the docs for more information. As always, find us on our support forums for help.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Adobe Tech Blog

News, updates, and thoughts related to Adobe, developers, and technology.

Written by Raymond Camden

Raymond Camden is a Senior Developer Evangelist for Adobe. He works on the Document Services tools, JavaScript, and the Jamstack.

Responses (1)

Write a response