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;
WordResource
The WordResource
class has four overloaded constructor allowing a file to be uploaded from a filepath, a byte array, or an input stream.
Constructor
public WordResource(String filePath)
public WordResource(String filePath, String resourceName)
public WordResource(byte[] value, String resourceName)
public WordResource(InputStream data, String resourceName)
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.setPageWidth(300);
word.setPageHeight(200);
word.setTopMargin(10F);
word.setBottomMargin(10F);
word.setRightMargin(40F);
word.setLeftMargin(40F);
pdf.getInputs().add(word);
return pdf;
Constructor
The WordInput
class constructor takes a WordResource
, PageSize
, PageOrientation
, and margins
.
public function __construct(WordResource $resource, $size = PageSize::Letter, $orientation = PageOrientation::Portrait, float $margins = null)
Properties
The WordInput
class has the following properties.
public $TopMargin;
public $LeftMargin;
public $BottomMargin;
public $RightMargin;
public $PageWidth;
public $PageHeight;
WordResource
The WordResource
class takes a Word document from a file path or binary.
Constructor
public function __construct($file, string $resourceName = null)
Example
The following example illustrates converting a Word document to a PDF.
$pdf = new Pdf();
$pdf->ApiKey =$apikey;
$wordResource = new WordResource($path . "Doc1.docx");
$wordInput = new WordInput($wordResource);
array_push($pdf->Inputs, $wordInput);
$pdfResponse = $pdf->Process();
The following functions create a new word input from a Word document.
func NewWordInputWithResource(resources resource.WordResource) *Word
func (p *Word) Resources() []resource.Resource
func (p *Word) InputType() InputType
WordResource
The following four functions create a new WordResource
from a Word document.
func NewWordResourceWithResourcePath(resource string, resourceName string) WordResource
func NewWordResourceWithByteValue(resource []byte, resourceName string) WordResource
func (p WordResource) ResourceType() ResourceType
func (p *WordResource) fileExtension() string
Example
The following example illustrates converting a Word document to a PDF.
pdfExample := endpoint.NewPdf()
pdfExample.Endpoint.BaseUrl = "https://api.dpdf.io/"
pdfExample.Endpoint.ApiKey = "DP--api-key--"
wordResource := resource.NewWordResourceWithResourcePath("./resources/word-pdf/Doc1.docx", "Doc1.docx")
pdfInput := input.NewWordInputWithResource(wordResource)
pdfExample.Inputs = append(pdfExample.Inputs, pdfInput)
resp := pdfExample.Process()
The WordInput
class has one constructor that takes a WordResource
, PageSize
, PageOrientation
, and margins
.
Constructor
def __init__(self, resource, size = PageSize.Letter, orientation = PageOrientation.Portrait, margins = None):
'''
Initializes a new instance of the WordInput class.
Args:
resource (WordResource): The resource of type WordResource. size (PageSize): The page size of the output PDF.
orientation (PageOrientation): The page orientation of the output PDF.
margins (float): The page margins of the output PDF.
'''
Properties
The WordInput
class has the following properties.
self._page_size = size
self._page_orientation = orientation
self.top_margin = margins
self.bottom_margin = margins
self.right_margin = margins
self.left_margin = margins
self.page_width = None
self.page_height = None
WordResource
The WordResource
class has the following constructor.
Constructor
def __init__(self, variable, resource_name = None):
'''
Initializes a new instance of the WordResource class.
Args:
filePath (string | byte[]): The word file path. | The byte array of the word file.
resource_name (ResourceName): The resource name with file extension.
'''
Example
The follow example illustrates using the word
input type.
def ug_word_example(documentPath):
pdf=Pdf()
fileResource = documentPath
wordResource = WordResource(documentPath + "Doc1.docx")
word = WordInput(wordResource)
word.page_width = 300
word.page_height = 200
word.top_margin = 10
word.bottom_margin = 10
word.right_margin = 40
word.left_margin = 40
pdf.inputs.append(word)
return pdf
ExcelInput
Use an ExcelInput
class to process and add an Excel document to a PDF that is returned from the pdf
endpoint.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
Constructor
The ExcelInput
class has one constructor that takes a ExcelResource
, PageSize
, PageOrientation
, and margins
.
public ExcelInput(ExcelResource resource, PageSize size = PageSize.Letter, PageOrientation orientation = PageOrientation.Portrait, float? margins = null);
Properties
The ExcelInput
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; }
ExcelResource
The ExcelResource
class has three overloaded constructor allowing a file to be uploaded from a filepath, a byte array, or a stream.
Constructors
The ExcelResource
class has one constructor that takes a ExcelResource
, PageSize
, PageOrientation
, and margins
.
public ExcelResource(string filePath, string resourceName = null);
public ExcelResource(byte[] value, string resourceName);
public ExcelResource(Stream data, string resourceName);
The excel
input type only supports Excel documents on your local system.
Example
The following example illustrates creating a PDF from a Excel document.
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;
pdf.AddExcel(new ExcelResource(basePath + "/sample-data.xlsx"));
PdfResponse pdfResponse = pdf.Process();
Constructor
The ExcelInput
class has a constructor that takes a page size, page orientation, and margins.
TBD
Properties
The ExcelInput
class has the following properties.
TBD
ExcelResource
The ExcelResource
class allows obtaining a Word document from a string representing the file's path or as binary.
Constructor
TBD
Example
The follow example illustrates creating a PDF document from a Excel document.
Source: InstructionsExample.js Constructor
The ExcelInput
class has four constructors that takes a ExcelResource
, PageSize
, PageOrientation
, and margins
.
public ExcelInput(ExcelResource resource, PageSize size, PageOrientation orientation, Float margins)
public ExcelInput(ExcelResource resource, PageSize size, PageOrientation orientation)
public ExcelInput(ExcelResource resource, PageSize size)
public ExcelInput(ExcelResource resource)
Properties
The ExcelInput
class has the following properties (inherited from ConverterInput
.
private PageSize pageSize = PageSize.LETTER;
private PageOrientation pageOrientation = PageOrientation.PORTRAIT;
private Float topMargin = null;
private Float bottomMargin = null;
private Float rightMargin = null;
private Float leftMargin = null;
private float pageWidth;
private float pageHeight;
ExcelResource
The ExcelResource
class has four overloaded constructor allowing a file to be uploaded from a filepath, a byte array, or an input stream.
Constructor
public ExcelResource(String filePath)
public ExcelResource(String filePath, String resourceName)
public ExcelResource(byte[] value, String resourceName)
public ExcelResource(InputStream data, String resourceName)
Example
The following example illustrates creating a PDF from a Excel document.
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
ExcelResource excelResource = new ExcelResource(basePath + "sample-data.xlsx");
pdf.addExcel(excelResource, PageSize.LETTER, PageOrientation.PORTRAIT, (float)1.0);
PdfResponse pdfResponse = pdf.process();
Constructor
The ExcelInput
class constructor takes a ExcelResource
, PageSize
, PageOrientation
, and margins
.
public function __construct(ExcelResource $resource, $size = PageSize::Letter, $orientation = PageOrientation::Portrait, float $margins = null)
Properties
The ExcelInput
class has the following properties.
$this->TopMargin;
$this->BottomMargin;
$this->RightMargin;
$this->PageWidth;
$this->PageHeight;
$this->_TemplateId;
$this->ResourceName;
$this->Id;
ExcelResource
The ExcelResource
class takes a Excel document from a file path or binary.
Constructor
public function __construct($file, string $resourceName = null)
Example
The following example illustrates converting an Excel document to a PDF.
$pdf = new Pdf();
$pdf->ApiKey =$apikey;
$excelResource = new ExcelResource($path . "sample-data.xlsx");
$excelInput = new ExcelInput($excelResource);
array_push($pdf->Inputs, $excelInput);
$pdfResponse = $pdf->Process();
The following three functions create a new Excel input from an ExcelResource
.
func NewExcelInputWithResource(resources resource.ExcelResource) *Excel
func (p *Excel) Resources() []resource.Resource
func (p *Excel) InputType() InputType
ExcelResource
The following four functions create a new ExcelResource
from an excel document.
func NewExcelResourceWithResourcePath(resource string, resourceName string) ExcelResource
func NewExcelResourceWithByteValue(resource []byte, resourceName string) ExcelResource
func (p ExcelResource) ResourceType() ResourceType
func (p *ExcelResource) fileExtension() string
Example
func main() {
pdfExample := endpoint.NewPdf()
pdfExample.Endpoint.ApiKey = apiKey
excelResource := resource.NewExcelResourceWithResourcePath(basePath+"sample-data.xlsx", "sample-data.xlsx")
pdfInput := input.NewExcelInputWithResource(excelResource)
pdfExample.Inputs = append(pdfExample.Inputs, pdfInput)
resp := pdfExample.Process()
res := <-resp
if res.IsSuccessful() == true {
os.Remove(outputPath)
os.WriteFile(outputPath, res.Content().Bytes(), os.ModeType)
} else {
fmt.Print(res.ErrorJson())
}
}
The ExcelInput
class has one constructor that takes a ExcelResource
, PageSize
, PageOrientation
, and margins
.
Constructor
def __init__(self, resource, size = PageSize.Letter, orientation = PageOrientation.Portrait, margins = None):
Properties
The ExcelInput
class has the following properties.
"id":self.id,
"resourceName": self.resource_name,
"templateId": self._template_id,
"type": self._type,
"pageWidth": self.page_width,
"pageHeight": self.page_height,
ExcelResource
The ExcelResource
class has the following constructor.
Constructor
def __init__(self, variable, resource_name = None):
Example
The follow example illustrates using the excel
input type.
pdf=Pdf()
pdf.api_key=apikey
fileResource = documentPath
excelResource = ExcelResource(documentPath + "sample-data.xlsx")
excel = ExcelInput(excelResource)
pdf.inputs.append(excel)
response = pdf.process()
PageInput
Use a PageInput
to create, process, and add a PDF page to a PDF that is returned from the pdf
endpoint. Also use this input to create a form.
This input creates only single-page PDFs and is intended for cover-pages and other simple PDFs. See the Designer documentation for more information on using Designer to create PDF documents/reports.
When using this input you place one or more elements on the page to create the PDF. The Add Text, Images, Lines, and Rectangles to PDF's solution provides an illustrative example.
Refer to the CloudAPI Instructions documentation for examples illustrating fonts, Acroform elements, outlines, and adding other elements to a page to create PDFs using the page
input.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
When creating a PDF page from scratch, use the PageInput
class. But you can only create a single-page PDF using PageInput
. Use DynamicPDF Designer to create more complex, multi-page PDF documents.
Constructors
The PageInput
class has the following constructors.
public PageInput(float pageWidth, float pageHeight);
public PageInput(PageSize size = PageSize.Letter, PageOrientation orientation = PageOrientation.Portrait, float? margins = null);
If creating complex PDFs, it is easier to use Designer to create a template that creates a PDF rather than using the PageInput
class.
Properties
The PageInput
class has the following properties for customizing a page.
public float? PageWidth { get; set; }
public float? PageHeight { get; set; }
public float? TopMargin { get; set; }
public float? LeftMargin { get; set; }
public float? BottomMargin { get; set; }
public float? RightMargin { get; set; }
public PageSize PageSize { get; set; }
public PageOrientation PageOrientation { get; set; }
public List<Element> Elements { get; }
When creating a PDF page from scratch, use the PageInput
class. But you can only create a single-page PDF using the PageInput
. Use DynamicPDF Designer to create more complex, multi-page PDF documents.
Constructors
The PageInput
class has the following constructor.
export class PageInput extends Input {
#pageHeight;
#pageWidth;
#pageSize;
#pageOrientation;
/** Gets or sets the elements of the page. */
elements = [];
#smaller;
#larger;
/**
* Initializes a new instance of the `PageInput` class.
* @param {number} pageWidth The width of the page.
* @param {number} pageHeight The height of the page.
*/
constructor(pageWidth, pageHeight) {
If creating complex PDFs, it is easier to use Designer to create a template that creates a PDF rather than using the PageInput
class.
Properties
pageHeight;
pageWidth;
pageSize;
pageOrientation;
topMargin
bottomMargin
leftMargin
rightMargin
When creating a PDF page from scratch, use the PageInput
class. But you can only create a single-page PDF using the PageInput
. Use DynamicPDF Designer to create more complex, multi-page PDF documents.
Constructors
The PageInput
class has the following constructors.
public PageInput(PageSize size, PageOrientation orientation, Float margins)
public PageInput(PageSize size, PageOrientation orientation)
public PageInput(PageSize size)
public PageInput(float pageWidth, float pageHeight)
public PageInput()
If creating complex PDFs, it is easier to use Designer to create a template that creates a PDF rather than using the PageInput
class.
Properties
The PageInput
class has the following properties for customizing a page.
private Float pageWidth = null;
private Float pageHeight = null;
private Float topMargin = null;
private Float bottomMargin = null;
private Float rightMargin = null;
private Float leftMargin = null;
private PageSize pageSize = PageSize.LETTER;
private PageOrientation pageOrientation = PageOrientation.PORTRAIT;
private List<Element> elements = null;
private final float DefaultPageHeight = 792.0f;
private final float DefaultPagewidth = 612.0f;
Example
The following example illustrates creating a PDF from a page input.
Pdf pdf = new Pdf();
PageInput pageInput = pdf.addPage(1008, 612);
PageNumberingElement pageNumberingElement =
new PageNumberingElement("1", ElementPlacement.TOPRIGHT);
pageNumberingElement.setColor(RgbColor.getRed());
pageNumberingElement.setFont(Font.getCourier());
pageNumberingElement.setFontSize(42);
pageInput.getElements().add(pageNumberingElement);
TextElement textElement = new TextElement("Hello from DynamicPDF API", ElementPlacement.BOTTOMCENTER);
pageInput.getElements().add(textElement);
return pdf;
When creating a PDF page from scratch, use the PageInput
class. But you can only create a single-page PDF using the PageInput
. Use DynamicPDF Designer to create more complex, multi-page PDF documents.
Constructors
The PageInput
class has the following constructor.
class PageInput extends Input
{
/**
*
* Initializes a new instance of the PageInput class.
*
* @param float $pageWidth The width of the page.
* @param float $pageHeight The height of the page.
* @param float $margin The margins of the page.
*/
public function __construct(?float $pageWidth = null, ?float $pageHeight = null, ?float $margin = null)
If creating complex PDFs, it is easier to use Designer to create a template that creates a PDF rather than using the PageInput
class.
Properties
The PageInput
class has the following properties for customizing a page.
public $PageWidth;
public $PageHeight;
public $TopMargin;
public $BottomMargin;
public $RightMargin;
public $LeftMargin;
public $Elements = array();
When creating a PDF page from scratch, use the Page
struct. But you can only create a single-page PDF using Page
.
The Page
struct has the following method for creating page.
func NewPage() *Page
func NewPageWithDimension(width float32, height float32) *Page
Properties
The Page
struct has the following properties for customizing a page.
type Page struct {
Input
PageHeight float32 `json:"pageHeight"`
PageWidth float32 `json:"pageWidth"`
smaller float32
larger float32
}
The page
input only creates a single-page PDF, use DynamicPDF Designer and the dlex
input for more complex and multi-page PDFs.
Example
The following example illustrates using a pageInput
to create a PDF single-page document.
pdf := endpoint.NewPdf()
pdf.Endpoint.BaseUrl = "https://api.dpdf.io/"
pdf.Endpoint.ApiKey = "<api-key>"
pdf.Author = "Jane Doe"
pdf.Title = "Sample PDF"
pdf.Subject = "topLevel document metadata"
pdf.Creator = "John Creator"
pdf.Keywords = "dynamicpdf api pdf instructions"
pageInput := input.NewPage()
lineElement := element.NewLine(element.BottomCenter, 150, 200)
pageInput.Elements = append(pageInput.Elements, lineElement)
pageInput.PageHeight = 612
pageInput.PageWidth = 1008
pdf.Inputs = append(pdf.Inputs, pageInput)
resp := pdf.Process()
res := <-resp
if res.IsSuccessful() == false {
if res.ClientError() != nil {
fmt.Print("Error: " + res.ClientError().Error())
} else {
fmt.Print("Error: " + res.ErrorJson())
}
} else {
os.WriteFile("C:/temp/page-output.pdf", res.Content().Bytes(), os.ModeType)
}
When creating a PDF page from scratch, use the PageInput
class. But you can only create a single-page PDF using the PageInput
. Use DynamicPDF Designer to create more complex, multi-page PDF documents.
Constructors
The PageInput
class has the following constructor.
class PageInput(Input):
'''
Represents a page input.
'''
def __init__(self, size = PageSize.Letter, orientation = PageOrientation.Portrait, margins = None):
If creating complex PDFs, it is easier to use Designer to create a template that creates a PDF rather than using the PageInput
class.
Properties
The PageInput
class has the following properties for customizing a page.
# 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
# Gets or sets the width of the page.
self.page_width = None
# Gets or sets the height of the page.
self.page_height = None
@property
def page_orientation(self):
@property
def elements(self):
Example
The following example illustrates using the page input type.
You can only create a single page using the page input type. You could create multiple pages and append them; however, a better solution is to use Designer to create a DLEX report.
def ug_page_example():
pdf = Pdf()
pageInput = pdf.add_page(1008, 612)
textElement = TextElement("Hello from DynamicPDF API.", ElementPlacement.TopCenter)
pageNumberingElement = PageNumberingElement("A", ElementPlacement.TopRight)
pageNumberingElement.color = RgbColor.red()
pageNumberingElement.font = Font.courier()
pageNumberingElement.font_size = 42
pageInput.elements.append(pageNumberingElement)
pageInput.elements.append(textElement)
return pdf
Related Classes
Building a page from scratch provides most of the functionality of our flagship server/desktop product, DynamicPDF Core Suite. The client libraries provides numerous classes for creating your PDF using PageInput
.
Element Classes | ||
---|---|---|
AztecSymbolSize | LineElement | |
Compaction | MsiBarcodeCheckDigitMode | |
DataMatrixEncodingType | PageNumberingElement | |
DataMatrixFunctionalCharacter | RectangleElement | |
DataMatrixSymbolSize | TextElement | |
Element | ValueType | |
ElementPlacement | StackedGs1DataBarType | |
ElementType | QrCodeElement | |
ErrorCorrection | QrCodeEnc1 | |
Barcode Elements | ||
AztecBarcodeElement | Dim2BarcodeElement | |
Code11BarcodeElement | Gs1BarcodeElement | |
Code12BarcodeElement | lata25BarcodeElement | |
Code128BarcodeElement | MsiBarcodeElement | |
Code25BarcodeElement | Pdf47BarcodeElement | |
Code39BarcodeElement | TextBarcodeElement | |
Code93BarcodeElement | ||
DataMatrixBarcodeElement |
Refer to the CloudAPI Instructions documentation for examples illustrating using many of the above classes to create PDFs using the page
input.
PdfInput
Use a PdfInput
to create, process, and add a PDF to a PDF that is returned from the PDF endpoint. You usually use this input with one or more other inputs types to merge into a combined PDF.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
The PdfInput
has the following constructors for adding a PDF to the pdf
endpoint.
Constructors
public PdfInput(string resourceName);
public PdfInput(PdfResource resource, MergeOptions options = null);
public PdfInput(string cloudResourcePath, MergeOptions options = null);
- Load a PDF using the resource's name in cloud storage.
- Load a PDF from a
PdfResource
. - Load a PDF from a PDF in cloud storage's relative path.
Properties
The PdfInput
has the following three properties that allow customizing which pages to take from a PDF.
public MergeOptions MergeOptions { get; set; }
public int? StartPage { get; set; }
public int? PageCount { get; set; }
PdfResource
The PdfResource
allows obtaining a PDF from your local system rather than DynamicPDF Cloud Storage.
Constructors
The PdfResource
class has the following three constructors. The constructors allow using a file path, a byte array, or a Stream
.
public PdfResource(string filePath, string resourceName = null);
public PdfResource(byte[] value, string resourceName = null);
public PdfResource(Stream data, string resourceName = null);
Example
The following example illustrates three different ways to get a PDF and then merges them into a combined PDF.
Pdf pdf = new Pdf();
//get pdf from local file system
pdf.AddPdf(new PdfResource("c:/temp/users-guide-resources/DocumentA.pdf"));
// get pdf from bytes
PdfResource resource = new PdfResource(File.ReadAllBytes("c:/temp/users-guide-resources/DocumentB.pdf"));
pdf.AddPdf(resource);
//get pdf from cloud storage
pdf.AddPdf("samples/users-guide-resources/DocumentC.pdf");
return pdf;
The PdfInput
has the following constructors for adding a PDF to the pdf
endpoint.
Constructors
The PdfInput
class has the following constructor. The resource
parameter is either a string to the resource in cloud storage or a PdfResource
for local resources. It also includes an option mergeOptions
parameter for customizing how the PDF will be merged.
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) {
Properties
The PdfInput
has the following three properties that allow customizing which pages to take from a PDF.
mergeOptions;
pageCount;
startPage;
Example
The following example illustrates three different ways to load a PDF resource. First, it gets the PDF from local file system, then from stream, and finally from cloud storage.
var pdf = new Pdf();
var pdfInput = pdf.addPdf(new PdfResource(basePath + "DocumentA.pdf"));
var pdfResource = new PdfResource(fs.readFileSync(basePath + "DocumentB.pdf"));
pdf.addPdf(pdfResource);
pdf.addPdf("samples/users-guide-resources/DocumentC.pdf");
Use a PdfInput
to create, process, and add a PDF to a PDF that is returned from the PDF endpoint.
Constructors
The PdfInput
class has the following constructors.
public PdfInput(PdfResource resource, MergeOptions options)
public PdfInput(PdfResource resource)
public PdfInput(String cloudResourcePath, MergeOptions options)
public PdfInput(String cloudResourcePath)
- Load a PDF using the resource's name in cloud storage.
- Load a PDF from a
PdfResource
. - Load a PDF from a PDF in cloud storage's relative path.
To load a PDF on your local system, use the PdfResource
class.
Properties
The PdfInput
has the following three properties that allow customizing which pages to take from a PDF.
private MergeOptions mergeOptions = null;
private int startPage;
private int pageCount;
PdfResource
The PdfResource
allows obtaining a PDF from your local system rather than DynamicPDF Cloud Storage.
Constructors
The PdfResource
class has the following three constructors. The class has a constructor that allows using a file path, a byte array, or a InputStream
.
public PdfResource(String filePath, String resourceName)
public PdfResource(String filePath)
public PdfResource(byte[] value)
public PdfResource(InputStream data, String resourceName)
public PdfResource(InputStream data)
Example
The following example illustrates merging a PDF using three different PDFs. The first PDF is loaded from local file storage, the second is loaded as binary, while the third is from DynamicPDF cloud storage.
Pdf pdf = new Pdf();
//get pdf from local file system
pdf.addPdf(new PdfResource(basePath + "DocumentA.pdf"));
// get pdf from bytes
PdfResource resource = null;
try {
resource = new PdfResource(Files.readAllBytes(Paths.get(basePath + "DocumentB.pdf")));
} catch (IOException e) {
e.printStackTrace();
}
pdf.addPdf(resource);
//get pdf from cloud storage
pdf.addPdf("samples/users-guide-resources/DocumentC.pdf");
return pdf;
Constructors
The PdfInput
class has the following constructor. It initializes a new instance of the PdfInput
class and has a $resource
parameter that takes either a string path to the resource path in DynamicPdf Cloud Storage or the Pdfresource
on your local system. It also allows how a PDF is merged using the MergeOptions
class.
class PdfInput extends Input
{
/**
*
* Initializes a new instance of the PdfInput class.
*
* @param string|PdfResource $resource The resource path in cloud resource manager or the resource of type PdfResource.
* @param ?MergeOptions $options The merge options for the pdf.
*/
public function __construct($resource, ?MergeOptions $options = null)
Properties
The PdfInput
has the following three properties that allow customizing which pages to take from a PDF.
public $MergeOptions;
public $StartPage;
public $PageCount;
PdfResource
The PdfResource
allows obtaining a PDF from your local system rather than DynamicPDF Cloud Storage.
Constructors
The PdfResource
class has the following constructor. the $file
parameter takes a string to the file path, a byte array, or stream of the pdf file. It also has an optional $resourceName
to name the resource.
class PdfResource extends Resource
{
/**
*
* Initializes a new instance of the PdfResource class.
*
* @param string|array|stream $filePath The pdf file path or the byte array of the pdf file or the stream of the pdf file.
* @param ?string $resourceName The name of the resource.
*/
public function __construct($file, ?string $resourceName = null)
The PdfInput
(pdf.go) has methods for loading a PDF from your DynamicPDF Cloud Storage or from your local system.
func NewPdfWithCloudPath(cloudResourcePath string, option MergeOptions) *Pdf
func NewPdfWithResource(resource resource.PdfResource) *Pdf
func NewPdfWithResourceWithMergerOption(resource resource.PdfResource, option MergeOptions) *Pdf
Example
The following example merges two PDF documents into a new PDF. The first PDF is loaded from cloud storage while the second is loaded from your local system. The two PDFs are then merged into a combined PDF and returned to the calling client.
pr := endpoint.NewPdf()
mergeOption := input.NewMergeOptions()
prInput := input.NewPdfWithCloudPath("example/DocumentB.pdf", mergeOption)
pr.Inputs = append(pr.Inputs, prInput)
pr.Endpoint.BaseUrl = baseUrl
pr.Endpoint.ApiKey = key
pdfResource := resource.NewPdfResourceWithResourcePath("c:/temp/dynamicpdf-api-samples/DocumentA.pdf", "DocumentA.pdf")
prInput2 := input.NewPdfWithResource(pdfResource)
pr.Inputs = append(pr.Inputs, prInput2)
return pr
Constructors
The PdfInput
class has the following constructor. It initializes a new instance of the PdfInput
class and has a resource
parameter that takes either a string path to the resource path in DynamicPDF Cloud Storage or the Pdfresource
on your local system. It also allows how a PDF is merged using the MergeOptions
class.
class PdfInput(Input):
'''
Represents a pdf input
'''
def __init__(self, resource, options = None):
Properties
The PdfInput
has the following three properties that allow customizing which pages to take from a PDF.
# Gets or sets the merge options MergeOptions.
self.merge_options = options
# Gets or sets the start page.
self.start_page = None
# Gets or sets the page count.
self.page_count = None
PdfResource
The PdfResource
allows obtaining a PDF from your local system rather than DynamicPDF Cloud Storage.
Constructors
The PdfResource
class has the following constructor. the $file
parameter takes a string to the file path, a byte array, or stream of the pdf file. It also has an optional $resourceName
to name the resource.
class PdfResource(Resource):
'''
Represents a pdf resource
'''
def __init__(self, input, resource_name = None):
Example
The following example illustrates using the pdf
input type. The example first gets a PDF directly from a local file stream. It then gets a PDF from a binary stream, and finally, it gets a PDF from DynamicPDF Cloud Storage.
def ug_pdf_example(documentPath):
pdf=Pdf()
inputA = pdf.add_pdf(PdfResource(documentPath + "DocumentA.pdf"))
#read pdf as binary stream
with io.open(documentPath + "DocumentB.pdf", 'rb') as f:
pdfBinary = f.read()
pdfStream = io.BytesIO(pdfBinary)
f.close()
pdf.add_pdf(PdfResource(pdfStream))
pdf.add_pdf("samples/users-guide-resources/DocumentC.pdf")
return pdf