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.
Sample | Sample Folder | Resources |
---|---|---|
Create a PDF | samples/create-pdf-pdf-endpoint | Northwind Logo.gif , SimpleReportWithCoverPage.dlex , SimpleReportWithCoverPage.json , instructions.json |
- From the File Manager, download
SimpleReportWithCoverPage.json
andinstructions.json
to your local system; here we assume/temp/dynamicpdf-api-samples/create-pdf-dlex
. - After downloading, delete
instructions.json
andSimpleReportWithCoverPage.json
from your cloud storage space using the File Manager.
Resource | Cloud/Local |
---|---|
Northwind Logo.gif | cloud |
SimpleReportWithCoverPage.dlex | cloud |
SimpleReportWithCoverPage.json | local |
instructions.json | local |
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.
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.
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.
Language | Class | Location (package/namespace/etc.) | GitHub Project |
---|---|---|---|
Java | CreatePdfDlex.java | com.dynamicpdf.api.examples | https://github.com/dynamicpdf-api/java-client-examples |
C# | CreatePdfDlex.cs | DynamicPdfCloudApiClientExamples | https://github.com/dynamicpdf-api/dotnet-client-examples |
Nodejs | CreatePdfDlex.js | nodejs-client-examples | https://github.com/dynamicpdf-api/nodejs-client-examples |
PHP | CreatePdfDlex.php | php-client-examples | https://github.com/dynamicpdf-api/php-client-examples |
GO | pdf-dlex-example.go | go-client-examples | https://github.com/dynamicpdf-api/go-client-examples/tree/main |
Python | PdfDlexExample.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
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 newLayoutDataResource
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);
}
}
}
}
Available on NPM:
npm i @dynamicpdf/api
- Use npm to install the DynamicPDF API module.
- Create a new class named
CompletingAcroForm
. - Create a new static method named
Run
. - Add the following code to the
Run
method. - Create a new
Pdf
instance and a newLayoutDataResource
instance. - Add the path to the DLEX file (in the File Manager) and pass the
LayoutDataResource
instance. - Call the endpoint using the
Process
method. - If the endpoint has no errors, then save the newly created PDF.
- Create a call to run the application,
node CreatePdfDlex.js
. - Execute the application and the PDF is written to your local system.
import fs from 'fs';
import {
Pdf,
LayoutDataResource,
PdfResponse,
PdfResource
} from "@dynamicpdf/api"
export class CreatePdfDlex {
static async Run() {
var pdf = new Pdf();
pdf.apiKey = "DP.xxx-api-key-xxx";
var layoutDataResource = new LayoutDataResource("C:/temp/dynamicpdf-api-samples/create-pdf-dlex/SimpleReportWithCoverPage.json");
pdf.addDlex("samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex", layoutDataResource);
var res = await pdf.process();
if (res.isSuccessful) {
var outFile = "C:/temp/dynamicpdf-api-samples/create-pdf-dlex/create-pdf-dlex-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(res.errorJson);
}
}
}
await CreatePdfDlex.Run();
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.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
- Create a new class named
CompletingAcroform
with amain
method. - Create a new static method named
Run
. - Add the following code to the
Run
method. - Create a new
Pdf
instance and a newLayoutDataResource
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.
- Execute the application and the PDF is written to your filesystem.
package com.dynamicpdf.api.examples;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.dynamicpdf.api.LayoutDataResource;
import com.dynamicpdf.api.Pdf;
import com.dynamicpdf.api.PdfResource;
import com.dynamicpdf.api.PdfResponse;
public class CreatePdfDlex {
public static void main(String[] args) {
CreatePdfDlex.Run("DP.xxx--api-key--xxx",
"C:/temp/dynamicpdf-api-samples/create-pdf-dlex");
}
public static void Run(String apiKey, String basePath)
{
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
LayoutDataResource layoutDataResource = new LayoutDataResource(basePath + "/SimpleReportWithCoverPage.json");
pdf.addDlex("samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex", layoutDataResource);
PdfResponse response = pdf.process();
if(response.getIsSuccessful())
{
try {
FileUtils.writeByteArrayToFile(new File(basePath + "/create-pdf-dlex-output.pdf"),
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
CompletingAcroForm
. - Add a
Run
method. - Create a new static method named
Run
. - Add the following code to the
Run
method. - Create a new
Pdf
instance and a newLayoutDataResource
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.
- Add the call to the
CreatePdfDlex
class. - Run the application,
php CreatePdfDlex.php
and the PDF is written to your local system.
<?php
use DynamicPDF\Api\PdfResource;
use DynamicPDF\Api\Pdf;
use DynamicPDF\Api\LayoutDataResource;
require __DIR__ . '/vendor/autoload.php';
class CreatePdfDlex {
private static string $BasePath = "C:/temp/dynamicpdf-api-samples/create-pdf-dlex/";
public static function Run() {
$pdf = new Pdf();
$pdf->ApiKey ="DP.xxx--api-key--xxx";
$layoutData = new LayoutDataResource(CreatePdfDlex::$BasePath . "SimpleReportWithCoverPage.json");
$pdf->AddDlex("samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex", $layoutData);
//call the pdf endpoint and return response
$response = $pdf->Process();
//if response is successful the save the PDF returned from endpoint
if($response->IsSuccessful)
{
file_put_contents(CreatePdfDlex::$BasePath . "create-pdf-dlex-output.pdf", $response->Content);
} else {
echo($response->ErrorMessage);
}
}
}
CreatePdfDlex::Run();
Available a GO package: https://pkg.go.dev/github.com/dynamicpdf-api/go-client
- Ensure you have the required GO libraries.
- Create a new class named
CompletingAcroForm
. - Add a
main
method. - Create a new
Pdf
instance and a newLayoutDataResource
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.
- Run the application,
go pdf-tutorial-dlex.go
and the PDF is written to your local system.
package main
import (
"fmt"
"os"
"github.com/dynamicpdf-api/go-client/endpoint"
"github.com/dynamicpdf-api/go-client/resource"
)
func main() {
pr := endpoint.NewPdf()
pr.Endpoint.BaseUrl = "https://api.dpdf.io/"
pr.Endpoint.ApiKey = "DP---API-KEY---"
basePath := "c:/temp/dynamicpdf-api-samples/"
layoutDataResource := resource.NewLayoutDataResource(basePath+"SimpleReportWithCoverPage.json", "SimpleReportWithCoverPage.json")
pr.AddDlexWithCloudResourceNLayoutData("samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex", layoutDataResource)
resp := pr.Process()
res := <-resp
if res.IsSuccessful() == false {
if res.ClientError() != nil {
fmt.Print("Failed with error: " + res.ClientError().Error())
} else {
fmt.Print("Failed with error: " + res.ErrorJson())
}
} else {
os.WriteFile(basePath+"pdf-dlex-pdf-output.pdf",
res.Content().Bytes(), os.ModeType)
}
}
Available at: pip install dynamicpdf-api
- Ensure you have the required Python libraries.
- Create a new file named
PdfDlexExample.py
. - Add a
run
method. - Create a new
Pdf
instance and a newLayoutDataResource
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.
- Run the application,
python PdfDlexExample.py
and the PDF is written to your local system.
from dynamicpdf_api.pdf import Pdf
from dynamicpdf_api.page_input import PageInput
from dynamicpdf_api.layout_data_resource import LayoutDataResource
def run(apiKey, basePath):
pdf=Pdf()
pdf.api_key=apiKey
layout = LayoutDataResource(basePath + "SimpleReportWithCoverPage.json")
pdf.add_dlex("samples/creating-pdf-pdf-endpoint/SimpleReportWithCoverPage.dlex", layout)
response = pdf.process()
if response.is_successful:
with open(basePath + "pdf-dlex-output-python.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/"
run(api_key, basePath)
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.