Skip to main content

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. The tutorial first assumes the DLEX file is on the user's local filesystem. It then assumes the DLEX file is in the user's DynamicPDF API cloud storage space.

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.

SampleSample FolderResources
Create PDF (dlex-layout endpoint)samples/creating-pdf-dlex-layout-endpointcreate-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.
tip

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.

tip

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 in Designer

Typically, when using the dlex-layout endpoint you would first create the DLEX in Designer and test using JSON data in Designer with the DLEX as you edited.

info

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.
info

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 cURL

Let's start by using cURL to call the endpoint directly. First, we call it assuming the DLEX file is in your local filesystem. We then assume the DLEX file is in DynamicPDF API cloud storage.

info

The video tutorial uses Postman rather than cURL to call the endpoint.

DLEX Local

In this first example, the DLEX file resides on your local filesystem.

  • Create a new POST request and add the two form fields listed in the following table. Note that the LayoutData and the dlex are files on your local system.
ParameterParameter TypeValue
AuthorizationHeaderBearer: DP.xxx-api-key-xxx
ResourceForm file fieldC:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout/creating-pdf-dlex-layout.dlex
LayoutDataForm file field.C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout/creating-pdf-dlex-layout.json
ResourceForm file field.c:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout/creating-pdf-dlex-layout.png

The creating-pdf-dlex-layout.png image is embedded in the dlex file as a relative path and therefore must be included as a local resource when calling the endpoint.

<page id="Page1" pageSize="letter" pageOrientation="portrait" leftMargin="50" topMargin="50" rightMargin="50" bottomMargin="50">
<image id="Image1" x="196" y="146" width="81" height="90" path="creating-pdf-dlex-layout.png"/>
<recordBox id="RecordBox2" x="56" y="258" width="386" height="20" align="center" fontSize="18" underline="false" autoLeading="true" cleanParagraphBreaks="true" expandable="false" splittable="false" dataName="title"/>
<recordBox id="RecordBox3" x="201" y="297" width="100" height="14" align="center" underline="false" autoLeading="true" cleanParagraphBreaks="true" expandable="false" splittable="false" dataName="Author"/>
</page>
  • Create the following cURL command.
  • Set the DLEX and image as Resource fields that send a file from your local filesystem.
  • Set the LayoutData as also coming from your local filesystem.
curl -X POST "https://api.dpdf.io/v1.0/dlex-layout" 
-H "Authorization: Bearer DP.xxx-api-key-xxx"
-F "Resource=@C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex"
-F "Resource=@C:/temp/dynamicpdf-api-samples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.png"
-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.
info

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.

DLEX Remote

In this second example the dlex file resides in cloud storage.

  • Create a new POST request and add the two form fields listed in the following table. Note that the LayoutData is a file on your local system while DlexPath refers to the DLEX file in the File Manager.
ParameterParameter TypeValue
AuthorizationHeaderBearer: DP.xxx-api-key-xxx
DlexPathForm Fieldsamples/creating-pdf-dlex-layout-endpoint/create-pdf-dlex-layout.dlex
LayoutDataForm file field.C:/temp/dynamicpdf-api-samples/creatingpdf-dlex-layout/create-pdf-dlex-layout.json

The creating-pdf-dlex-layout.png image is embedded in the dlex file as a relative path and must resided in the same folder in DynamicPDF API cloud storage as the dlex.

  • Create the following cURL command where the PDF is sent to the endpoint as binary data.
curl -X POST "https://api.dpdf.io/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.

Calling Endpoint Using Client Library

You can also use one of the DynamicPDF API client libraries. Use the client library of your choice to complete this tutorial section.

info

