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 three PDF documents 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.
{
"author": "Alex Smith",
"inputs": [
{
"type": "pdf",
"resourceName": "DocumentA.pdf",
"startPage": 1,
"pageCount": 1
},
{
"type": "pdf",
"resourceName": "DocumentB.pdf"
},
{
"type": "pdf",
"resourceName": "samples/merge-pdfs-pdf-endpoint/DocumentC.pdf"
}
]
}
curl --location 'https://api.dynamicpdf.com/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--
--form 'Instructions=@C:/temp/solutions/merge-pdfs/instructions.json'
--form 'Resource=@C:/temp/solutions/merge-pdfs/DocumentA.pdf'
--form 'Resource=@C:/temp/solutions/merge-pdfs/DocumentB.pdf'
--form 'Resource=DocumentC.pdf'
Calling Endpoint Using Client Library
The following examples illustrate converting three PDFs to a combined PDF using the six different client libraries.
- C# (.NET)
- Java
- Node.js
- PHP
- GO
- Python
using System;
using System.IO;
namespace MergePdfs
{
class Program
{
static void Main(string[] args)
{
Run("DP.xxx-api-key-xxx", "C:/temp/dynamicpdf-api-samples/");
}
public static void Run(String apiKey, String basePath)
{
// create new pdf instance and set api key
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;
//add pdf and get reference to PdfInput then set pages to append
var inputA = pdf.AddPdf(new PdfResource(basePath + "DocumentA.pdf"));
inputA.StartPage = 1;
inputA.PageCount = 1;
// add all of pdf from local system
pdf.AddPdf(new PdfResource(basePath + "DocumentB.pdf"));
// add pdf from cloud in resource manager
pdf.AddPdf("/samples/merge-pdfs-pdf-endpoint/DocumentC.pdf");
//call the pdf endpoint and get response
PdfResponse pdfResponse = pdf.Process();
// if successful, save merged pdf to file otherwise print JSON error
if(pdfResponse.IsSuccessful)
{
File.WriteAllBytes(Path.Combine(basePath, "merge-pdfs-output.pdf"), pdfResponse.Content);
} else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
// process and get response from pdf endpoint
PdfResponse pdfResponse = pdf.Process();
// if the response is successful, save merged pdf to file if error, then printout JSON error
if(pdfResponse.IsSuccessful)
{
File.WriteAllBytes(Path.Combine(basePath, "merge-output.pdf"), pdfResponse.Content);
} else
{
Console.WriteLine(pdfResponse.ErrorJson);
}
}
}
import fs from 'fs';
import {
Pdf,
PdfResource,
PdfInput,
Endpoint
} from "@dynamicpdf/api"
export class MergePdfs {
static async Run() {
var pdf = new Pdf();
pdf.apiKey = "DP.xxx-api-key-xxx";
//add pdf from local system and obtain the reference to the pdfInput
var pdfInput = pdf.addPdf(new PdfResource("C:/temp/dynamicpdf-api-samples/DocumentA.pdf"));
//set the start page and page count so it returns one page
pdfInput.startPage = 1;
pdfInput.pageCount = 1;
//add the pdf from local system
pdf.addPdf(new PdfResource("C:/temp/dynamicpdf-api-samples/DocumentB.pdf"));
//add the pdf from File Manager
pdf.addPdf("samples/merge-pdfs-pdf-endpoint/DocumentC.pdf");
//call the endpoint and get response
var res = await pdf.process();
//if succsessful write response pdf to file otherwise print failure JSON
if (res.isSuccessful) {
var outFile = "C:/temp/dynamicpdf-api-samples/merge-pdfs-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
} else {
console.log(res.errorJson);
}
}
}
await MergePdfs.Run();
package com.dynamicpdf.api.examples;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.dynamicpdf.api.Pdf;
import com.dynamicpdf.api.PdfInput;
import com.dynamicpdf.api.PdfResource;
import com.dynamicpdf.api.PdfResponse;
public class MergePdfs {
public static void main(String[] args) {
MergePdfs.Run("DP.xxx-api-key-xxx",
"C:/temp/dynamicpdf-api-samples/");
}
public static void Run(String apiKey, String basePath) {
// create new pdf instance and set api key
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
// add pdf from local system where specify that only one page
// from DocumentA is to be merged.
PdfInput pdfInput = pdf.addPdf(new PdfResource(basePath + "DocumentA.pdf"));
pdfInput.setStartPage(1);
pdfInput.setPageCount(1);
// add all of DocumentB to the PDF to be created
pdf.addPdf(new PdfResource(basePath + "DocumentB.pdf"));
// add DocumentC from the cloud in File Manager
pdf.addPdf("samples/merge-pdfs-pdf-endpoint/DocumentC.pdf");
// call the endpoint and return the results
PdfResponse pdfResponse = pdf.process();
// if the response is successful, save merged pdf to file. if error, then
// printout JSON error
if (pdfResponse.getIsSuccessful()) {
try {
FileUtils.writeByteArrayToFile(new File(basePath + "merge-pdfs-output.pdf"), pdfResponse.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println(pdfResponse.getErrorJson());
}
}
}
<?php
require __DIR__ . '/vendor/autoload.php';
use DynamicPDF\Api\Pdf;
use DynamicPDF\Api\PdfResource;
use DynamicPDF\Api\PdfInput;
class MergePdfs
{
private static string $BasePath = "C:/temp/dynamicpdf-api-samples/";
public static function Run()
{
//create a new pdf and set api key
$pdf = new Pdf();
$pdf->ApiKey = "DP.xxx-api-key-xxx";
// add pdf from local system where specify that only one page
// from DocumentA is to be merged.
$pdfInput = $pdf->AddPdf(new PdfResource(MergePdfs::$BasePath . "DocumentA.pdf"));
$pdfInput->StartPage = 1;
$pdfInput->PageCount = 1;
//add pdf from local system
$pdf->AddPdf(new PdfResource(MergePdfs::$BasePath . "DocumentB.pdf"));
//add pdf from the cloud
$pdf->AddPdf("samples/merge-pdfs-pdf-endpoint/DocumentC.pdf");
//call the endpoint and get the response
$response = $pdf->Process();
//if successful save pdf to a file
if($response->IsSuccessful)
{
file_put_contents(MergePdfs::$BasePath . "merge-pdfs-output.pdf", $response->Content);
} else {
echo("Error: ");
echo($response->StatusCode);
echo($response->ErrorMessage);
}
}
}
MergePdfs::Run();
package main
import (
"fmt"
"os"
"github.com/dynamicpdf-api/go-client/endpoint"
"github.com/dynamicpdf-api/go-client/input"
"github.com/dynamicpdf-api/go-client/resource"
)
func main() {
pr := endpoint.NewPdf()
pr.Endpoint.BaseUrl = "https://api.dynamicpdf.com/"
pr.Endpoint.ApiKey = "DP.xxx-api-key-xxx"
basePath := "c:/temp/dynamicpdf-api-samples/"
pdfResource := resource.NewPdfResourceWithResourcePath(basePath+"DocumentA.pdf", "DocumentA.pdf")
prInput := input.NewPdfWithResource(pdfResource)
prInput.StartPage = 1
prInput.PageCount = 1
pr.Inputs = append(pr.Inputs, prInput)
pdfResource2 := resource.NewPdfResourceWithResourcePath(basePath+"DocumentB.pdf", "DocumentB.pdf")
prInput2 := input.NewPdfWithResource(pdfResource2)
pr.Inputs = append(pr.Inputs, prInput2)
mergeOption := input.NewMergeOptions()
prInput3 := input.NewPdfWithCloudPath("samples/merge-pdfs-pdf-endpoint/DocumentC.pdf", mergeOption)
pr.Inputs = append(pr.Inputs, prInput3)
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(basePath+"merge-pdfs-output.pdf",
res.Content().Bytes(), os.ModeType)
}
}
from dynamicpdf_api.pdf import Pdf
from dynamicpdf_api.pdf_resource import PdfResource
def merge_pdfs():
basePath = "C:/temp/dynamicpdf-api-samples/"
pdf=Pdf()
pdf.api_key="DP.xxx-api-key-xxx"
inputA = pdf.add_pdf(PdfResource(basePath + "DocumentA.pdf"))
inputA.start_page = 1
inputA.page_count = 2
pdf.add_pdf(PdfResource(basePath + "DocumentB.pdf"))
pdf.add_pdf("samples/merge-pdfs-pdf-endpoint/DocumentC.pdf")
response = pdf.process()
if response.is_successful:
with open(basePath + "merge-pdfs-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)
merge_pdfs()
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.