Creating a PDF from DLEX (dlex-layout Endpoint)
Use the `dlex-layout` endpoint to create PDF reports dynamically on the cloud.
This tutorial illustrates using a DLEX file and associated JSON data to generate a PDF using the DynamicPDF API's dlex-layout
endpoint.
Required Resources
To complete this tutorial, you must add the Create PDF (dlex-layout Endpoint) sample to your samples
folder in your cloud storage space using the File Manager. After adding the sample resources, you should see a samples/creating-pdf-dlex-layout-endpoint
folder containing the resources for this tutorial.
Sample | Sample Folder | Resources |
---|---|---|
Create PDF (dlex-layout endpoint) | samples/creating-pdf-dlex-layout-endpoint | create-pdf-dlex-layout.dlex , create-pdf-dlex-layout.png , create-pdf-dlex-layout.json |
- From the File Manager, download
create-pdf-dlex-layout.json
to your local system; here we assume/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout
.
Resource | Cloud/Local |
---|---|
create-pdf-dlex-layout.dlex | cloud |
create-pdf-dlex-layout.png | cloud |
create-pdf-dlex-layout.json | local |
See Sample Resources for instructions on adding sample resources.
If you are not familiar with the File Manager or Apps and API Keys, refer to the following tutorial and relevant Users Guide pages.
Obtaining API Key
This tutorial assumes a valid API key obtained from the DynamicPDF API's Portal
. Refer to the following for instructions on getting an API key.
If you are not familiar with the File Manager or Apps and API Keys, refer to the following tutorial and relevant Users Guide pages.
Test DLEX on Cloud
Typically, when using the dlex-layout
endpoint you would first create the DLEX on the cloud using test data.
Remember, layout data is your organization's data, keep this data safe by never storing it in the cloud - unless the data is sample data
Let's test in Designer that the report is correct.
- Navigate to the File Manager and double-click on the
create-pdf-dlex-layout.dlex
file to open the DLEX in Designer.
Note that if the DLEX file and the JSON file have the same name and are in the same folder, then Designer will attempt to load the data automatically.
- Run the report by clicking the Run Report (the green arrow) button.
Figure 1. Running a report in Designer displays the report in a new browser tab.
Make Request Using API
- Create a new
POST
request and add the two form fields listed in the following table. Note that theLayoutData
is a file on your local system whileDlexPath
refers to the DLEX file in the File Manager.
Parameter | Parameter Type | Value |
---|---|---|
Authorization | Header | Bearer: DP.xxx-api-key-xxx |
DlexPath | Form Field | samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex |
LayoutData | Form file field. | C:/temp/dynamicpdf-api-samples/creatingpdf-dlex-layout/create-pdf-dlex-layout.json |
- Create the following cURL command where the PDF is sent to the endpoint as binary data.
curl -X POST "https://api.dynamicpdf.com/v1.0/dlex-layout"
-H "Authorization: Bearer DP.xxx-api-key-xxx"
-F "DlexPath=samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex"
-F "LayoutData=@C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.json"
-o create-pdf-dlex-layout-output.pdf
- Execute the cURL command and the PDF should be written to your local system.
Note that if the create-pdf-dlex-layout.pdf
file cannot be opened, then open it in a text editor to view the JSON error. This is a cURL limitation, Postman will display the error.
Figure 1. The report created from the DLEX and JSON layout data.
Calling Endpoint Using Client Library
To simplify development, you can also use one of the DynamicPDF API client libraries. Use the client library of your choice to complete this tutorial section.
Complete Source
You can access the complete source for this project at one of the following GitHub projects.
Language | File Name | Location (package/namespace/etc.) | GitHub Project |
---|---|---|---|
Java | CreatingPdfDlexLayout.java | com.dynamicpdf.api.examples | https://github.com/dynamicpdf-api/java-client-examples |
C# | Program.cs | CreatingPdfDlexLayout | https://github.com/dynamicpdf-api/dotnet-client-examples |
Nodejs | CreatingPdfDlexLayout.js | nodejs-client-examples | https://github.com/dynamicpdf-api/nodejs-client-examples |
PHP | CreatingPdfDlexLayout.php | php-client-examples | https://github.com/dynamicpdf-api/php-client-examples |
GO | dlex-layout-example.go | go-client-examples | https://github.com/dynamicpdf-api/go-client-examples/tree/main |
Python | DlexLayoutExample.py | python-client-examples | https://github.com/dynamicpdf-api/python-client-examples |
Click on the language tab of choice to view the tutorial steps for the particular language.
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
Available on NuGet:
Install-Package DynamicPDF.API
- Create a new Console App (.NET Core) project named
CreatingPdfDlexLayout
. - Add the DynamicPDF.API NuGet package.
- Create a new static method named
Run
. - In
Run
create a newLayoutData
instance and in the constructor pass the path to the layout data on your local system. - Create a new
DlexLayout
instance and in the constructor pass the cloud path in the File Manager and theLayoutData
instance. - Add a call to the
Process
method the returns aPdfResponse
instance. - If the call to the endpoint is successful, then save the response's PDF content to a file.
- Run the application and the PDF is written to your local system.
using DynamicPDF.Api;
using System;
using System.IO;
namespace CreatingPdfDlexLayout
{
class Program
{
static void Main(string[] args)
{
Run("DP.xxx-api-key-xxx", "C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/");
}
public static void Run(String apiKey, String basePath)
{
{
//get the layout data from local system
LayoutDataResource layoutData = new LayoutDataResource(basePath + "create-pdf-dlex-layout.json");
// specify the dlex on cloud and load layoutdata
DlexLayout dlexEndpoint = new DlexLayout("samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex", layoutData);
dlexEndpoint.ApiKey = apiKey;
//call the endpoint and get response
PdfResponse response = dlexEndpoint.Process();
//if successful write response to filesystem
if (response.isSuccessful)
{
File.WriteAllBytes(basePath + "create-pdf-dlex-output.pdf", (byte[])response.Content);
}
else
{
Console.WriteLine(response.ErrorJson);
}
}
}
}
}
Available on NPM:
npm i @dynamicpdf/api
- Use npm to install the DynamicPDF API module.
- Create a new class named
CreatePdfDlexLayout
. - Create a static
Run
method. - In
Run
create a newLayoutData
instance and in the constructor pass the path to the layout data on your local system. - Create a new
DlexLayout
instance and in the constructor pass the cloud path in the File Manager and theLayoutData
instance. - Add a call to the
process
method the returns aPdfResponse
instance. - If the call to the endpoint is successful, then save the response's PDF content to a file.
- Add a call to the
Run
method below the class definition.
import fs from 'fs';
import {
LayoutDataResource,
DlexLayout
} from "@dynamicpdf/api"
export class CreatingPdfDlexLayout {
static async Run() {
//get layout data from local system
var layoutData = new LayoutDataResource("C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.json");
//load dlex from cloud and layout data
var dlexEndpoint = new DlexLayout("samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex", layoutData);
dlexEndpoint.apiKey = "DP.xxx-api-key-xxx";
//call the dlex-layout endpoint and get reslts
var res = await dlexEndpoint.process();
//if successful then save to file
if (res.isSuccessful) {
var outFile = "C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(response.errorJson);
}
}
}
await CreatingPdfDlexLayout.Run();
- Run the application
node CreatingPdfDlexLayout.js
and the JSON is output to the console.
Available on Maven:
https://search.maven.org/search?q=g:com.dynamicpdf.api
<dependency>
<groupId>com.dynamicpdf.api</groupId>
<artifactId>dynamicpdf-api</artifactId>
<version>1.0.0</version>
</dependency>
- Create a new Maven project and add the DynamicPDF API as a dependency, also add the Apache Commons IO library.
- Create a new class named
CreatingPdfDlexLayout
with amain
method.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
- Create a new class named
CreatingPdfDlexLayout
with amain
method. - Create a new static method named
Run
. - In
Run
create a newLayoutData
instance and in the constructor pass the path to the layout data on your local system. - Create a new
DlexLayout
instance and in the constructor pass the cloud path in the File Manager and theLayoutData
instance. - Add a call to the
process
method the returns aPdfResponse
instance. - If the call to the endpoint is successful, then save the response's PDF content to a file.
- Run the application and the PDF is written to your local system.
package com.dynamicpdf.api.examples;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.dynamicpdf.api.DlexLayout;
import com.dynamicpdf.api.LayoutDataResource;
import com.dynamicpdf.api.PdfResponse;
public class CreatingPdfDlexLayout {
public static void main(String[] args) {
CreatingPdfDlexLayout.Run("DP.xxx-api-key-xxx",
"C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/");
}
public static void Run(String apiKey, String basePath) {
//get the local layoutdata
LayoutDataResource layoutData = new LayoutDataResource(basePath + "create-pdf-dlex-layout.json");
//load layoutdata and cloud dlex
DlexLayout dlexEndpoint = new DlexLayout("samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex", layoutData);
dlexEndpoint.setApiKey(apiKey);
//call the dlex-layout endpoint and get response
PdfResponse response = dlexEndpoint.process();
//if successful save as file
if (response.getIsSuccessful()) {
try {
FileUtils.writeByteArrayToFile(new File(basePath + "/create-pdf-dlex-layout-output.pdf"),
(byte[]) response.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(response.getErrorJson());
}
}
}
Available as a Composer package:
composer require dynamicpdf/api
Use composer to ensure you have the required PHP libraries.
Create a new class named
CreatePdfDlexLayout
.Add a
Run
method.In
Run
create a newLayoutData
instance and in the constructor pass the path to the layout data on your local system.Create a new
DlexLayout
instance and in the constructor pass the cloud path in the File Manager and theLayoutData
instance.Add a call to the
Process
method the returns aPdfResponse
instance.If the call to the endpoint is successful, then save the response's PDF content to a file.
Run the application
php CreatingPdfDlexLayout.php
and the JSON is output to the console.
<?php
require __DIR__ . '/vendor/autoload.php';
use DynamicPDF\Api\LayoutDataResource;
use DynamicPDF\Api\DlexLayout;
class CreatingPdfDlexLayout
{
private static string $BasePath = "C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/";
public static function Run()
{
//get layoutdata from local system
$layoutData = new LayoutDataResource(CreatingPdfDlexLayout::$BasePath . "create-pdf-dlex-layout.json");
//load dlex from cloud and layoutdata
$dlexEndpoint = new DlexLayout("samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex", $layoutData);
$dlexEndpoint->ApiKey = "DP.xxx-api-key-xxx";
//call dlex-layout endpoint and get response
$response = $dlexEndpoint->Process();
//if successul write to file
if($response->IsSuccessful)
{
file_put_contents(CreatingPdfDlexLayout::$BasePath . "create-pdf-dlex-layout-output.pdf", $response->Content);
} else {
echo($response->ErrorMessage);
}
}
}
CreatingPdfDlexLayout::Run();
Available as a GO package: https://pkg.go.dev/github.com/dynamicpdf-api/go-client
Ensure you have the required GO libraries.
Create a new file named
dlex-layout-example.go
.Add a
main
method.In
main
create a newLayoutData
instance and in the constructor pass the path to the layout data on your local system.Create a new
DlexLayout
instance and in the constructor pass the cloud path in the File Manager and theLayoutData
instance.Add a call to the
Process
method the returns aPdfResponse
instance.If the call to the endpoint is successful, then save the response's PDF content to a file.
Run the application
go dlex-layout-example.go
and the JSON is output to the console.
package main
import (
"fmt"
"os"
"github.com/dynamicpdf-api/go-client/endpoint"
"github.com/dynamicpdf-api/go-client/resource"
)
func main() {
layoutDataResource := resource.NewLayoutDataResource("c:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout.json", "creating-pdf-dlex-layout.json")
layoutData := endpoint.NewDlexEndpoint("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutDataResource)
layoutData.Endpoint.BaseUrl = "https://api.dynamicpdf.com/"
layoutData.Endpoint.ApiKey = "DP.xxx-api-key-xxx"
resp := layoutData.Process()
res := <-resp
if res.IsSuccessful() == true {
os.WriteFile("c:/temp/dynamicpdf-api-samples/dlex-layout-output.pdf", res.Content().Bytes(), os.ModeType)
} else {
fmt.Println("errorId: " + res.ErrorId().String())
fmt.Println("errorMsg: " + res.ErrorMessage())
fmt.Println("Failed with error json: " + res.ErrorJson())
}
}
Available at: pip install dynamicpdf-api
Ensure you have the required Python libraries.
Create a new file named
DlexLayoutExample.py
.Add a
dlex_layout
method.In
dlex_layout
create a newLayoutData
instance and in the constructor pass the path to the layout data on your local system.Create a new
DlexLayout
instance and in the constructor pass the cloud path in the File Manager and theLayoutData
instance.Add a call to the
process
method the returns aPdfResponse
instance.If the call to the endpoint is successful, then save the response's PDF content to a file.
Run the application
python DlexLayoutExample.py
and the JSON is output to the console.
from dynamicpdf_api.dlex_layout import DlexLayout
from dynamicpdf_api.layout_data_resource import LayoutDataResource
def dlex_layout(apiKey, basePath):
layoutData = LayoutDataResource(basePath + "creating-pdf-dlex-layout.json")
dlexEndpoint =DlexLayout("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutData)
dlexEndpoint.api_key=apiKey
response = dlexEndpoint.process()
if response.is_successful:
with open(basePath + "python-dlex-layout-example.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
# Call the function
api_key = 'DP.---API-KEY---'
basePath = "C:/temp/dynamicpdf-api-samples/"
dlex_layout(api_key, basePath)
In all six languages, the steps were similar. First, we created a new LayoutData
instance by loading the path to the JSON via the constructor. Next, we created a new instance of the DlexLayout
class, which abstracts the dlex-layout
endpoint. In the constructor we passed the path to the DLEX file on the File Manager and the LayoutData
instance. The DlexLayout
instance saves the results as a PDF after processing if the processing was successful. Finally, we called the Process
method and saved the resultant PDF.