Convert Images to PDFs
Convert images to PDF using the pdf endpoint.
The pdf
endpoint makes converting images to PDF a breeze. Supported image types include the following nine common image types: JPEG, PNG, BMP, EMF, WMF, EXIF, JPEG2000, GIF, and TIFF.
The following examples illustrate converting a TIFF and a PNG image to a combined PDF.
You can also add an image to a PDF you create from scratch or add to an existing PDF.
Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.
See the image
input type documentation for more information.
Calling Endpoint Directly
The following JSON instructions document illustrates converting two different images to a combined PDF.
{
"author": "CeteSoftware",
"creator": "DynamicPDF Cloud Api",
"inputs": [
{
"type": "image",
"resourceName":"testimage.tif",
"pageWidth": 612,
"pageHeight":1008,
"expandToFit":false,
"align": "center",
"vAlign": "center"
},
{
"type": "image",
"resourceName":"dynamicpdfLogo.png",
"pageWidth": 1008,
"pageHeight":612,
"expandToFit":true,
"align": "center",
"vAlign": "center"
}
]
}
curl --location 'https://api.dpdf.io/v1.0/pdf'
--header 'Authorization: Bearer DP.BKMFqqItEDZb9iBeSAJvzZAgqhH6oyY9SBiKezfyw+rTRosKxx0oD9f9'
--form 'Instructions=@C:/temp/solutions/image-conversion/instructions.json'
--form 'Resource=@C:/temp/solutions/image-conversion/testimage.tif'
--form 'Resource=@C:/temp/solutions/image-conversion/dynamicpdfLogo.png'
Calling Endpoint Using Client Library
The following examples illustrate converting two images to a combined PDF using the six different client libraries.
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
public static void Run(string apiKey, string basePath, string outputPath)
{
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;
ImageResource imageResource = new ImageResource(basePath + "testimage.tif");
ImageInput imageInput = pdf.AddImage(imageResource);
imageInput.Align = Align.Center;
imageInput.VAlign = VAlign.Center;
imageInput.ExpandToFit = false;
imageInput.PageHeight = 1008;
imageInput.PageWidth = 612;
ImageResource imageResourceTwo = new ImageResource(basePath + "dynamicpdfLogo.png");
ImageInput imageInputTwo = pdf.AddImage(imageResourceTwo);
imageInputTwo.Align = Align.Center;
imageInputTwo.VAlign = VAlign.Center;
imageInputTwo.ExpandToFit = true;
imageInputTwo.PageHeight = 612;
imageInputTwo.PageWidth = 1008;
PdfResponse pdfResponse = pdf.Process();
if (pdfResponse.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/convert-img-to-pdf-output.pdf", pdfResponse.Content);
}
else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
static async Run() {
var pdf = new Pdf();
var imageResource = new ImageResource(Constants.BasePath + "image-conversion/testimage.tif")
var imageResource2 = new ImageResource(Constants.BasePath + "image-conversion/dynamicpdfLogo.png")
var imageInput = new ImageInput(imageResource);
var imageInput2 = new ImageInput(imageResource2);
imageInput.expandToFit = false;
imageInput.align = "center";
imageInput.vAlign = "center";
imageInput.pageHeight = 1008;
imageInput.pageWidth = 612;
imageInput2.expandToFit = true;
imageInput2.align = "center";
imageInput2.vAlign = "center";
imageInput2.pageHeight = 612;
imageInput2.pageWidth = 1008;
pdf.inputs.push(imageInput);
pdf.inputs.push(imageInput2);
pdf.apiKey = "DP.--api-key--";
var res = await pdf.process();
if (res.isSuccessful) {
var outFile = Constants.OutputPath + "image-conversion-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(res.errorJson);
}
}
public static void Run(String apiKey, String basePath)
{
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
ImageResource imageResource = new ImageResource(basePath + "testimage.tif");
ImageInput imageInput = pdf.addImage(imageResource);
imageInput.setAlign(Align.CENTER);
imageInput.setVAlign(VAlign.CENTER);
imageInput.setExpandToFit(false);
imageInput.setPageHeight(1008);
imageInput.setPageWidth(612);
ImageResource imageResource2 = new ImageResource(basePath + "dynamicpdfLogo.png");
ImageInput imageInput2 = pdf.addImage(imageResource2);
imageInput2.setAlign(Align.CENTER);
imageInput2.setVAlign(VAlign.CENTER);
imageInput2.setExpandToFit(false);
imageInput2.setPageHeight(612);
imageInput2.setPageWidth(1008);
imageInput2.setScaleX((float).5);
imageInput2.setScaleY((float).5);
pdf.setApiKey(apiKey);
PdfResponse response = pdf.process();
if(response.getIsSuccessful())
{
try {
FileUtils.writeByteArrayToFile(new File(DynamicPdfCloudApiExamples.OUTPUT_PATH + "/images-convert-pdf-output.pdf"),
response.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else
{
System.out.println(response.getErrorJson());
}
}
public static function Run(string $apikey, string $path, string $outputPath)
{
$pdf = new Pdf();
$pdf->ApiKey = $apikey;
$imageResource = new ImageResource($path . "testimage.tif");
$imageResource2 = new ImageResource($path . "dynamicpdfLogo.png" );
$imageInput = $pdf->AddImage($imageResource);
$imageInput2 = $pdf->AddImage($imageResource2);
$imageInput->VAlign = VAlign::Center;
$imageInput->Align = Align::Center;
$imageInput->PageHeight = 1008;
$imageInput->PageWidth = 612;
$imageInput->ExpandToFit = false;
$imageInput2->VAlign = VAlign::Center;
$imageInput2->Align = Align::Center;
$imageInput2->PageHeight = 612;
$imageInput2->PageWidth = 1008;
$imageInput2->ExpandToFit = true;
$imageInput->Align = Align::Center;
$imageInput2->VAlign = VAlign::Top;
$response = $pdf->Process();
if($response->IsSuccessful)
{
file_put_contents($outputPath . "image-conversion-output.pdf", $response->Content);
} else {
echo("Error: ");
echo($response->StatusCode);
echo($response->ErrorJson);
}
}
func main() {
pr := endpoint.NewPdf()
pr.Endpoint.ApiKey = apiKey
imageResource1 := resource.NewImageResourceWithResourcePath(basePath+"testimage.tif", "testimage.tif")
imageInput1 := input.NewImagewithImageResource(imageResource1)
imageInput1.Align = position.Center
imageInput1.ExpandToFit = false
imageInput1.SetPageHeight(1008)
imageInput1.SetPageWidth(612)
imageInput1.VAlign = position.Top
imageInput1.Align = position.Center
pr.Inputs = append(pr.Inputs, imageInput1)
imageResource2 := resource.NewImageResourceWithResourcePath(basePath+"dynamicpdfLogo.png", "dynamicpdfLogo.png")
imageInput2 := pr.AddImage(imageResource2)
imageInput2.ExpandToFit = false
imageInput2.SetPageWidth(1008)
imageInput2.SetPageHeight(612)
imageInput2.VAlign = position.Middle
imageInput2.Align = position.Center
imageInput2.ScaleX = .5
imageInput2.ScaleY = .5
resp := pr.Process()
res := <-resp
if res.IsSuccessful() == false {
if res.ClientError() != nil {
fmt.Print("Failed with error: " + res.ClientError().Error())
} else {
fmt.Print("Failed with error: " + res.ErrorJson())
}
} else {
os.WriteFile(outputPath,
res.Content().Bytes(), os.ModeType)
}
}
def image_conversion_example(apikey, full_path):
pdf=Pdf()
pdf.api_key = apikey
pdf.author = "John Doe"
pdf.title = "My Blank PDF Page"
imageResource = ImageResource(full_path + "testimage.tif")
imageResource2 = ImageResource(full_path + "dynamicpdfLogo.png")
imageInput = pdf.add_image(imageResource)
imageInput.expand_to_fit = False
imageInput.page_width = 612
imageInput.page_height = 1008
imageInput.v_align = VAlign.Center
imageInput.align = Align.Center
imageInput2 = pdf.add_image(imageResource2)
imageInput2 = ImageInput(imageResource)
imageInput2.expand_to_fit = True
imageInput2.page_width = 1008
imageInput2.page_height = 612
imageInput2.v_align = VAlign.Center
imageInput2.align = Align.Center
response = pdf.process()
if response.is_successful:
with open(output_path + "image-conversion.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
The pdf
endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf
endpoint.