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.

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.
ResourceCloud/Local
create-pdf-dlex-layout.dlexcloud
create-pdf-dlex-layout.pngcloud
create-pdf-dlex-layout.jsonlocal
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 on Cloud

Typically, when using the dlex-layout endpoint you would first create the DLEX on the cloud using test data.

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 API

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

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.

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
  • 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 new LayoutData 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 a PdfResponse 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);
}
}
}
}
}

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.

   Follow us on social media for latest news!