Merge PDF documents

Use this action to combine multiple PDF documents into a single PDF document.

Endpoints

POST https://api.pdfblocks.com/v1/merge_documents

Global endpoint (default)

POST https://eu.api.pdfblocks.com/v1/merge_documents

European-only endpoint

POST https://{base_url}/v1/merge_documents

Self-hosted server endpoint

Parameters

The endpoint accepts multipart/form-data request bodies with the folowing parameters:

file Required

The content of the input PDF documents.

  • Repeat the file field for each of the PDF document to merge. See the code examples below.
  • The output merged PDF document will contain the input documents in the same order they are added.
  • You can also use file[] and file_1 to file_10 as the parameter name.

Returns

If successful, the call returns a response with Content-Type: application/pdf with the content of the merged PDF document. Otherwise, this call returns an error.

Code examples

curl

curl \
-F file=@input_1.pdf \
-F file=@input_2.pdf \
-F file=@input_3.pdf \
-o merged.pdf \
-H 'X-Api-Key: your_api_key' \
https://api.pdfblocks.com/v1/merge_documents

Python

# pip install requests

import requests

url = 'https://api.pdfblocks.com/v1/merge_documents'

body = [
  ('file', open('input_1.pdf', 'rb')),
  ('file', open('input_2.pdf', 'rb')),
  ('file', open('input_3.pdf', 'rb'))
  ]

headers = { 'X-Api-Key' : 'your_api_key' }

response = requests.post(url, files=body, headers=headers)

if response.status_code == 200:
  with open('merged.pdf', 'wb') as output:
    for chunk in response.iter_content():
        output.write(chunk)

Ruby

# gem install http

require 'http'

url = 'https://api.pdfblocks.com/v1/merge_documents'

body = {
  file: [
    HTTP::FormData::File.new('input_1.pdf'),
    HTTP::FormData::File.new('input_2.pdf'),
    HTTP::FormData::File.new('input_3.pdf')
  ]
}

headers = { 'X-Api-Key' => 'your_api_key' }

response = HTTP.post(url, form: body, headers: headers)

if response.status.ok?
  File.open('merged.pdf', 'wb') do |output|
    response.body.each { |chunk| output.write(chunk) }
  end
end

C# (.NET Core)

// Using .NET Core 3.1

using System.IO;
using System.Net.Http;

class Program
{
    static void Main()
    {
        string url = "https://api.pdfblocks.com/v1/merge_documents";

        var inputFile1 = new FileStream("input_1.pdf", FileMode.Open, FileAccess.Read);
        var inputFile2 = new FileStream("input_2.pdf", FileMode.Open, FileAccess.Read);
        var inputFile3 = new FileStream("input_3.pdf", FileMode.Open, FileAccess.Read);

        var body = new MultipartFormDataContent();
        body.Add(new StreamContent(inputFile1), "file");
        body.Add(new StreamContent(inputFile2), "file");
        body.Add(new StreamContent(inputFile3), "file");

        var message = new HttpRequestMessage(HttpMethod.Post, url);
        message.Content = body;
        message.Headers.Add("X-Api-Key", "your_api_key");

        var client = new HttpClient();
        var response = client.SendAsync(message).Result;
        if(response.IsSuccessStatusCode)
        {
            using var output = new FileStream("merged.pdf", FileMode.Create, FileAccess.Write);
            response.Content.CopyToAsync(output).Wait();
        }
    }
}