Merge

Pdf4me Merge unites two or possibly multiple PDFs by concatenating them. As a result providing a single Pdf file joined in the order the files are provided.

FeatureParameterResponseActionDescriptionLinks
mergeMergeMergeResMergeActionUnites two or possibly multiple PDFs by concatenating them. Swagger
Sample
merge2pdfsfile1, file2file streamUnites two PDFs by concatenating them and further outputs a single Pdf file.
Swagger
Sample

Samples

Merge

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl No Sample
// create merge object
var merge = new Merge()
{
    // documents
    Documents = new System.Collections.Generic.HashSet()
    {
        new Document()
        {
            DocData = File.ReadAllBytes("myFirstPdf.pdf"),
            Name = "myFirstPdf.pdf",
        },
        new Document()
        {
            DocData = File.ReadAllBytes("mySecondPdf.pdf"),
            Name = "mySecondPdf.pdf",
        },
    },
    // action
    MergeAction = new MergeAction()
};

// merge
var res = await Pdf4meClient.Pdf4me.Instance.MergeClient.MergeAsync(merge);

// extract the merged PDF and writing it to disk
byte[] mergedPdf = res.Document.DocData;
File.WriteAllBytes("mergedPdf.pdf", mergedPdf);
// setup the mergeClient
MergeClient mergeClient = new MergeClient(pdf4meClient);

// create merge object
Merge merge = new Merge();
// documents
List documents = new ArrayList();
Document doc1 = new Document();
doc1.setDocData(Files.readAllBytes(Paths.get("myFirstPdf.pdf")));
documents.add(doc1);
Document doc2 = new Document();
doc2.setDocData(Files.readAllBytes(Paths.get("mySecondPdf.pdf")));
documents.add(doc2);
merge.setDocuments(documents);
// action
merge.setMergeAction(new MergeAction());

// merge
MergeRes res = mergeClient.merge(merge);

// extracting the generated PDF and writing it to disk
byte[] mergedPdf = res.getDocument().getDocData();
FileUtils.writeByteArrayToFile(new File("mergedPdf.pdf"), mergedPdf);
// setup the pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// create merge object
const mergeReq = {
  // documents
  documents: [
    {
      docData: fs.readFileSync(path.join(__dirname, 'myFirstPdf.pdf')).toString('base64'),
    },
    {
      docData: fs.readFileSync(path.join(__dirname, 'mySecondPdf.pdf')).toString('base64'),
    },
  ],
  // action
  mergeAction: {},
}

// merge
pdf4meClient.merge(mergeReq)
  .then(function(mergeRes) {
    // extract the merged PDF and writing it to disk
    const pdf = Buffer.from(mergeRes.document.docData, 'base64')
    fs.writeFileSync(path.join(__dirname, 'mergedPdf.pdf'), pdf)
  })
  .catch(err => {
    console.log(err)
  })
$mergedPdf = $client->pdf4me()->pdfMerge([
    "documents" => [
        [
            'docData' => $client->getFileData('myFirstPdf.pdf')
        ],
        [
            'docData' => $client->getFileData('mySecondPdf.pdf')
        ]
    ]
]);

// extracting the generated PDF
$mergedPdf = base64_decode($createMerge->document->docData);
// and writing it to file
file_put_contents('mergedPdf.pdf', $mergedPdf);
# setup the merge_client
merge_client = MergeClient(pdf4me_client)

# create the merge object
merge = Merge(
    documents=[
        Document(
            doc_data=FileReader().get_file_data('myFirstPdf.pdf')
        ),
        Document(
            doc_data=FileReader().get_file_data('mySecondPdf.pdf')
        )
    ],
    merge_action=MergeAction()
)

# merge
res = merge_client.merge(merge=merge)

# extracting the generated PDF
merged_pdf = base64.b64decode(res['document']['doc_data'])
# writing it to disk
with open('mergedPdf.pdf', 'wb') as f:
    f.write(merged_pdf)
file_path_1 = './in/PDF_1Page.pdf'
file_path_2 = './in/PDF_10Pages.pdf'
file_path_3 = './in/PDF_100Pages.pdf'

action = Pdf4me::Merge.new(
    documents: [
      Pdf4me::Document.new(
        doc_data: Base64.encode64(File.open(file_path_1, 'rb', &:read))
      ),
      Pdf4me::Document.new(
        doc_data: Base64.encode64(File.open(file_path_2, 'rb', &:read))
      ),
      Pdf4me::Document.new(
        doc_data: Base64.encode64(File.open(file_path_3, 'rb', &:read))
      )
    ]
)
response = action.run

# saving the merged PDF
File.open('./out/merg_111Pages.pdf', 'wb') do |f|
 f.write(Base64.decode64(response.document.doc_data))