For all client libraries, the important coding point to remember is how you handle the dlex by the endpoint. If obtaining the dlex from your local system, then create a DlexResource class (or the client language's equivalent). If obtaining the dlex from cloud storage, then the endpoint class has a method where the relative path is specified as a string.

danger

Be certain to add any local resources used by the dlex if you are loading the dlex from your local system.

Complete Source

You can access the complete source for this project from one of the following GitHub projects.

LanguageFile NameLocation (package/namespace/etc.)GitHub Project
JavaCreatingPdfDlexLayout.javacom.dynamicpdf.api.exampleshttps://github.com/dynamicpdf-api/java-client-examples
C#Program.csCreatingPdfDlexLayouthttps://github.com/dynamicpdf-api/dotnet-client-examples
NodejsCreatingPdfDlexLayout.jsnodejs-client-exampleshttps://github.com/dynamicpdf-api/nodejs-client-examples
PHPCreatingPdfDlexLayout.phpphp-client-exampleshttps://github.com/dynamicpdf-api/php-client-examples
GOdlex-layout-example.gogo-client-exampleshttps://github.com/dynamicpdf-api/go-client-examples/tree/main
PythonDlexLayoutExample.pypython-client-exampleshttps://github.com/dynamicpdf-api/python-client-examples
tip

Click on the language tab of choice to view the tutorial steps for the particular language.

Available on NuGet:

Install-Package DynamicPDF.API

The project is named DlexLayoutTutorial and is in the dotnet-client-examples solution. It is a simple wrapper to the DlexLayoutExample class in the DynamicPdfClientLibraryExamples project.

  • It first sets the api key and the base path containing the resources for the tutorial.
  • It then calls the DlexLayoutExample class's Run method.
using DynamicPdfClientLibraryExamples.Examples;
using DynamicPdfClientLibraryExamples.Utility;

namespace DlexLayoutTutorial
{
class Program
{
static void Main(string[] args)
{
string apiKey = "DP---api-key---";
string basePath = "../DynamicPdfClientLibraryExamples/Resources/creating-pdf-dlex-layout-endpoint/";
string outputPath = "Output";

FileUtility.CreatePath(outputPath);
DlexLayoutExample.Run(apiKey, FileUtility.GetPath(basePath), FileUtility.GetPath(outputPath));

}
}
}
Source: Program.cs
  • The DynamicPdfClientLibraryExamples class then calls two methods, RunFromCloud and RunFromLocal.

RunFromCloud

  • The RunFromCloud method, when loading the dlex file, gets the file from cloud storage.
  • The dlex file is loaded directly via the DlexLayout class constructor, where it sets the relative path to the dlex file in cloud storage and the LayoutDataResource instance as constructor parameters.

RunFromLocal

  • The RunFromLocal method, when loading the dlex file, creates a new DlexResource instance using the local path to the dlex file.
  • The method then adds the png image as an additional resource to the DlexLayout endpoint.
using DynamicPDF.Api;
using System;
using System.IO;

namespace DynamicPdfClientLibraryExamples.Examples
{
public class DlexLayoutExample
{

public static void Run(string apiKey, string basePath, string outputPath)
{
RunFromCloud(apiKey, basePath, outputPath);
RunFromLocal(apiKey, basePath, outputPath);
}

public static void RunFromCloud(string apiKey, string basePath, string outputPath)
{
LayoutDataResource layoutData = new LayoutDataResource(basePath + "creating-pdf-dlex-layout.json");
DlexLayout dlexEndpoint = new DlexLayout("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutData);
dlexEndpoint.ApiKey = apiKey;
PdfResponse response = dlexEndpoint.Process();

if (response.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/csharp-dlex-layout-example-output.pdf", response.Content);
}
else
{
Console.WriteLine(response.ErrorJson);
}
}

public static void RunFromLocal(string apiKey, string basePath, string outputPath)
{
LayoutDataResource layoutData = new LayoutDataResource(basePath + "creating-pdf-dlex-layout.json");
DlexResource dlexResource = new DlexResource(basePath + "creating-pdf-dlex-layout.dlex", "creating-pdf-dlex-layout.dlex");
DlexLayout dlexEndpoint = new DlexLayout(dlexResource , layoutData);
dlexEndpoint.AddAdditionalResource(basePath + "creating-pdf-dlex-layout.png", "creating-pdf-dlex-layout.png");
dlexEndpoint.ApiKey = apiKey;
PdfResponse response = dlexEndpoint.Process();

if (response.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/csharp-dlex-layout-local-example-output.pdf", response.Content);
}
else
{
Console.WriteLine(response.ErrorJson);
}
}
}
}
Source: DlexLayoutExample.cs

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.

Remote DLEX

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.

Local DLEX

We created a new instance of a DlexResource where we passed the path to the DLEX file on your local filesystem. We then created an instance of the DlexLayout class and in the constructor we passed theDlexResource instance and the LayoutData instance. We also added the png as an additional resource.

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.

   Follow us on social media for latest news!