pdf Inputs
The pdf endpoint has six input types dlex, image, html, word, page, and pdf. Use these inputs to create rich PDF documents.
The DynamicPDF API's pdf
endpoint uses a JSON document that provides processing instructions for manipulating PDFs. The instructions document has six input types.
endpoint | description |
---|---|
dlex | Specifies a DLEX document used with JSON layoutdata to create a rich PDF document/report. |
image | Specifies an image that is converted into a PDF. |
html | Specifies an HTML document or fragment that is converted into a PDF. |
word | Specifies a Word document that is converted into a PDF. |
excel | Specifies an Excel document that is converted into a PDF. |
page | Specifies one or more inputs to create a new single-page PDF. |
pdf | Specifies a pre-existing PDF to be be combined with one or the other input types to create a new PDF. Optionally allows applying templates to an existing PDF to overlay elements such as text, shapes, and images. |
Refer to documentation on the instructions schema for more general information on how to use the pdf
endpoint.
Use the six input types in any combination to combine into a merged PDF document.
For all inputs, input the relative path to the resource as a string if using a resource such as a DLEX file, image, or layout data that resides in your DynamicPDF API's cloud storage. Otherwise, you will use one of the input's associated resource classes if the resource is on your local system.
The resources for all the examples in this documentation are available from samples/users-guide-resources
in cloud storage. See environment-manager-sample-resources for more information.
DlexInput
The DlexInput
class adds a DLEX file to an instructions document for processing by the pdf
endpoint. This input type is used in combination with DynamicPDF Designer to create rich reports/documents.
See the Designer documentation for more information on using Designer.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
The .NET C# client library DlexInput
class has the following constructors.
Constructors
public DlexInput(DlexResource dlexResource, LayoutDataResource layoutData);
public DlexInput(DlexResource dlexResource, string layoutData);
public DlexInput(string cloudResourcePath, LayoutDataResource layoutData);
public DlexInput(string cloudResourcePath, string layoutData);
- The
DlexResource
class obtains the DLEX for processing. - The
LayoutDataResource
class obtains the JSON data processed by the DLEX to create the resultant PDF report. - The
cloudResourcePath
string passes the relative path to a DLEX file in your cloud storage. - The
layoutData
string that passes the path in your local storage to the JSON layout data.
You do not create a DlexInput
instance directly. Instead, you pass the DlexResource
and LayoutDataResource
to the Pdf
class's AddDlex
method.
Properties
The DlexInput
class has one property, which you set via the Pdf
AddDlex
method rather than through a DlexInput
instance.
public string LayoutDataResourceName { get; set; }
LayoutDataResource
The LayoutDataResource
class accepts a serializable object containing JSON data or a string. If an object, then the JSON-compatible object is serialized into JSON using the Newsoft.Json.Serialization
class. If a string, then the string is assumed to be a path to a JSON document.
Constructors
public LayoutDataResource(object layoutData, string layoutDataResourceName = null);
public LayoutDataResource(string layoutData, string layoutDataResourceName = null);
Properties
public string LayoutDataResourceName { get; set; }
DlexResource
The DlexResource
class accepts a string, byte array, or a Stream
. If a string, the constructor assumes a path to a DLEX file. Otherwise, it expects the data as either a byte array or Stream
.
Constructors
public DlexResource(string dlexPath, string resourceName = null) : base(dlexPath, resourceName)
public DlexResource(byte[] value, string resourceName = null)
public DlexResource(Stream data, string resourceName = null) : base(data, resourceName)
Examples
The following example illustrates using a DLEX file and JSON file that reside on a local filesystem.
Pdf pdf = new Pdf();
DlexResource dlex = new DlexResource("c:/temp/dlex-resource/SimpleReportWithCoverPage.dlex");
LayoutDataResource layout = new LayoutDataResource("c:/temp/dlex-resource/SimpleReportWithCoverPage.json");
pdf.AddDlex(dlex, layout);
return pdf;
The DlexResource
and LayoutDataResource
classes have constructors that allow a byte array or a stream. The data could come from any number of sources, including a database. The following example illustrates.
If you use a DLEX file that resides locally on your system, remember any embedded resources must also reside in a path reachable by the DLEX.
Pdf pdf = new Pdf();
//get the DLEX from local filesystem
DlexResource dlexResource = new DlexResource("c:/temp/users-guide-resources/SimpleReportWithCoverPage.dlex");
LayoutDataResource layout = new LayoutDataResource("c:/temp/users-guide-resources/SimpleReportWithCoverPage.json");
pdf.AddDlex(dlexResource, layout);
//get the image embedded in the dlex from local filesystem
pdf.AddAdditionalResource("c:/temp/users-guide-resources/NorthwindLogo.gif");
//get layout data as a string
string stringLayoutData = File.ReadAllText("c:/temp/users-guide-resources/SimpleReportWithCoverPage.json");
LayoutDataResource layoutTwo = new LayoutDataResource(stringLayoutData);
//get dlex from cloud storage
pdf.AddDlex("samples/users-guide-resources/SimpleReportWithCoverPage.dlex", layoutTwo);
return pdf;
Refer to DlexLayoutObjectExample
in the dotnet-client-examples
(hhttps://github.com/dynamicpdf-api/dotnet-client-examples/blob/main/DynamicPdfClientLibraryExamples/Examples/InstructionsExample.cs) for an example using serializable objects as layout data.
The Nodejs client library's DlexInput
class has the following constructor.
Constructors
The constructor initializes a new instance of the DlexInput
class by:
- posting the DLEX file and the JSON data file or,
- the DLEX file path that is present in the cloud environment and the JSON data file or,
- the DLEX file path and DLEX data file path that is present in the cloud environment from the client to the API to create the PDF report.
export class PdfInput extends Input {
/**Gets or sets the merge options `MergeOptions` */
mergeOptions;
/**Gets or sets the start page. */
startPage;
/** Gets or sets the page count.*/
pageCount;
/**
* Initializes a new instance of the `PdfInput` class.
* @param { PdfResource | string } resource The resource of type `PdfResource`. | The resource path in cloud resource manager.
* @param {MergeOptions} mergeOptions The merge options for the pdf.
*/
constructor(resource, mergeOptions = null) {
super(resource);
if (mergeOptions != null)
this.mergeOptions = mergeOptions;
this._Type = inputType.pdf;
}
You do not create a DlexInput
instance directly. Instead, you pass the DlexResource
and LayoutDataResource
to the Pdf
class's addDlex
method.
LayoutDataResource
The LayoutDataResource
has one constructor that takes either the JSON data as a Buffer
or as a file path to the JSON data. It also includes an optional resource name.
Constructors
export class LayoutDataResource extends Resource {
#fileExtension = ".json";
/** Gets or sets name of the layout data resource. */
layoutDataResourceName;
/**
* Initializes a new instance of the `LayoutDataResource` class
* using the layout data object and a resource name.
* @param {Buffer | string } layoutData Serializable object data (utf-8 Buffer) to create PDF report or the layout data JSON file path (string).
* @param {string} [layoutDataResourceName=null] The name for layout data resource.
*/
constructor(layoutData, layoutDataResourceName = null) {
DlexResource
The DlexResource
class has one constructor which takes either a string path to the resource file or the data as a Buffer
. It also allows an optional resource name.
Constructors
export class DlexResource extends Resource {
#FileExtension = ".dlex";
/** Gets or sets name for layout data resource. */
layoutDataResourceName;
/**
* Initializes a new instance of the `DlexResource` class
* with DLEX file path and resource name or
* byte data of the DLEX file and resource name as parameters.
* @param {string | Buffer[]} dlex The dlex file path. | The Buffer array of the dlex file.
* @param {string} resource The name of the resource.
*/
constructor(dlex, resource = null) {
Examples
The following two examples illustrate using a local DLEX file with local layout data to create a PDF using the pdf
endpoint. The first example uses a string to obtain the DLEX file. The second reads the DLEX file into an object and then loads the binary into a DlexResource
.
If you use a DLEX file that resides locally on your system, remember any embedded resources must also reside in a path reachable by the DLEX.
var pdf = new Pdf();
var dlex = new DlexResource("c:/temp/dlex-resource/SimpleReportWithCoverPage.dlex");
var layout = new LayoutDataResource("c:/temp/dlex-resource/SimpleReportWithCoverPage.json");
pdf.addDlex(dlex, layout);
var pdf = new Pdf();
var dlexStream = fs.readFileSync("c:/temp/dlex-resource/SimpleReportWithCoverPage.dlex", "utf8");
var dlex = new DlexResource(dlexStream);
var layout = new LayoutDataResource("c:/temp/dlex-resource/SimpleReportWithCoverPage.json");
pdf.addDlex(dlex, layout);
The Java client library DlexInput
class has the following constructors.
Constructors
public DlexInput(DlexResource dlexResource, LayoutDataResource layoutData)
public DlexInput(DlexResource dlexResource, String layoutData)
public DlexInput(String cloudResourcePath, LayoutDataResource layoutData)
public DlexInput(String cloudResourcePath, String layoutData)
- The
DlexResource
class obtains the DLEX for processing - The
LayoutDataResource
class obtains the JSON data that is processed by the DLEX to create the resultant PDF report. - The
cloudResourcePath
is a string that passes the relative path to a DLEX file in your cloud storage. - The
layoutData
is a string that passes the path in your local storage to the JSON layout data.
You do not create a DlexInput
instance directly. Instead, you pass the DlexResource
and LayoutDataResource
to the Pdf
class's AddDlex
method.
Properties
The DlexInput
class has one property, which you set via the Pdf
addDlex
method rather than through a DlexInput
instance.
private String layoutDataResourceName
LayoutDataResource
The LayoutDataResource
class accepts a serializable object containing JSON data or a string. If an object, then the JSON compatible object is serialized into JSON. If a string, then the string is assumed to be a path to a JSON document.
Constructors
public LayoutDataResource(Object layoutData, String layoutDataResourceName)
public LayoutDataResource(Object layoutData)
public LayoutDataResource(String layoutData, String layoutDataResourceName)
public LayoutDataResource(String layoutData)
Properties
private ResourceType type = ResourceType.LAYOUTDATA;
private String layoutDataResourceName;
DlexResource
The DlexResource
class accepts a string, byte array, or a InputStream
. If a string, the constructor assumes a path to a DLEX file. Otherwise, it expects the data as either a byte array or an InputStream
.
Constructors
public DlexResource(String dlexPath, String resourceName)
public DlexResource(String dlexPath)
public DlexResource(byte[] value, String resourceName)
public DlexResource(byte[] value)
public DlexResource(InputStream data, String resourceName)
public DlexResource(InputStream data)
If you use a DLEX file that resides locally on your system, remember any embedded resources must also reside in a path reachable by the DLEX.
Example
The following example illustrates creating a PDF where the DLEX file and the layout data JSON file resides on your local file system and then creating a PDF where the DLEX resides in cloud storage and the JSON is binary string data.
// combine two pdf reports into single report (merge)
Pdf pdf = new Pdf();
//create pdf using local dlex and embedded image
LayoutDataResource layoutDataResource = new LayoutDataResource(basePath + "SimpleReportWithCoverPage.json");
DlexResource dlexResource = new DlexResource(basePath + "SimpleReportWithCoverPage.dlex");
pdf.addDlex(dlexResource, layoutDataResource);
pdf.addAdditionalResource("c:/temp/users-guide-resources/NorthwindLogo.gif");
// create pdf using cloud storage dlex and binary JSON data
String jsonData = null;
try {
jsonData = Files.readString(Paths.get(basePath + "SimpleReportWithCoverPage.json"));
} catch (IOException e1) {
e1.printStackTrace();
}
LayoutDataResource layoutData2 = new LayoutDataResource(jsonData);
pdf.addDlex("samples/users-guide-resources/SimpleReportWithCoverPage.dlex", layoutData2);
return pdf;
The following example illustrates using a serialized object as JSON data.
DynamicPDF API uses the Jackson ObjectMapper
class to serialize Java objects into JSON and deserialize JSON string into Java objects.
String jsonText = null;
SimpleReport simpleReport = null;
Pdf pdf = new Pdf();
try {
jsonText = Files.readString(Paths.get(basePath + "/SimpleReportWithCoverPage.json"));
// ObjectMapper instantiation
ObjectMapper objectMapper = new ObjectMapper();
// Deserialization into the `SimpleReport` class
simpleReport = objectMapper.readValue(jsonText, SimpleReport.class);
com.fasterxml.jackson.databind.ObjectMapper basicMapper = new ObjectMapper();
jsonText = basicMapper.writeValueAsString(simpleReport);
try {
jsonText = Files.readString(Paths.get(basePath + "SimpleReportWithCoverPage.json"));
} catch (IOException e1) {
e1.printStackTrace();
}
LayoutDataResource layoutData = new LayoutDataResource(jsonText);
pdf.addDlex("samples/users-guide-resources/SimpleReportWithCoverPage.dlex", layoutData);
} catch (Exception e1) {
e1.printStackTrace();
}
return pdf;
The DlexInput
class allows you to use a DLEX combined with the DLEX file's layout data to create a PDF.
Constructors
The DlexInput
class has the following constructor and initializes a new instance by posting the DLEX file and the JSON data file from the client to the API to create the PDF report. The $dlex
parameter is the DLEX file path present in the resource manager or the DlexResource
file created as per the desired PDF report layout design. The $layout
parameter is the JSON data file path present in the resource manager used to create the PDF report or the LayoutDataResource
file used to create the PDF report.
class DlexInput extends Input
{
/**
*
* Initializes a new instance of the DlexInput class by posting the DLEX file and the JSON data file from
* the client to the API to create the PDF report.
*
* @param string|DlexResource $dlex The DLEX file path present in the resource manager or the DlexResource file created as per the desired PDF report layout design.
* @param string|LayoutDataResource $layout The JSON data file path present in the resource manager used to create the PDF report or the LayoutDataResource file used to create the PDF report.
*/
public function __construct($dlex, $layout)
You do not call the DlexInput
class directly, but rather through the Pdf
class's AddDlex
method. See the example below.
LayoutDataResource
The LayoutDataResource
class initializes a new instance using the layout data object and a resource. The $layout
parameter is a serializable object data to create PDF report or the layout data JSON file path. The $layoutDataResourceName
is the name for layout data resource.
Constructors
class LayoutDataResource extends Resource
{
/**
*
* Initializes a new instance of the LayoutDataResource class using the layout data object and a resource
* name.
*
* @param array|string $layout Data to create PDF report as an array of JSON content or as a string of JSON file path.
* @param string $layoutDataResourceName The name for layout data resource.
*/
public function __construct($layout = null, string $layoutDataResourceName = null)
DlexResource
The DlexResource
class initializes a new instance with the DLEX file path and resource name as parameters. The $dlex
parameter is the DLEX file path, the byte array of the DLEX file, or a stream containing the DLEX file. The optional $resourceName
is the name of the resource.
Constructors
class DlexResource extends Resource
{
/**
*
* Initializes a new instance of the DlexResource class with DLEX file path and resource name as parameters.
*
* @param string|array|stream $dlex The dlex file path or the byte array of the dlex file or the stream of the dlex file.
* @param string $resourceName The name of the resource.
*/
public function __construct($dlex, string $resourceName = null)
Examples
The following examples illustrates first creating a PDF from a DLEX and its layout data and then creating a PDF from a stream.
//get layoutdata from local filesystem
$layoutData = new LayoutDataResource(CreatingPdfDlexLayout::$BasePath . "creating-pdf-dlex-layout.json");
//load dlex from cloud and layoutdata from file
$dlexEndpoint = new DlexLayout("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", $layoutData);
$dlexEndpoint->ApiKey = "DP.xxx-api-key-xxx";
$response = $dlexEndpoint->Process();
$file_content = file(CreatingPdfDlexString::$BasePath . "SimpleReportWithCoverPage.json");
$layoutData = new LayoutDataResource($file_content);
$pdf = new Pdf();
$pdf->ApiKey ="DP.xxx-api-key-xxx";
//load dlex from cloud and layoutdata from object
$pdf->AddDlex("samples/dlex-layout/SimpleReportWithCoverPage.dlex", $layoutData);
$response = $pdf->Process();
The following methods initializes a new instance of a DlexInput
( Dlex.go). Note that the DlexInput
is a struct where you only call the methods. It has methods to load a DLEX file as a local resource or from the cloud. Call one of these methods to create the resultant PDF.
The dlexResource
is either a path to the DLEX file in DynamicPDF CloudAPI cloud storage or the resource on your local system. The layoutData
is the layout data (The JSON data) in DynamicPDF CloudAPI cloud storage or on your local system.
func NewDlexWithDlexNLayoutResources(dlexResource resource.DlexResource, layoutData resource.LayoutDataResource) *Dlex
func NewDlexWithDlexResourceNLayoutDataPath(dlexResource resource.DlexResource, layoutData string) *Dlex
func NewDlexWithCloudResourceNLayoutData(cloudResourcePath string, layoutData resource.LayoutDataResource) *Dlex
func NewDlexWithCloudResourceNLayoutDataPath(cloudResourcePath string, layoutData string) *Dlex
Example
In the following example the PDF is created using a local path to the DLEX file and a local path to the layout data's JSON document.
pdfD := endpoint.NewPdf()
pdfD.AddDlexWithDlexResourceNLayoutDataPath("C:/temp/dynamicpdf-api-samples/getting-started.dlex", "C:/temp/dynamicpdf-api-samples/getting-started.json")
pdfD.Endpoint.BaseUrl = baseUrl
pdfD.Endpoint.ApiKey = key
return pdfD
The DlexInput
class allows you to use a DLEX combined with the DLEX file's layout data to create a PDF.
Constructors
The DlexInput
class has the following constructor and initializes a new instance by posting the DLEX file and the JSON data file from the client to the API to create the PDF report. The resource
parameter is the DLEX file path present in the resource manager or the DlexResource
file created as per the desired PDF report layout design. The layout
parameter is the JSON data file path present in the resource manager used to create the PDF report or the LayoutDataResource
file used to create the PDF report.
class DlexInput(Input):
'''
Represents a Dlex input
'''
def __init__(self, resource, layout_data):
You do not call the DlexInput
class directly, but rather through the Pdf
class's add_dlex
method. See the example below.
LayoutDataResource
The LayoutDataResource
class initializes a new instance using the layout data object and a resource. The $layout
parameter is a serializable object data to create PDF report or the layout data JSON file path. The $layoutDataResourceName
is the name for layout data resource.
Constructors
class LayoutDataResource(Resource):
'''
Represents the Layout data resource used to create PDF reports
'''
def __init__(self, layout_data, layout_data_resource_name = None):
DlexResource
The DlexResource
class initializes a new instance with the DLEX file path and resource name as parameters. The $dlex
parameter is the DLEX file path, the byte array of the DLEX file, or a stream containing the DLEX file. The optional $resourceName
is the name of the resource.
Constructors
class DlexResource(Resource):
'''
Represents a Dlex resource object that is created using the DLEX file and a name.
'''
def __init__(self, dlex, resource = None):
Examples
The following example illustrates creating a PDF from a DLEX and its layout data as a file path.
def ug_dlex_pdf_example(documentPath):
pdf = Pdf()
layout = LayoutDataResource(documentPath + "SimpleReportWithCoverPage.json")
pdf.add_dlex("samples/users-guide-resources/SimpleReportWithCoverPage.dlex", layout)
return pdf
This next example illustrates creating a PDF from a DLEX and its layout data as an object. Although the example uses a JSON file, it loads the file into a string and then converts the JSON string into an object hierarchy.
def ug_dlex_string_pdf_string_example(documentPath):
pdf = Pdf()
with open(documentPath + "SimpleReportWithCoverPage.json","r") as f:
fileData = f.read()
f.close()
python_obj = json.loads(fileData)
layout = LayoutDataResource(python_obj)
pdf.add_dlex("samples/users-guide-resources/SimpleReportWithCoverPage.dlex", layout)
return pdf
The complete example is available from the InstructionsExample.py
file on GitHub (https://github.com/dynamicpdf-api/python-client-examples).
See DlexLayoutObjectExample.py
for an example of creating a Python object hierarchy as layout data
ImageInput
Use an ImageInput
to process and add an image to a PDF that is returned from the pdf
endpoint. The DynamicPDF API supports the following nine common image types.
File Type | Description |
---|---|
JPEG | JPEGs are handled natively through a highly efficient pass through process. |
PNG | PNGs are handled natively. If the PNG does not have an alpha channel (transparency), it uses a highly efficient pass through process. If the PNG does have an alpha channel it is parsed and reformatted for inclusion in a PDF document. |
BMP | BMPs are converted to a bitmap for inclusion in a PDF document. |
EMF | EMFs are converted to a bitmap for inclusion in a PDF document. |
WMF | WMFs are converted to a bitmap for inclusion in a PDF document. |
EXIF | EXIFs are handled natively through a highly efficient pass through process. |
JPEG 2000 | JPEG 2000s are handled natively through a highly efficient pass through process. |
GIF | GIFs are handled natively, but are parsed and reformatted for inclusion in a PDF document. |
TIFF | TIFFs are handled natively and most types of TIFFs (LZW being the exception) use a highly efficient pass through process. Multi-page Tiffs are supported, allowing access to any page in a TIFF document. |
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
The ImageInput
class has the following two constructors. Note that when using the constructor that takes a string, it expects the relative path in your DynamicPDF Cloud Storage and not a local path. Use the ImageResource
class for local images.
Constructors
public ImageInput(ImageResource resource);
public ImageInput(string cloudResourcePath);
Properties
The ImageInput
class provides the following properties for customizing the outputted PDF.
public float? ScaleX { get; set; }
public float? ScaleY { get; set; }
public float? TopMargin { get; set; }
public float? LeftMargin { get; set; }
public float? BottomMargin { get; set; }
public float? RightMargin { get; set; }
public float? PageWidth { get; set; }
public float? PageHeight { get; set; }
public bool? ExpandToFit { get; set; }
public bool? ShrinkToFit { get; set; }
public Align Align { get; set; }
public VAlign VAlign { get; set; }
public int? StartPage { get; set; }
public int? PageCount { get; set; }
ImageResource
Use the ImageResource
class when working with local resources. This class has constructors that accept a file path, a byte array, or a Stream
.
public ImageResource(string filePath, string resourceName = null);
public ImageResource(byte[] value, string resourceName = null);
public ImageResource(Stream data, string resourceName = null);
Example
The following example illustrates using the image
input type to get an image as a file, as binary, and then from DynamicPDF Cloud Storage.
Pdf pdf = new Pdf();
//get image from local system
ImageResource ir = new ImageResource("C:/temp/users-guide-resources/A.png");
pdf.AddImage(ir);
//get Image as binary from local system
ImageResource ir2 = new ImageResource(File.ReadAllBytes("C:/temp/users-guide-resources/B.png"));
pdf.AddImage(ir2);
//get image from cloud storage
pdf.AddImage("samples/users-guide-resources/C.png");
return pdf;
The ImageInput
class has one constructor. The resource parameter is either an ImageResource
instance or a path present in cloud resource manager.
Constructors
export class ImageInput extends Input {
/**
* Initializes a new instance of the `ImageInput` class.
* @param {ImageResource | string} resource object to create ImageInput. | The image file path present in cloud resource manager.
*/
constructor(resource) {
Properties
The ImageInput
converts an image into a PDF and provides the following properties for customizing its output.
scaleX;
scaleY;
expandToFit;
shrinkToFit;
align = align.center;
vAlign = vAlign.center;
startPage;
pageCount;
ImageResource
The ImageResource
class initializes a new instance from the image's file path or a byte array of the image file. It also has an optional string to specify the resource name.
Constructors
export class ImageResource extends Resource {
/**
* Initializes a new instance of the `ImageResource` class.
* @param {string | Buffer[]} filePath The image file path. | The byte array of the image file.
* @param {string} resourceName The name of the resource.
*/
constructor(image, resourceName) {
The ImageInput
class converts an image into a PDF. The class has the following two constructors. Use the constructor that takes a string as the cloudResourcePath
for images in your DynamicPDF Cloud Storage. Use the ImageResource
class for images on your local system.
Constructors
public ImageInput(ImageResource resource)
public ImageInput(String cloudResourcePath)
Properties
The ImageInput
converts an image into a PDF and provides the following properties for customizing its output.
private float scaleX;
private float scaleY;
private float topMargin;
private float leftMargin;
private float bottomMargin;
private float rightMargin;
private float pageWidth;
private float pageHeight;
private boolean expandToFit;
private boolean shrinkToFit;
private Align align = Align.CENTER;
private VAlign vAlign = VAlign.CENTER;
private int startPage;
private int pageCount;
ImageResource
Use the ImageResource
class when working with local resources. This class has a constructors that accept a file path, a byte array, or a InputStream
.
Constructors
public ImageResource(String filePath, String resourceName)
public ImageResource(String filePath)
public ImageResource(byte[] value, String resourceName)
public ImageResource(byte[] value)
public ImageResource(InputStream data, String resourceName)
public ImageResource(InputStream data)
Properties
ResourceType resourceType = ResourceType.IMAGE;
String mimeType;
Example
The following example illustrates creating a PDF from three images where one image is a local file, another image is binary, and an image is from cloud storage.
Pdf pdf = new Pdf();
//get image from local system
ImageResource ir = new ImageResource(basePath + "A.png");
pdf.addImage(ir);
//get Image as binary from local system
ImageResource ir2 = null;
try {
ir2 = new ImageResource(Files.readAllBytes(Paths.get(basePath + "B.png")));
} catch (IOException e) {
e.printStackTrace();
}
pdf.addImage(ir2);
//get image from cloud storage
pdf.addImage("samples/users-guide-resources/C.png");
return pdf;
The ImageInput
class initializes a new instance where the $resource
property is either the image file relative path relative in cloud storage or an ImageResource
object.
Constructors
class ImageInput extends Input
{
/**
*
* Initializes a new instance of the ImageInput class.
*
* @param string|ImageResource $resource The image file path present in cloud resource manager or the ImageResource object to create ImageInput.
*/
public function __construct($resource)
Properties
The ImageInput
class has the following properties for customizing the PDF created from the image.
public $ScaleX;
public $ScaleY;
public $TopMargin;
public $LeftMargin;
public $BottomMargin;
public $RightMargin;
public $PageWidth;
public $PageHeight;
public $ExpandToFit;
public $ShrinkToFit;
public $Align = Align::Center;
public $VAlign = VAlign::Center;
public $StartPage;
public $PageCount;
ImageResource
Use the ImageResource
class to create a new class instance where the $file
parameter is either the image file path, the byte array of the image file, or the stream of the image file. The $resourceName
is an optional resource name.
Constructors
class ImageResource extends Resource
{
/**
*
* Initializes a new instance of the ImageResource class.
*
* @param string|array|stream $filePath The image file path or the byte array of the image file or the stream of the image file.
* @param string $resourceName The name of the resource.
*/
public function __construct($file, string $resourceName = null)
The following methods implement an ImageInput
instance (image.go
). The NewImageWithResourcePath
uses the image file path present in your DynamicPDF Cloud Storage. The NewImageWithImageResource
method uses the resource to create an instance of the Image
class.
func NewImageWithResourcePath(value string) *Image
func NewImagewithImageResource(value resource.ImageResource) *Image
Properties
The Image
structure has the following properties available to format the image converted to PDF.
ScaleX int
ScaleY int `json:"scaleY"`
ExpandToFit float32 `json:"expandToFit"`
ShrinkToFit float32 `json:"shrinkToFit"`
Align position.Align `json:"align"`
VAlign position.VAlign `json:"vAlign"`
Example
In the following example the endpoint fetches an image from your local system and another image from cloud storage. It then merges these two images to create a two page PDF document.
prImage := endpoint.NewPdf()
imageResource := resource.NewImageResourceWithResourcePath("C:/temp/getting-started.png", "getting-started.png")
prImage.AddImage(imageResource)
prImage.AddImageCloudPath("samples/image-info/getting-started.png")
prImage.Endpoint.ApiKey = key
prImage.Endpoint.BaseUrl = baseUrl
return prImage
The ImageInput
class initializes a new instance where the resource
property is either the image file relative path relative in cloud storage or an ImageResource
object.
Constructors
class ImageInput(Input):
'''
Represents an image input
'''
def __init__(self, resource):
Properties
The ImageInput
class has the following properties for customizing the PDF created from the image.
# Gets or sets the scaleX of the image.
self.scale_x = None
# Gets or sets the scaleY of the image.
self.scale_y = None
# Gets or sets the top margin.
self.top_margin = None
# Gets or sets the left margin.
self.left_margin = None
# Gets or sets the bottom margin.
self.bottom_margin = None
# Gets or sets the right margin.
self.right_margin = None
# Gets or sets the page width.
self.page_width = None
# Gets or sets the page height.
self.page_height = None
# Gets or sets a boolean indicating whether to expand the image.
self.expand_to_fit = None
# Gets or sets a boolean indicating whether to shrink the image.
self.shrink_to_fit = None
# Gets or sets the horizontal alignment of the image.
self.align = Align.Center
# Gets or sets the vertical alignment of the image.
self.v_align = VAlign.Center
# Gets or sets the start page.
self.start_page = None
# Gets or sets the page count.
self.page_count = None
ImageResource
Use the ImageResource
class to create a new class instance where the image
parameter is either the image file path or the byte array of the image file. The resourceName
is an optional resource name.
Constructors
class ImageResource(Resource):
'''
Represents an image resource used to create an ImageInput
object to create PDF from images
'''
def __init__(self, image, resource_name = None):
Example
The following example illustrates three different ways to process an image. The code first reads an image as an io.BytesIO
stream. Then it uses the file directly from cloud storage. Finally, it gets the file from the local file system.
def ug_image_example(documentPath):
pdf = Pdf()
#read image as binary stream
with io.open(documentPath + "A.png", 'rb') as f:
imageBinary = f.read()
imageStream = io.BytesIO(imageBinary)
f.close()
imageResource = ImageResource(imageStream)
pdf.add_image(imageResource)
#get image from cloud storage
pdf.add_image("samples/users-guide-resources/B.png")
#get image from local file
imageFile = documentPath + "C.png"
imageResource2 = ImageResource(imageFile)
pdf.add_image(imageResource2)
return pdf
HtmlInput
Use an HtmlInput
class to process and add HTML to a PDF that is returned from the pdf
endpoint.
DynamicPDF API fully supports HTML 5 and CSS but does not support JavaScript.
An embedded base path must be a resource publicly accessible via HTTP.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
The HtmlInput
class has one constructor that takes an HtmlResource
, a basepath
(if it exists), PageSize
, PageOrientation
, and margins
as a float.
Constructor
public HtmlInput(HtmlResource resource, string basepath = null, PageSize size = PageSize.Letter, PageOrientation orientation = PageOrientation.Portrait, float? margins = null);
The HtmlInput
class only accepts local resources and only accepts raw HTML strings.
Properties
The HtmlInput
class has the following properties for customizing the format of the created PDF.
public string BasePath { get; set; }
public float? TopMargin { get; set; }
public float? LeftMargin { get; set; }
public float? BottomMargin { get; set; }
public float? RightMargin { get; set; }
public float PageWidth { get; set; }
public float PageHeight { get; set; }
public PageSize PageSize { get; set; }
public PageOrientation PageOrientation { get; set; }
HtmlResource
The HtmlResource
class constructor only accepts raw HTML strings
Constructors
public HtmlResource(string html, string resourceName = null)
Example
The following example illustrates using an HTML file to create a PDF.
HtmlResource resource = new HtmlResource(System.IO.File.ReadAllText(basePath + "products.html"));
pdf.AddHtml(resource);
The HtmlInput
class has one constructor that takes an HtmlResource
, a basepath
(if it exists), PageSize
, PageOrientation
, and margins
as a float.
Constructor
export class HtmlInput extends Input {
htmlString;
#smaller;
#larger;
#basePath;
#pageHeight;
#pageWidth;
#pageSize;
#pageOrientation;
/**
* Initializes a new instance of the `HTMLInput` class.
* @param {HtmlResource} resource which represents the html code.
* @param {string} basePath for the html resource. This is the root path for any relative path used in html.
* @param {PageSize} pageSize of the PDF pages
* @param {Orientation} orientation for the PDF pages
* @param {number} margins for all four sides
*/
constructor(resource, basePath = null, pageSize = PageSize.Letter, orientation = Orientation.portrait, margins) {
The HtmlInput
class only accepts local resources and only accepts raw HTML strings.
HtmlResource
The HtmlResource
class constructor only accepts raw HTML strings.
Constructor
export class HtmlResource extends Resource {
/**
* Initializes a new instance of the `HtmlResource` class
* with html string and resource name.
* @param {string} html The Html string.
* @param {string} resource The name of the resource.
*/
constructor(htmlString, resourceName = null) {
The following example illustrates creating a PDF from HTML.
static async HtmlExample(basePath) {
var pdf = new Pdf();
pdf.addHtml("<html>An example HTML fragment.</html>");
pdf.addHtml("<html><p>HTML with basePath.</p><img src='./images/logo.png'></img></html>",
"https://www.dynamicpdf.com", PageSize.LETTER, Orientation.PORTRAIT,1);
var resourcePath = basePath + "/products.html";
pdf.addHtml(new HtmlResource(resourcePath), null, PageSize.LETTER, Orientation.PORTRAIT, 1);
await this.ProcessAndSave(pdf, "html-output.pdf");
}
The HtmlInput
class has five overloaded methods. However, the methods all take an HtmlResource
. All other parameters are optional.
Constructor
public HtmlInput(HtmlResource resource, String basepath, PageSize size, PageOrientation orientation, Float margins)
public HtmlInput(HtmlResource resource, String basepath, PageSize size, PageOrientation orientation)
public HtmlInput(HtmlResource resource, String basepath, PageSize size)
public HtmlInput(HtmlResource resource, String basepath)
public HtmlInput(HtmlResource resource)
HtmlResource
The HtmlResource
class has two constructors, both have an html
string parameter that is the raw HTML and one allows specifying a resource name.
Constructor
public HtmlResource(String html, String resourceName)
public HtmlResource(String html)
Example
The following example illustrates using an HTML file to create a PDF.
Pdf pdf = new Pdf();
pdf.AddHtml("<html>An example HTML fragment.</html>", null, PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
// use basepath in an HTML string, note the image is accessible via WWW. Note the embedded CSS.
pdf.addHtml("<html><p style='color:red;font-family:verdana;font-size:30px'>HTML with basePath.</p><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com", PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
// add html from a path on local drive
String temp = null;
try {
temp = Files.readString(Paths.get(basePath + "products.html"));
} catch (IOException e) {
e.printStackTrace();
}
HtmlResource resource = new HtmlResource(temp);
pdf.AddHtml(resource, null, PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
return pdf;
The HtmlInput
class has one constructor that takes an HtmlResource
, a basepath
(if it exists), PageSize
, PageOrientation
, and margins
as a float.
Constructor
class HtmlInput extends Input
{
/**
*
* Initializes a new instance of the ImageInput class.
*
* @param string|HtmlResource $resource The Html string or the HtmlResource object to create HtmlInput.
* @param string $basePath The basepath option for the url.
* @param string $pageSize The page size of the output PDF.
* @param string $orientation The page orientation of the output PDF.
* @param float $margin The page margins of the output PDF.
*/
public function __construct($resource, string $basePath = null, $pageSize = PageSize::Letter, $orientation = PageOrientation::Portrait, $margin = null)
{
The HtmlInput
class only accepts local resources and only accepts raw HTML strings.
The HtmlInput
class has the following properties for customizing the produced PDF.
Properties
public $_ResourceName
public $HtmlString;
public $BasePath;
public $PageWidth;
public $PageHeight;
public $TopMargin;
public $BottomMargin;
public $RightMargin;
public $LeftMargin;
HtmlResource
The HtmlResource
class constructor only accepts raw HTML strings
Constructor
class HtmlResource extends Resource
{
/**
*
* Initializes a new instance of the HtmlResource class.
*
* @param string $filePath The image file path or the byte array of the image file or the stream of the image file.
* @param string $resourceName The name of the resource.
*/
public function __construct($file, string $resourceName = null)
{
The following example illustrates creating a PDF from HTML.
$pdf = new Pdf();
$pdf->AddHtml("<html><p>This is a test.</p></html>");
$resource = new HtmlResource($basePath . "HtmlWithAllTags.html");
$pdf->AddHtml($resource);
$pdf->AddHtml("<html><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com");
return $pdf;
The HtmlInput
(html.go
) has two methods for inputting HTML. The NewHtmlInputWithString
method is for adding raw HTML while the NewHtmlInputWithResource
method is for adding an HtmlResource.
func NewHtmlInputWithString(inputString string) *Html
func NewHtmlInputWithResource(resource resource.HtmlResource) *Html
The HTML input is represented by an Html
struct.
type Html struct {
Input
smaller float32
larger float32
}
The Html
struct has an Input
struct. The Input struct contains the properties for fomatting the resultant PDF.
type Input struct {
InputCollector `json:"-"`
id string
ResourceName string `json:"resourceName,omitempty"`
templateId element.Template
template_ID string
resources []resource.Resource
inputType InputType
Elements []element.ElementCollector `json:"elements,omitempty"`
LayoutDataResourceName string `json:"layoutDataResourceName,omitempty"`
htmlString string
basePath string
topMargin float32
leftMargin float32
bottomMargin float32
rightMargin float32
pageWidth float32
pageHeight float32
pageSize PageSize
pageOrientation Orientation
}
The HtmlInput
structure only accepts local resources and only accepts raw HTML strings.
HtmlResource
An HtmlResource
(html.go
) has two methods for inputting HTML from your local system.
func NewHtmlResource(resource string, resourceName string) HtmlResource
func (p HtmlResource) ResourceType() ResourceType
Example
In the following example an HTML is loaded from an HTML String fragment, and a file containing an HTML document.
pdfExample := endpoint.NewPdf()
pdfExample.Endpoint.BaseUrl = "https://api.dpdf.io/"
pdfExample.Endpoint.ApiKey = "DP.xxx-api-key-xxx"
inputOne := input.NewHtmlInputWithString("<html>An example HTML fragment.</html>")
pdfExample.Inputs = append(pdfExample.Inputs, inputOne)
resp := pdfExample.Process()
res := <-resp
if res.IsSuccessful() == true {
os.WriteFile("c:/temp/html-to-pdf/html-output-go.pdf", res.Content().Bytes(), os.ModeType)
} else {
fmt.Print(res.ErrorJson())
}
The HtmlInput
class has one constructor that takes an HtmlResource
, a basepath
(if it exists), PageSize
, PageOrientation
, and margins
.
Constructor
class HtmlInput(Input):
'''
Represents a html input
'''
def __init__(self, resource, base_path = None, size = PageSize.Letter, orientation = PageOrientation.Portrait, margins = None):
The HtmlInput
class only accepts local resources and only accepts raw HTML strings.
The HtmlInput
class has the following properties for customizing the produced PDF.
Properties
# Gets or sets the top margin.
self.top_margin = margins
# Gets or sets the bottom margin.
self.bottom_margin = margins
# Gets or sets the right margin.
self.right_margin = margins
# Gets or sets the left margin.
self.left_margin = margins
@property
def page_size(self):
@property
def page_orientation(self):
HtmlResource
The HtmlResource
class constructor only accepts raw HTML strings
Constructor
class HtmlResource(Resource):
'''
Represents a pdf resource
'''
def __init__(self, html_string, resource_name = None):
Example
The following example illustrates using creating PDF from an HTML string and then from a local file.
You can only use HTML strings and not file paths when working with the html
input type.
def ug_html_example(documentPath):
pdf = Pdf()
pdf.add_html("<html><p>Welcome to DynamicPDF API.</p></html>")
with open(documentPath + "products.html","rt") as f:
fileData = f.read()
f.close()
pdf.add_html(fileData)
pdf.add_html("<html><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com")
return pdf
Although an embedded CSS stylesheet must be accessible via HTTP, an easy workaround is to replace the <link> tag content with the stylesheet's content.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
tempHtml = File.ReadAllText(basePath + "example.html");
tempCss = File.ReadAllText(basePath + "example.css");
StringBuilder sb = new StringBuilder();
sb.Append(tempHtml.Substring(0, tempHtml.IndexOf("<link")));
sb.Append("<style>" + tempCss + "</style>");
tempHtml = tempHtml.Substring(tempHtml.IndexOf("<link"));
sb.Append(tempHtml.Substring(tempHtml.IndexOf("/>") + 2));
HtmlResource resource = new HtmlResource(sb.ToString());
pdf.AddHtml(resource, null, PageSize.Letter, PageOrientation.Portrait, 1F);
tempHtml = Files.readString(Paths.get(basePath + "example.html"));
tempCss = Files.readString(Paths.get(basePath + "example.css"));
StringBuilder sb = new StringBuilder();
sb.append(tempHtml.substring(0, tempHtml.indexOf("<link")));
sb.append("<style>" + tempCss + "</style>");
tempHtml = tempHtml.substring(tempHtml.indexOf("<link"), tempHtml.length());
sb.append(tempHtml.substring(tempHtml.indexOf("/>") + 2));
HtmlResource resource = new HtmlResource(sb.toString());
pdf.AddHtml(resource, null, PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
var tempHtml = fs.readFileSync(basePath + "example.html", 'utf8');
var tempCss = fs.readFileSync(basePath + "example.css", 'utf-8');
var sb = tempHtml.substring(0, tempHtml.indexOf("<link"));
sb = sb + "<style>" + tempCss + "</style>";
tempHtml = tempHtml.substring(tempHtml.indexOf("<link"));
sb = sb + tempHtml.substring(tempHtml.indexOf("/>") + 2);
var resource = new HtmlResource(sb);
pdf.addHtml(resource, null, PageSize.LETTER, Orientation.PORTRAIT,1);
$tempHtml = file_get_contents(PdfHtmlCssWorkAroundExample::$BasePath . "example.html");
$tempCss = file_get_contents(PdfHtmlCssWorkAroundExample::$BasePath . "example.css");
$sb = substr($tempHtml, 0, strpos($tempHtml,"<link"));
$sb = $sb . "<style>" . $tempCss . "</style>";
$tempHtml = substr($tempHtml, strpos($tempHtml, "<link"));
$sb = $sb . substr($tempHtml, strpos($tempHtml, "/>") + 2);
$resource = new HtmlResource($sb);
$pdf->AddHtml($resource, null, "Letter", "Portrait", 1);
TBD
def html_css_work_around(api_key, full_path, output_path):
pdf = Pdf()
pdf.api_key = api_key
with open(full_path + "example.html","r") as f:
tempHtml = f.read()
f.close()
with open(full_path + "/example.css","r") as g:
tempCss = g.read()
g.close()
sb1 = tempHtml[0:tempHtml.index("<link"):1];
sb2 = tempHtml[tempHtml.index("/>") + 2::];
sb = sb1 + "<style>" + tempCss + "</style>" + sb2;
print(sb);
resource = HtmlResource(sb);
pdf.add_html(resource, None, PageSize.Letter, PageOrientation.Portrait,1);
response = pdf.process()
if response.is_successful:
with open(output_path + "workaround-output.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_json)
if __name__ == "__main__":
html_css_work_around(api_key, users_guide_resource_path, output_path + "/users-guide-output/")
WordInput
Use an WordInput
class to process and add a Word document to a PDF that is returned from the pdf
endpoint.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
Constructor
The WordInput
class has one constructor that takes a WordResource
, PageSize
, PageOrientation
, and margins
.
public WordInput(WordResource resource, PageSize size = PageSize.Letter, PageOrientation orientation = PageOrientation.Portrait, float? margins = null);
Properties
The WordInput
class has the following properties.
//Gets or sets the top margin.
public float? TopMargin { get; set; }
//Gets or sets the left margin.
public float? LeftMargin { get; set; }
//Gets or sets the bottom margin.
public float? BottomMargin { get; set; }
//Gets or sets the right margin.
public float? RightMargin { get; set; }
//Gets or sets the page width.
public float PageWidth { get; set; }
//Gets or sets the page height.
public float PageHeight { get; set; }
//Gets or sets the page size.
[JsonIgnore]
public PageSize PageSize { get; set; }
//Gets or sets page orientation.
[JsonIgnore]
public PageOrientation PageOrientation { get; set; }
WordResource
The WordResource
class has three overloaded constructor allowing a file to be uploaded from a filepath, a byte array, or a stream.
Constructors
The WordResource
class has one constructor that takes a WordResource
, PageSize
, PageOrientation
, and margins
.
public WordResource(string filePath, string resourceName = null);
public WordResource(byte[] value, string resourceName);
public WordResource(Stream data, string resourceName);
The Word input type only supports Word documents on your local system.
Example
The following example illustrates creating a PDF from a Word document.
Pdf pdf = new Pdf();
WordResource wordResource = new WordResource(basePath + "Doc1.docx");
WordInput word = new WordInput(wordResource);
word.PageWidth = 300;
word.PageHeight = 200;
word.TopMargin = 10;
word.BottomMargin = 10;
word.RightMargin = 40;
word.LeftMargin = 40;
pdf.Inputs.Add(word);
return pdf;
Constructor
The WordInput
class has a constructor that takes a page size, page orientation, and margins.
/** Initializes a new instance of the "WordInput class by posting the //
* Word resource file and the page size , the page orientation and the page margins of the output PDF.
* @param {WordResource} WordResource
* @param {PageSize} PageSize
* @param {PageOrientation} PageOrientation
* @param {number} Pagemargin
*/
constructor(resource, size = PageSize.Letter, orientation = Orientation.Portrait, margins = null)
Properties
The WordInput
class has the following properties.
#pageSize;
#pageOrientation;
#larger;
#smaller
#pageWidth;
#pageHeight;
WordResource
The WordResource
class allows obtaining a Word document from a string representing the file's path or as binary.
Constructor
/**
* Initializes a new instance of the `wordResource` class
* with word file path and resource name or
* byte data of the word file and resource name as parameters.
* @param {string | Buffer[]} word The word file path. | The Buffer array of the word file.
* @param {string} resource The name of the resource.
*/
constructor(word, resource = null)
Example
The follow example illustrates creating a PDF document from a Word document.
var pdf = new Pdf();
var resource = new WordResource( basePath + "Doc1.docx", "Doc1.docx");
var word =new WordInput(resource);
word.LeftMargin=100;
word.TopMargin =100;
word.PageSize=PageSize.A3;
pdf.inputs.push(word);
Constructor
The WordInput
class has four constructors that takes a WordResource
, PageSize
, PageOrientation
, and margins
.
public WordInput(WordResource resource, PageSize size, PageOrientation orientation, Float margins)
public WordInput(WordResource resource, PageSize size, PageOrientation orientation)
public WordInput(WordResource resource, PageSize size)
public WordInput(WordResource resource)
Properties
The WordInput
class has the following properties.
private PageSize pageSize = PageSize.LETTER;
private PageOrientation pageOrientation = PageOrientation.PORTRAIT;
private float pageWidth;
private float pageHeight;
private Float topMargin = null;
private Float bottomMargin = null;
private Float rightMargin = null;
private Float leftMargin = null;
private List<TextReplace> textReplace;