Skip to main content

Creating a PDF from DLEX (pdf Endpoint)


Use the pdf endpoint to generate a PDF report using DLEX.

This tutorial uses a DLEX file and its associated JSON data to create a report. You should be familiar with creating a report using a DLEX file and JSON data.

Required Resources

To complete this tutorial, you must add the Create a PDF sample to your samples folder in your cloud storage space using the File Manager. After adding the sample resources, you should see a samples/create-pdf-pdf-endpoint folder containing the resources for this tutorial.

SampleSample FolderResources
Create a PDFsamples/create-pdf-pdf-endpointNorthwind Logo.gif, SimpleReportWithCoverPage.dlex, SimpleReportWithCoverPage.json, instructions.json
  • From the File Manager, download SimpleReportWithCoverPage.json and instructions.json to your local system; here we assume /temp/dynamicpdf-api-samples/create-pdf-dlex.
  • After downloading, delete instructions.json and SimpleReportWithCoverPage.json from your cloud storage space using the File Manager.
ResourceCloud/Local
Northwind Logo.gifcloud
SimpleReportWithCoverPage.dlexcloud
SimpleReportWithCoverPage.jsonlocal
instructions.jsonlocal
tip

See Sample Resources for instructions on adding sample resources.

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.

Create Instructions JSON

Before we can call the pdf endpoint, we must create the instructions JSON document with the processing instructions for the PDF.

  • Create the following instructions and save to the local folder created above. Save the file as instructions.json.
{
"author": "Alex Smith",
"title": "My PDF From REST API",
"inputs": [
{
"type": "dlex",
"resourceName": "samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex",
"layoutDataResourceName": "SimpleReportWithCoverPage.json"
}
]
}

In this instructions document we use the dlex input element as the input to create the finished PDF. We use a the DLEX and Pdf in cloud storage and SimpleReportWithCoverPage.json locally.

info

Typically you would create the DLEX in the Cloud and test using a test JSON dataset. Then, after the DLEX is correct, you would download the JSON data, or dynamically generate it locally on your client. Even though we take security seriously, you should not store proprietary data in DynamicPDF Cloud Storage.

Calling API Directly Using POST

Completing a form and saving it as a new PDF requires the pdf endpoint. The pdf endpoint uses an instructions JSON document you send to the endpoint. The endpoint then processes the instructions to create the resultant PDF. When you call the pdf endpoint directly, you must create an instructions document. When you use one of the DynamicPDF client libraries, then the client library hides the complexities of creating the instructions document from you.

Let's call the endpoint directly using an instructions document.

  • Create the following cURL command and execute from the command-line.
curl https://api.dpdf.io/v1.0/pdf 
-H "Authorization: Bearer DP.xxx-api-key-xxx"
-F "Instructions=@c:/temp/dynamicpdf-api-samples/create-pdf-dlex/instructions.json"
-F "Resource=@c:/temp/dynamicpdf-api-samples/create-pdf-dlex/SimpleReportWithCoverPage.json"
-o simple-report-output.pdf
  • Open the generated PDF and notice the report followed by the merged PDF.

Figure 2. Generated PDF document consisting of a report and appended pages from PDF.

Make Request Using Client Library

To simplify your development, you can also use one of the DynamicPDF API client libraries. Use the client library of your choice to complete this tutorial section. Each client library tab contains tutorial steps particular to the selected language.

Complete Source

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

LanguageClassLocation (package/namespace/etc.)GitHub Project
JavaCreatePdfDlex.javacom.dynamicpdf.api.exampleshttps://github.com/dynamicpdf-api/java-client-examples
C#CreatePdfDlex.csDynamicPdfCloudApiClientExampleshttps://github.com/dynamicpdf-api/dotnet-client-examples
NodejsCreatePdfDlex.jsnodejs-client-exampleshttps://github.com/dynamicpdf-api/nodejs-client-examples
PHPCreatePdfDlex.phpphp-client-exampleshttps://github.com/dynamicpdf-api/php-client-examples
GOpdf-dlex-example.gogo-client-exampleshttps://github.com/dynamicpdf-api/go-client-examples/tree/main
PythonPdfDlexExample.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 CreatePdfDlex.
  • Add the DynamicPDF.API NuGet package.
  • Create a new static method named Run.
  • Add the following code to the Run method.
  • Create a new Pdf instance and a new LayoutDataResource instance.
  • Add the path to the DLEX file (in cloud storage) and pass the LayoutDataResource instance.
  • Call the endpoint using the Process method.
  • If the endpoint has no errors, then save the newly created PDF.
using DynamicPDF.Api;
using System;
using System.IO;

namespace CreatePdfDlex
{
class Program
{
static void Main(string[] args)
{
Run("DP. api-key", "C:/temp/dynamicpdf-api-samples/create-pdf-dlex/");
}

public static void Run(String apiKey, String basePath)
{
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;
LayoutDataResource layoutDataResource = new LayoutDataResource(basePath + "SimpleReportWithCoverPage.json");
pdf.AddDlex("samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex", layoutDataResource);

PdfResponse response = pdf.Process();

if (!response.IsSuccessful)
{
Console.WriteLine(response.ErrorJson);
}
else
{
File.WriteAllBytes(basePath + "create-pdf-dlex-output.pdf", response.Content);
}
}
}
}

In all six languages, the steps were similar. First, we created a new instance of the Pdf class, which abstracts the pdf endpoint. This Pdf instance holds the created PDF after processing. We then loaded the DLEX's data into the LayoutData class and then the DLEX and the LayoutData instance into the Pdf instance using the AddDlex method. After preparing the Pdf instance, we called the pdf endpoint using the Process method. If successful, then the resultant PDF was saved to the local system.

   Follow us on social media for latest news!