Merge PDFs and Other Resources
Merge multiple PDF documents into one combined document.
The DynamicPDF API easily merges multiple PDFs into a combined PDF. You can also merge Word, HTML, and images into a combined PDF.
Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.
Refer to the other solutions on how to convert HTML, images, and Word documents to PDFs. Each type is an input, and multiple inputs are combined together, in the order they occur in the inputs
array to form the combined PDF.
Calling Endpoint Directly
The following JSON instructions document illustrates combining two PDF documents, a Word document, an image, an HTML document, and a DLEX report into a combined PDF.
- Create an instructions JSON document and add an
inputs
array. - Add the inputs to the array in the order in which they are to be merged.
{
"inputs": [
{
"type": "pdf",
"resourceName": "DocumentA.pdf"
},
{
"type": "pdf",
"resourceName": "DocumentB.pdf"
},
{
"type": "word",
"resourceName": "Doc1.docx"
},
{
"type": "image",
"scaleX": 0.5,
"scaleY": 0.5,
"resourceName": "testimage.tif"
},
{
"type": "dlex",
"layoutDataResourceName": "creating-pdf-dlex-layout.json",
"resourceName": "samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex"
},
{
"type": "html",
"resourceName": "products.html"
}
]
}
curl -X POST https://api.dpdf.io/v1.0/pdf
--header "Authorization: Bearer DP--api-key--"
--form "Instructions=@C:/temp/solution/instructions.json"
--form "Resource=@C:/temp/solution/DocumentA.pdf"
--form "Resource=@C:/temp/solution/DocumentB.pdf"
--form "Resource=@C:/temp/solution/testimage.tif"
--form "Resource=@C:/temp/solution/products.html"
--form "Resource=@C:/temp/solution/creating-pdf-dlex-layout.json"
--form "Resource=@C:/temp/solution/Doc1.docx"
--output output.pdf
Calling Endpoint Using Client Library
The following examples illustrate converting two PDFs, a Word document, an image, a DLEX report, and an HTML document into 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;
var inputA = pdf.AddPdf(new PdfResource(basePath + "/merge-pdfs-pdf-endpoint/DocumentA.pdf"));
pdf.AddPdf(new PdfResource(basePath + "/merge-pdfs-pdf-endpoint/DocumentB.pdf"));
pdf.AddWord(new WordResource(basePath + "/users-guide/Doc1.docx"));
ImageInput input = pdf.AddImage(new ImageResource(basePath + "/image-conversion/testimage.tif"));
input.ScaleX = (float)0.5;
input.ScaleY = (float)0.5;
LayoutDataResource layoutData = new LayoutDataResource(basePath + "/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.json");
pdf.AddDlex("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutData);
HtmlResource resource = new HtmlResource(System.IO.File.ReadAllText(basePath + "/users-guide/products.html"));
pdf.AddHtml(resource);
PdfResponse pdfResponse = pdf.Process();
if (pdfResponse.IsSuccessful)
{
File.WriteAllBytes(outputPath + "/merge-pdfs-solution-output-csharp.pdf", pdfResponse.Content);
}
else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
static async Run() {
var pdf = new Pdf();
var pdfInput = pdf.addPdf(new PdfResource(Constants.BasePath + "merge-pdfs-pdf-endpoint/DocumentA.pdf"));
var pdfResource = new PdfResource();
pdf.addPdf(new PdfResource(Constants.BasePath + "merge-pdfs-pdf-endpoint/DocumentB.pdf"));
pdf.addWord(new WordResource(Constants.BasePath + "users-guide/Doc1.docx", "Doc1.docx"));
var imageInput = pdf.addImage(new ImageResource(Constants.BasePath + "image-conversion/testimage.tif"));
imageInput.ScaleX = 0.5;
imageInput.ScaleY = 0.5;
var layoutData = new LayoutDataResource(Constants.BasePath + "creating-pdf-dlex-layout/creating-pdf-dlex-layout.json");
pdf.addDlex("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutData);
var resourcePath = Constants.BasePath + "/users-guide/products.html";
pdf.addHtml(new HtmlResource(resourcePath));
pdf.apiKey = Constants.ApiKey;
var res = await pdf.process();
if (res.isSuccessful) {
var outFile = Constants.OutputPath + "merge-pdfs-solution-nodejs-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, String outputPath) {
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
pdf.addPdf(new PdfResource(basePath + "/merge-pdfs-pdf-endpoint/DocumentA.pdf"));
pdf.addPdf(new PdfResource(basePath + "/merge-pdfs-pdf-endpoint/DocumentB.pdf"));
df.addWord(new WordResource(basePath + "/users-guide/Doc1.docx"), PageSize.LETTER, PageOrientation.PORTRAIT, (float) 1.0);
ImageInput input = pdf.addImage(new ImageResource(basePath + "/image-conversion/testimage.tif"));
input.setScaleX((float) 0.5);
input.setScaleY((float) 0.5);
String temp = "";
try {
temp = Files.readString(Paths.get(basePath + "/users-guide/products.html"));
} catch (IOException e) {
e.printStackTrace();
}
HtmlResource resource = new HtmlResource(temp);
pdf.addHtml(resource, "", PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
LayoutDataResource layoutData = new LayoutDataResource(
basePath + "/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.json");
pdf.addDlex("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutData);
PdfResponse pdfResponse = pdf.process();
if (pdfResponse.getIsSuccessful()) {
try {
FileUtils.writeByteArrayToFile(new File(DynamicPdfCloudApiExamples.OUTPUT_PATH + "/merging-solution-java-output.pdf"), pdfResponse.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(pdfResponse.getErrorJson());
}
}
public static function Run(string $apikey, string $path, string $output_path)
{
$pdf = new Pdf();
$pdf->ApiKey = $apikey;
$pdfInput = $pdf->AddPdf(new PdfResource($path . "/merge-pdfs-pdf-endpoint/DocumentA.pdf"));
$pdf->AddPdf(new PdfResource($path . "/merge-pdfs-pdf-endpoint/DocumentB.pdf"));
$wordResource = new WordResource($path . "/users-guide/Doc1.docx");
$wordInput = new WordInput($wordResource);
array_push($pdf->Inputs, $wordInput);
$imageResource = new ImageResource($path . "/image-conversion/testimage.tif");
$imageInput = $pdf->AddImage($imageResource);
$file = DynamicPdfExamples::GetFileData($path . "/users-guide/products.html");
$htmlResource = new HtmlResource($file);
$pdf->AddHtml($htmlResource);
$layoutData = new LayoutDataResource($path . "/creating-pdf-dlex-layout/creating-pdf-dlex-layout.json");
$pdf->AddDlex("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", $layoutData);
$response = $pdf->Process();
if($response->IsSuccessful)
{
file_put_contents($output_path . "/merge-pdfs-solution-php-output.pdf", $response->Content);
} else {
echo("Error: ");
echo($response->StatusCode);
echo($response->ErrorJson);
}
}
func main() {
pr := endpoint.NewPdf()
pr.Endpoint.ApiKey = apiKey
pdfResource := resource.NewPdfResourceWithResourcePath(basePath+"merge-pdfs-pdf-endpoint/DocumentA.pdf", "DocumentA.pdf")
prInput := input.NewPdfWithResource(pdfResource)
pr.Inputs = append(pr.Inputs, prInput)
pdfResource2 := resource.NewPdfResourceWithResourcePath(basePath+"merge-pdfs-pdf-endpoint/DocumentB.pdf", "DocumentB.pdf")
prInput2 := input.NewPdfWithResource(pdfResource2)
pr.Inputs = append(pr.Inputs, prInput2)
wordResource := resource.NewWordResourceWithResourcePath(basePath+"users-guide/Doc1.docx", "Doc1.docx")
pdfInput := input.NewWordInputWithResource(wordResource)
pr.Inputs = append(pr.Inputs, pdfInput)
imageResource1 := resource.NewImageResourceWithResourcePath(basePath+"image-conversion/testimage.tif", "testimage.tif")
imageInput1 := input.NewImagewithImageResource(imageResource1)
pr.Inputs = append(pr.Inputs, imageInput1)
layoutDataResource := resource.NewLayoutDataResource(basePath+"/creating-pdf-dlex-layout/creating-pdf-dlex-layout.json",
"creating-pdf-dlex-layout.json")
pr.AddDlexWithCloudResourceNLayoutData("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex",
layoutDataResource);
htmlResource := resource.NewHtmlResource(basePath+"users-guide/products.html", "products.html")
htmlInput := input.NewHtmlInputWithResource(htmlResource)
pr.Inputs = append(pr.Inputs, htmlInput)
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.Remove(outputPath)
os.WriteFile(outputPath, res.Content().Bytes(), os.ModeType)
}
}
def merge_solution(apikey, base_path, output_path):
pdf=Pdf()
pdf.api_key=apikey
inputA = pdf.add_pdf(PdfResource(base_path + "merge-pdfs-pdf-endpoint/DocumentA.pdf"))
pdf.add_pdf(PdfResource(base_path + "merge-pdfs-pdf-endpoint/DocumentB.pdf"))
pdf.add_word(WordResource(base_path + "users-guide/Doc1.docx"))
imageResource = ImageResource(base_path + "image-conversion/testimage.tif")
imageInput = pdf.add_image(imageResource)
layoutData = LayoutDataResource(base_path + "creating-pdf-dlex-layout/creating-pdf-dlex-layout.json")
pdf.add_dlex("samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex", layoutData)
with open(base_path + "users-guide/products.html", 'r', encoding='utf-8') as file:
htmlString = file.read()
htmlResource = HtmlResource(htmlString)
pdf.add_html(htmlResource)
response = pdf.process()
if response.is_successful:
with open(output_path + "merge-solution-output-python.pdf", "wb") as output_file:
output_file.write(response.content)
else:
print(response.error_id)
print(response.error_message)
print(response.error_json)
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.