end

Merge2pdfs

  • curl
  • C#
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
curl https://api.pdf4me.com/Merge/Merge2Pdfs ^
    -H "Authorization: Basic DEV-KEY" ^
    -F "file1=@./myFirstPdf.pdf" ^
    -F "file2=@./mySecondPdf.pdf" ^
    -o ./mergedPdf.pdf
// merge
byte[] mergedPdf = await Pdf4meClient.Pdf4me.Instance.MergeClient.Merge2PdfsAsync(
    File.ReadAllBytes("myFirstPdf.pdf"),
    File.ReadAllBytes("mySecondPdf.pdf")
);
    
// and writing the generated PDF to disk
File.WriteAllBytes("mergedPdf.pdf", mergedPdf);
// setup the mergeClient
MergeClient mergeClient = new MergeClient(pdf4meClient);

// merge and writing the generated PDF to disk
byte[] mergedPdf = mergeClient.merge2Pdfs(new File("myFirstPdf.pdf"), new File("mySecondPdf.pdf"));
FileUtils.writeByteArrayToFile(new File("mergedPdf.pdf"), mergedPdf);
// setup the pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')

// merge
pdf4meClient.merge2pdfs(
    fs.createReadStream(path.join(__dirname, 'myFirstPdf.pdf')),
    fs.createReadStream(path.join(__dirname, 'mySecondPdf.pdf'))
  )
  .then(pdf => {
    // and writing the generated PDF to disk
    fs.writeFileSync(path.join(__dirname, 'mergedPdf.pdf'), pdf)
  })
  .catch(err => {
    console.log(err)
  })
$mergedPdf = $client->pdf4me()->merge2Pdfs(
    [
        "file1" => __DIR__.'/myFirstPdf.pdf'
        "file2" => __DIR__.'/mySecondPdf.pdf'
    ]
);

//writing it to file
file_put_contents('mergedPdf.pdf, $mergedPdf);
# setup the merge_client
merge_client = MergeClient(pdf4me_client)

# merge
merged_pdf = merge_client.merge_2_pdfs(
    file1=FileReader().get_file_handler(path='myFirstPdf.pdf'),
    file2=FileReader().get_file_handler(path='mySecondPdf.pdf')
)
# writing the generated PDF to disk
with open('mergedPdf.pdf', 'wb') as f:
    f.write(merged_pdf)
a = Pdf4me::MergeTwoPdfs.new(
        file1: '/myFirstPdf.pdf',
        file2: '/mySecondPdf.pdf',
        save_path: 'merged.pdf'
    )
a.run

Model

Merge

Name Type Description Notes
document Document
mergeAction MergeAction
jobId String [optional]
jobIdExtern String [optional]
integrations [String] [optional]

MergeAction

Name Type Description Notes

MergeRes

Name Type Description Notes
document Document Document composed of the merged files.

Document

Name Type Description Notes
jobId String JobId of Documents WorkingSet.
documentId String Document Id
name String Filename inlcuding filetype.
docStatus String Status of the Document, e.g. Stamped.
pages Page Description of pages.
docData [byte] Document bytes.
docMetadata DocMetadata Document metadata such as title, pageCount et al.
docLogs DocLog Logging information about the request, e.g. timestamp.

Page

Name Type Description Notes
documentId String Globally unique Id.
pageId String Globally unique Id.
pageNumber Integer PageNumber, starting with 1.
rotate double By how much the page was rotated from its original orientation.
thumbnail byte Thumbnail representing this particular page.
sourceDocumentId String Id of the document it was created from, e.g. in case of an extraction, the result's sourceDocumentId is the Id of the PDF the pages have been extracted from.
sourcePageNumber Integer Page number of the original page in the original document, e.g. let's assume document B consists of page number 4 of document A (extraction).
Thus, document B's only page's sourcePageNumber is number 4.

DocMetadata

Name Type Description Notes
title String Title of document.
subject String Subject of document.
pageCount long Number of pages.
size long Number bytes of the document.
isEncrypted boolean If the document is Encrypted
pdfCompliance String Pdf Compliance, e.g. PDF/A.
isSigned boolean If the document is Encrypted
uploadedMimeType String Uploaded MimeType, e.g. application/bson.
uploadedFileSize long Uploaded file size.

DocLog

Name Type Description Notes
messageType String MessageType, e.g. PdfALog.
message String Message itself, e.g. a warning.
timestamp dateTime Timestamp.
docLogLevel String Type of message. Supported Values :
"verbose", "info", "warning", "error", "timing"
durationMilliseconds long Timing for requested log information [ms].
Was this article helpful to you? Yes 2 No

How can we help?