Tableau: Mass generation of PDF reports

Tableau is a tool that helps us present information in a highly visual way without requiring much development time.
For example, El Comercio uses Tableau Online to present information in some of its posts:

ElComercio

With Tableau we can also connect to different types of databases and with Tableau Server share reports internally.
Now, sometimes it is not enough to have the information online and it is necessary to export the report in PDF or image format.
This is an easy task if it is only one report or if we do not use filters in the visualization, but what if we have 100, 1000 or 5000 reports that we must export, doing it manually is definitely not an option.
For this, in Tableau Server there is a tool called tabcmd, which allows you to connect to the Tableau server and be able to export the reports that are required. This tool is not installed by default, but the steps are simple and can be followed from the Tableau page.
The utilities of tabcmd are many, as we can see on the uses page or in the list of commands. One that particularly interests me is exporting the reports in PDF, which we can execute from the Windows console:
tabcmd export Book/View –pdf -f “C:\Directory\FileName
And if we combine it with the url filters (in Tableau Server you can pass parameters to a report through the url and thus apply the filters):
We can create something beautiful and very useful 😀
Returning to the initial problem, we need to export the reports in pdf format automatically; For example, the demographic data of the departments of Peru:

MapaPerú

In this case, we want to export a PDF report for each department (in this case, the department is used as a filter):
tabcmd login -s “http://localhost:8000/” -u admin -p password
tabcmd export MapaInteractivo/Peru?Departamento=Lima –pdf -f D:\tableau\Reporte-Lima
This will give us the Lima report in PDF format, but to automate it, we can create a .bat file and use variables 😀
Step by step:
1. First, we must log in to access our server and the report:
tabcmd login -s “http://localhost:8000/” -u admin -p password
2. Read the information from a CSV file (database.csv), run a loop to read each line of the file, and export the reports:
for /f “tokens=1-3 delims=,” %%a in (D:\tableau\database.csv) do (
tabcmd export MapaInteractivo/Peru?Departamento=%%a –pdf -f D:\tableau\%%c
)
Where, the database has the following structure: Department (filter), Directory Path, Path + File Name:
Lima, \Lima\, \Lima\Reporte-Lima
Lambayeque, \Lambayeque\, \Lambayeque\Reporte-Lambayeque
Tumbes, \Tumbes\, \Tumbes\Reporte-Tumbes
Each column is assigned a variable in alphabetical order, that is:
a = Department
b = Directory path where the file will be saved
c = Path + File Name
These values ​​will change, and a file will be exported for each row in the file.
We can also add additional lines of code to create the destination directory if it doesn’t exist and to check if a PDF file with the same name already exists, so it doesn’t generate it again. The final code would look like this:
@echo off
tabcmd login -s “http://localhost:8000/” -u admin -p password
for /f “tokens=1-3 delims=,” %%a in (D:\tableau\database.csv) do (
if not exist D:\tableau\storage\%%b (
mkdir D:\tableau\storage\%%b
)
if not exist D:\tableau\storage\%%c (
tabcmd export MapaInteractivo/Peru?Departamento=%%a –pdf -f D:\tableau\%%c
)
) pause
For this example, there were only 24 reports, one for each department of Peru, but it could also be used at the district level, or in databases of much larger datasets.
The source code can be found on Data Poetry’s GitHub account.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get updates

Spam-free subscription, we guarantee. This is just a friendly ping when new content is out.