dlex-layout
Use the dlex-layout endpoint with one of DynamicPDF's client libraries to create PDF reports from DLEX files.
The dlex-layout
endpoint is for creating PDF reports from DLEX files. The endpoint consists of two form field parameters and returns a PDF document. The dlex-layout
endpoint uses a DLEX file combined with JSON data to create PDF reports. It is an HTTP POST command that submits a form.
Refer to the following Users Guide page if you need more information illustrating how to call the endpoint directly as a REST call.
- Calling the
dlex-layout
endpoint using REST (dlex-layout REST API).
API
The dlex-layout
endpoint uses JSON layout data and a DLEX file to create a resultant PDF. Layout data can come from a file, a string, or a serializable object (Java, C#).
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
The dlex-layout
endpoint is represented in the client library using the DlexLayout
class. A DlexLayout
class takes a path to the DLEX data and a LayoutDataResource
instance. The DlexLayout
class also has an overloaded constructor that takes a path to a resource residing on your local system; however, it requires using a DlexResource
instance. The LayoutDataResource
instance can take an object, a path to the layout data, or the layout data as a string.
public class DlexLayout : Endpoint
{
public DlexLayout(string cloudDlexPath, LayoutDataResource layoutData);
public DlexLayout(DlexResource dlexResource, LayoutDataResource layoutData);
public string DlexPath;
public PdfResponse Process();
public Task<PdfResponse> ProcessAsync();
}
public class LayoutDataResource : Resource
{
//
// Summary:
// Initializes a new instance of the DynamicPDF.Api.LayoutDataResource class using
// the layout data object and a resource name.
//
// Parameters:
// layoutData:
// Serializable object data to create PDF report.
//
// layoutDataResourceName:
// The name for layout data resource.
public LayoutDataResource(object layoutData, string layoutDataResourceName = null);
//
// Summary:
// Initializes a new instance of the DynamicPDF.Api.LayoutDataResource class using
// a JSON file with data and a resource name.
//
// Parameters:
// layoutData:
// The layout data JSON file path or JSON string.
//
// layoutDataResourceName:
// The name for layout data resource.
public LayoutDataResource(string layoutData, string layoutDataResourceName = null);
}
The dlex-layout
endpoint is represented in the client library using the DlexLayout
class. A DlexLayout
class takes a path to the DLEX data and a LayoutDataResource
instance. The LayoutDataResource
instance can take a Buffer
containing a JSON string or a path to the layout data.
When passing string data to LayoutDataResource
, you must pass a Buffer
not a string. If you pass a string then LayoutDataResource
interprets the string as a filepath.
var fileData = fs.readFileSync("C:/temp/data.json");
var layoutData = new LayoutDataResource(fileData);
/**
* Represents a Dlex layout endpoint.
*/
export class DlexLayout extends Endpoint {
resource;
#form;
_Resources = [];
/** Gets or sets the DLEX file path present in the resource manager. */
dlexPath;
/**
* Initializes a new instance of the < see cref="DlexLayout"/> class using the
DLEX file path present in the cloud environment and the JSON data for the PDF report.
* @param {string} cloudDlexPath The DLEX file path present in the resource manager
* @param {LayoutDataResource} layoutData The `LayoutDataResource` json data file used to create the PDF report.
* @param {DlexResource} dlexResource The DLEX file and the JSON data file from the client to the API to create the PDF report.*/
constructor(dlex, layoutData) {
The dlex-layout
endpoint is represented in the client library using the DlexLayout
class. A DlexLayout
class takes a path to the DLEX data and a LayoutDataResource
instance. The LayoutDataResource
instance can take an object, a path to the layout data, or the JSON as a string.
public class DlexLayout extends Endpoint
{
/**
* Initializes a new instance of the <code>DlexLayout</code> class using the
* DLEX file path present in the cloud environment and the JSON data for the PDF report.
* @param cloudDlexPath The DLEX file path present in the resource manager.
* @param layoutData The <code>LayoutDataResource</code>, json data file used to create the PDF report.
*/
public DlexLayout(String cloudDlexPath, LayoutDataResource layoutData) {
super();
dlexPath = cloudDlexPath;
this.resource = layoutData;
}
/**
* Initializes a new instance of the <code>DlexInput</code> class by posting the
* DLEX file and the JSON data file from the client to the API to create the PDF report.
* @param dlexResource The <code>DlexResource</code> dlex file created as per the desired PDF report design.
* @param layoutData The <code>LayoutDataResource</code>, json data file used to create the PDF report.
*/
public DlexLayout(DlexResource dlexResource, LayoutDataResource layoutData) {
super();
getResources().add(dlexResource);
this.resource = layoutData;
}
}
public class LayoutDataResource extends Resource {
/**
* Initializes a new instance of the <code>LayoutDataResource</code> class
* using a JSON file with data and a resource name.
* @param layoutData The layout data JSON file path or JSON string.
* @param layoutDataResourceName The name for layout data resource.
*/
public LayoutDataResource(Object layoutData, String layoutDataResourceName);
/**
* Initializes a new instance of the <code>LayoutDataResource</code> class
* using the layout data object.
* @param layoutData Serializable object data to create PDF report.
*/
public LayoutDataResource(Object layoutData);
/**
* Initializes a new instance of the <code>LayoutDataResource</code> class
* using a JSON file with data and a resource name.
* @param layoutData The layout data JSON file path or JSON string.
* @param layoutDataResourceName The name for layout data resource.
*/
public LayoutDataResource(String layoutData, String layoutDataResourceName);
/**
* Initializes a new instance of the <code>LayoutDataResource</code> class
* using a JSON file with data.
* @param layoutData The layout data JSON file path or JSON string.
*/
public LayoutDataResource(String layoutData);
}
}
The dlex-layout
endpoint is represented in the client library using the DlexLayout
class. A DlexLayout
class takes a path to the DLEX data and a LayoutDataResource
instance. The LayoutDataResource
instance can take an object or a path to the layout data. Layout data can be a file path or binary.
class DlexLayout extends Endpoint
{
public function __construct(string $cloudDlexPath, LayoutDataResource $layoutData
public $DlexPath;
public function Process(): PdfResponse;
}
class LayoutDataResource extends Resource
{
/**
*
* Initializes a new instance of the LayoutDataResource
* class using the layout data object and a resource name.
*
* @param object|string $layout Serializable object data to create PDF
* report or the layout data JSON file path.
* @param string $layoutDataResourceName The name for layout data resource.
*/
public function __construct($layout = null, string $layoutDataResourceName = null)
}
The dlex-layout
endpoint is represented in the client library using the DlexLayout
class. A DlexLayout
class takes a path to the DLEX data and a LayoutDataResource
instance. The LayoutDataResource
instance can take an object or a path to the layout data. Layout data can be a file path or binary.
func NewDlexEndpoint(cloudDlexPath string, resource resource.LayoutDataResource) *Dlex
type LayoutDataResource struct {
Resource
}
/*
* Initializes a new instance of the `LayoutDataResource` class
* using the layout data object and a resource name.
* @param {string }The layout data JSON file path.
* @param {string} layoutDataResourceName The name for layout data resource.
*/
func NewLayoutDataResource(filepath string, layoutdataresourcename string) LayoutDataResource
The dlex-layout
endpoint is represented in the client library using the DlexLayout
class. A DlexLayout
class takes a path to the DLEX data and a LayoutDataResource
instance. The LayoutDataResource
instance can take an object or a path to the layout data. Layout data can be a file path or binary.
class DlexLayout(Endpoint):
'''
Represents a Dlex layout endpoint.
'''
def __init__(self,cloud_dlex_path, layout_data):
'''
Initializes a new instance of the <see cref="DlexLayout"/> class using the
DLEX file path present in the cloud environment and the JSON data for the PDF report.
Args:
cloudDlexPath (string): The DLEX file path present in the resource manager
layoutData (LayoutDataResource): The LayoutDataResource json data file used to create the PDF report.
Example
A complete example using JSON data from a file is available via one of the following GitHub projects depending upon the language you wish to use.
Do not be misled into thinking layout data must be a file. In a production setting you will usually generate layout data dynamically and then send it to the DynamicPDF client library as a string buffer or serializable object hierarchy. Refer to the relevant client library's example project on GitHub for example projects using a string buffer or serializable object hierarchy. The following table list the relevant classes on GitHub.
Language | GitHub Users Guide Project | Class/File | Location/Package/Namespace |
---|---|---|---|
C# | https://github.com/dynamicpdf-api/dotnet-client-examples | Program.cs , Program.cs | DlexLayout , DlexLayoutObjectExample |
Go | https://github.com/dynamicpdf-api/go-client | dlex-layout-example.go | go-client-examples |
Java | https://github.com/dynamicpdf-api/java-client-examples | DlexLayoutExample.java , DlexLayoutExampleString.java , DlexLayoutExampleObject.java | com.dynamicpdf.api.examples |
Node.js | https://github.com/dynamicpdf-api/nodejs-client-examples | DlexLayoutExample.js , DlexLayoutStringBufferExample.js | nodejs-client-examples |
PHP | https://github.com/dynamicpdf-api/php-client-examples | DlexLayoutExample.js , DlexLayoutString.php | php-client-examples |
Python | https://github.com/dynamicpdf-api/python-client-examples | DlexLayoutExample.py | python-client-examples |
Obtain the resources used by the example from the DynamicPDF API File Manager.
Figure 1. The resources for the dlex-layout code examples.
In the following examples, the processing steps and syntax are consistent across languages. Each client library has the following classes.
- The
LayoutDataResource
holds the JSON dataset used by the DLEX file to create the finished report. - The
DlexLayout
instance holds the DLEX file containing and theLayoutDataResource
instance. - The
DlexLayout
instance calls theProcess
method and, if successful, returns aPdfResponse
containing the completed PDF.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
The following example illustrates using layout data from a JSON file. It also illustrates using a DLEX file in the cloud and a local DLEX file.
If your DLEX file has embedded resources such as images, then you must include them as an additional resource.
Refer to the DlexLayoutObjectExample
project in dotnet-client-examples on GitHub for an example using a simple serializable object.
public static void RunFromCloud(string apiKey, string basePath, string outputPath)
{
LayoutDataResource layoutData = new LayoutDataResource(basePath + "SimpleReportWithCoverPage.json");
DlexLayout dlexEndpoint = new DlexLayout("samples/dlex-layout/SimpleReportWithCoverPage.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 + "SimpleReportWithCoverPage.json");
DlexResource dlexResource = new DlexResource(basePath + "SimpleReportWithCoverPage.dlex", "SimpleReportWithCoverPage.dlex");
DlexLayout dlexEndpoint = new DlexLayout(dlexResource , layoutData);
dlexEndpoint.AddAdditionalResource(basePath + "NorthwindLogo.gif", "NorthwindLogo.gif");
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);
}
}
The following example illustrates using layout data from a JSON file. It also illustrates using a DLEX file in the cloud and a local DLEX file.
If your DLEX file has embedded resources such as images, then you must include them as an additional resource.
Refer to DlexLayoutStringBufferExample.js
in the nodejs-client-examples project on GitHub for examples using JSON data from serializable objects.
static async RunFromCloud(apiKey, localPath) {
var layoutData = new LayoutDataResource(localPath + "SimpleReportWithCoverPage.json");
var dlexEndpoint = new DlexLayout("samples/dlex-layout/SimpleReportWithCoverPage.dlex", layoutData);
dlexEndpoint.apiKey = apiKey;
var res = await dlexEndpoint.process();
if (res.isSuccessful) {
var outFile = localPath + "nodejs-dlex-layout-example-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(res.errorJson);
}
}
static async RunFromLocal(apiKey, localPath) {
var layoutData = new LayoutDataResource(localPath + "SimpleReportWithCoverPage.json");
var dlexResource = new DlexResource(localPath + "SimpleReportWithCoverPage.dlex", "SimpleReportWithCoverPage.dlex");
var dlexEndpoint = new DlexLayout(dlexResource, layoutData);
dlexEndpoint.dlexAdditionalResource(localPath + "NorthwindLogo.gif", "NorthwindLogo.gif");
dlexEndpoint.apiKey = apiKey;
var res = await dlexEndpoint.process();
if (res.isSuccessful) {
var outFile = localPath + "nodejs-dlex-layout-example-local-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(res.errorJson);
}
}
The following example illustrates using layout data from a JSON file. It also illustrates using a DLEX file in the cloud and a local DLEX file.
If your DLEX file has embedded resources such as images, then you must include them as an additional resource.
Refer to DlexLayoutExampleObject.java
in the java-client-examples project on GitHub for examples using JSON data from serializable objects.
public static void RunFromCloud(String apiKey, String basePath) {
LayoutDataResource layoutData = new LayoutDataResource(basePath + "SimpleReportWithCoverPage.json");
DlexLayout dlexEndpoint = new DlexLayout("samples/dlex-layout/SimpleReportWithCoverPage.dlex", layoutData);
dlexEndpoint.setApiKey(apiKey);
PdfResponse response = dlexEndpoint.process();
if (response.getIsSuccessful()==true) {
try {
FileUtils.writeByteArrayToFile(new File(basePath + "java-dlex-layout-output.pdf"),
(byte[])response.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else { System.out.println(PrettyPrintUtility.prettyPrintJSON(response.getErrorJson()));
}
}
public static void RunFromLocal(String apiKey, String basePath) {
LayoutDataResource layoutData = new LayoutDataResource(basePath + "SimpleReportWithCoverPage.json");
DlexResource dlexResource = new DlexResource(basePath + "SimpleReportWithCoverPage.dlex", "SimpleReportWithCoverPage.dlex");
DlexLayout dlexEndpoint = new DlexLayout(dlexResource, layoutData);
dlexEndpoint.addAdditionalResource(basePath + "NorthwindLogo.gif", "NorthwindLogo.gif");
dlexEndpoint.setApiKey(apiKey);
PdfResponse response = dlexEndpoint.process();
if (response.getIsSuccessful()==true) {
try {
FileUtils.writeByteArrayToFile(new File(basePath + "java-dlex-layout-local-output.pdf"),
(byte[])response.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else { System.out.println(PrettyPrintUtility.prettyPrintJSON(response.getErrorJson()));
}
}
If your DLEX file has embedded resources such as images, then you must include them as an additional resource.
The following example illustrates using layout data from a JSON file. It also illustrates using a DLEX file in the cloud and a local DLEX file.
public static function RunCloud(string $apikey, string $path, string $output_path)
{
$layoutData = new LayoutDataResource($path . "SimpleReportWithCoverPage.json");
$dlexEndpoint = new DlexLayout("samples/dlex-layout/SimpleReportWithCoverPage.dlex", $layoutData);
$dlexEndpoint->ApiKey = $apikey;
$response = $dlexEndpoint->Process();
if($response->IsSuccessful)
{
file_put_contents($output_path . "php-dlex-layout-example-output.pdf", $response->Content);
} else {
echo($response->ErrorJson);
}
}
public static function RunLocal(string $apikey, string $path, string $output_path)
{
$layoutData = new LayoutDataResource($path . "SimpleReportWithCoverPage.json");
$dlexResource = new DlexResource($path . "SimpleReportWithCoverPage.dlex", "SimpleReportWithCoverPage.dlex");
$dlexEndpoint = new DlexLayout($dlexResource, $layoutData);
$dlexEndpoint->AddAdditionalResource($path . "NorthwindLogo.gif", "NorthwindLogo.gif");
$dlexEndpoint->ApiKey = $apikey;
$response = $dlexEndpoint->Process();
if($response->IsSuccessful)
{
file_put_contents($output_path . "php-dlex-layout-local-example-output.pdf", $response->Content);
} else {
echo($response->ErrorJson);
}
}
The following example illustrates using layout data from a JSON file. It also illustrates using a DLEX file in the cloud and a local DLEX file.
If your DLEX file has embedded resources such as images, then you must include them as an additional resource.
func processCloud() {
layoutDataResource := resource.NewLayoutDataResource("./resources/dlex-layout/SimpleReportWithCoverPage.json", "SimpleReportWithCoverPage.json")
layoutData := endpoint.NewDlexEndpoint("samples/dlex-layout/SimpleReportWithCoverPage.dlex", layoutDataResource)
layoutData.Endpoint.BaseUrl = "https://api.dpdf.io/"
layoutData.Endpoint.ApiKey = "DP.--api-key--"
resp := layoutData.Process()
res := <-resp
if res.IsSuccessful() == true {
os.WriteFile("./output/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())
}
}
func processLocal() {
layoutDataResource := resource.NewLayoutDataResource("./resources/dlex-layout/SimpleReportWithCoverPage.json", "SimpleReportWithCoverPage.json")
theDlexResource := resource.NewDlexResourceWithPath("./resources/dlex-layout/SimpleReportWithCoverPage.dlex", "SimpleReportWithCoverPage.dlex")
layoutData := endpoint.NewDlexEndpointWithResource(*theDlexResource, layoutDataResource)
additionalResource := endpoint.NewDlexWithAdditionalResource("./resources/dlex-layout/NorthwindLogo.gif", "NorthwindLogo.gif")
layoutData.Resources = append(layoutData.Resources, additionalResource);
layoutData.Endpoint.BaseUrl = "https://api.dpdf.io/"
layoutData.Endpoint.ApiKey = "DP--api-key--"
resp := layoutData.Process()
res := <-resp
if res.IsSuccessful() == true {
os.WriteFile("./output/dlex-layout-local-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())
}
The following example illustrates using layout data from a JSON file. It also illustrates using a DLEX file in the cloud and a local DLEX file.
def dlex_layout_cloud(apiKey, full_path):
layoutData = LayoutDataResource(full_path + "SimpleReportWithCoverPage.json")
dlexEndpoint =DlexLayout("samples/dlex-layout/SimpleReportWithCoverPage.dlex", layoutData)
dlexEndpoint.api_key=apiKey
response = dlexEndpoint.process()
if response.is_successful:
with open(output_path + "python-dlex-layout-example.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
def dlex_layout_local(apiKey, full_path):
layoutData = LayoutDataResource(full_path + "SimpleReportWithCoverPage.json")
dlexResource = DlexResource(full_path + "SimpleReportWithCoverPage.dlex", "SimpleReportWithCoverPage.dlex")
dlexEndpoint =DlexLayout(dlexResource, layoutData)
dlexEndpoint.add_additional_resource(full_path + "NorthwindLogo.gif")
dlexEndpoint.api_key=apiKey
response = dlexEndpoint.process()
if response.is_successful:
with open(output_path + "python-dlex-layout-local-example.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)