Background:
This article provides a comprehensive guide on using API calls to render a report as a PDF. Whether you are a software developer or an IT administrator, this guide will provide you with the knowledge and tools necessary to automate this critical business intelligence process.
Note: For all APIs that must authenticate the user’s login, you must add a Param as the connect token. This will require “?token={token}“ to be appended to the end of the request URL, where {token} is replaced with the value of your connect token. For more information on how to generate a token, click here: How To Use Connect Tokens | Wyn-Enterprise.
Steps to Complete:
Get All Reports
POST {serverURL}/api/graphql?token={token}
Body (raw):
query {
documenttypes(key: "rdl") {
documents {
id, displayName
}
}
}
For this request, we will utilize a GraphQL call to return all the available reports to the user with the reports respective IDs.
Here is an expected output. For this article, we will be using the “Monthly Sales Report” as an example:
{
"data": {
"documenttypes": [
{
"documents": [
{
"id": "b10f914d-9fa4-4ac6-94f1-7f2991b1f821",
"displayName": "Monthly Sales Report"
},
{
"id": "f50cc170-6b0f-4cd1-bb04-d04d840c3891",
"displayName": "Sales by Genre"
},
{
"id": "2daad73b-e2a4-461c-b13e-35bca747fd6e",
"displayName": "Top 20 Highest Payouts"
},
{
"id": "d31cbcb5-51f2-4413-bca3-c6e97b2c3b2d",
"displayName": "Blood Test Results"
}
]
}
]
}
}
Show Report’s Available Values
POST /api/reporting/reports/{reportid}/values?token={token}
Headers:
-
Content-Type: application/json
Body (raw):
{}
This REST API will return all of the available values for the parameters in the report specified in the request URL.
Here is the expected output for the “Monthly Sales Report.”
{
"ReportParameter": {
"validValues": [
{
"label": "2014",
"value": "2014-01-01T00:00:00"
},
{
"label": "2015",
"value": "2015-01-01T00:00:00"
},
{
"label": "2016",
"value": "2016-01-01T00:00:00"
},
{
"label": "2017",
"value": "2017-01-01T00:00:00"
},
{
"label": "2018",
"value": "2018-01-01T00:00:00"
}
],
"values": []
}
}
Get Export Template ID
GET /api/v2/reporting/export-templates?token={token}
This REST API will return the IDs of all the potential export templates you have, from PDF, CSV, or custom templates.
Here is an expected output for a single result. Note that this API will return all of the available export templates, including custom ones you may have created.
[
{
"id": "b2f52743-2513-41e3-9cf9-6edf481a1a5d",
"description": "",
"exportType": "pdf",
"name": "PDF",
"roles": [
"Everyone"
],
"targetPlaces": "All",
"exportSettings": {},
"allowedSettings": {
"Version": true,
"EmbedFonts": true,
"NeverEmbedFonts": true,
"PrintOnOpen": true,
"Title": true,
"Author": true,
"Subject": true,
"Keywords": true,
"Application": true,
"Permissions": true,
"Use128Bit": true,
"HideToolbar": true,
"HideMenubar": true,
"HideWindowUI": true,
"FitWindow": true,
"CenterWindow": true,
"DisplayTitle": true,
"DisplayMode": true,
"DuplexMode": true,
"NumberOfCopies": true,
"ImageInterpolation": true,
"PaperSourceByPageSize": true,
"PrintPageRange": true,
"IsPaginated": true,
"PrintLayoutMode": true,
"SizeToFit": true,
"StartPage": true,
"EndPage": true,
"WatermarkAngle": true,
"WatermarkColor": true,
"WatermarkTitle": true,
"WatermarkFontFamily": true,
"WatermarkFontSize": true,
"WatermarkFontBold": true,
"WatermarkFontItalic": true,
"WatermarkFontStrikeout": true,
"WatermarkFontUnderline": true,
"Encrypt": true,
"OwnerPassword": true,
"UserPassword": true,
"FilenameTemplate": true,
"AddTimestamp": true
},
"index": 0
}
]
Export Report
POST /api/v2/reporting/reports/{reportid}/export/{exportSettingsTemplateID}?token={{token}}
Headers:
-
Content-Type: application/json-patch+json
Body (raw):
{
"parameters": {
"ReportParameter": [
"2014-01-01"
]
}
}
If you are executing the export report API against a report that has no parameters, the Body will be empty like this:
{}
Additionally, you may include various other settings in the body. The following example shows how to create a body with a parameter and various other settings in place:
{
"settings": {
"additionalProp1": "string"
},
"overrideExportTemplateSettings": false,
"skipEmptyReport": false,
"parameters": {
"ReportParameter": [
"2014-01-01"
]
},
"cacheRefreshIsRequired": false,
"allowedClaims": "string",
"userFunctions": {
"CSharp": "string",
"VisualBasic": "string"
}
}
Here is an explanation for each of these settings:
Setting | Function |
OverrideExportTemplateSettings | Whether to override export template settings. The default value is false. |
SkipEmptyReport | Skip rendering of empty reports. The default value is false. |
Parameters | Define the parameters required by using the “parameter name”: [“parameter value”] syntax. |
CacheRefreshIsRequired |
Refresh cached report. The default value is false. |
Settings | The export settings are available for RenderingExtensions. If export template has settings with value "User Allowed Settings" is true, this setting must be set in the payload. More info about RenderingExtensions |
When you successfully execute the request, the expected output will be as follows:
{
"resultUrl": "api/v2.0/reporting/render-results/ac2cbfc4-e019-4012-b54a-73b9b889ee2e",
"resultId": "ac2cbfc4-e019-4012-b54a-73b9b889ee2e",
"verificationUrl": "api/v2.0/reporting/render-results/ac2cbfc4-e019-4012-b54a-73b9b889ee2e/verify",
"renderingSkipped": false
}
Creating the Download URL
In order to work with this output, you will need to append it to the end of your {{serverURL}}. For example, using the above output, I will enter this URL to retrieve the exported PDF on the browser:
My serverURL is: http://localhost:51980/
My result URL for the exported PDF is: api/v2.0/reporting/render-results/ac2cbfc4-e019-4012-b54a-73b9b889ee2e
Therefore, the URL that a default configuration will go to for downloading the export file will be:
http://localhost:51980/api/v2.0/reporting/render-results/ac2cbfc4-e019-4012-b54a-73b9b889ee2e
Upon visiting this URL, the download will automatically begin.
This article should fully explain the process of exporting reports into a PDF using an API. Please note that this article also serves to help users export reports as any export template that is currently available to the user. Depending on the export template ID taken from step 3, users can export into CSV, Excel, or even a custom template you created for a specific need.
If there are any questions or complaints about this article, please do not hesitate to reach out to the Wyn Experts at experts@wynenterprise.com.