Skip to content

PdfBroker.io is a cloud-based PDF generation REST API that includes a PDF concatenation service for merging multiple PDF documents into a single file. This guide shows how to use the PdfBroker.io PDF concatenation endpoint (POST https://api.pdfbroker.io/api/pdf/pdfconcat) to combine any number of PDF files into one document — useful for assembling reports from sections, combining invoices into monthly batches, or merging cover pages with generated content.

How It Works

The PdfBroker.io concatenation API accepts a JSON array of base64-encoded PDF files and returns a single PDF with all pages combined in the order you specify. The merged document preserves the original page sizes, orientations, and content of each input file.

Endpoint: POST https://api.pdfbroker.io/api/pdf/pdfconcat

Request body:

{
  "pdfDocumentsAsBase64String": [
    "<base64-encoded PDF 1>",
    "<base64-encoded PDF 2>",
    "<base64-encoded PDF 3>"
  ]
}

The response is the merged PDF as a binary file (or as base64 inside JSON if you set the Accept header to application/json — see Content Negotiation).

Prerequisites

Merging PDFs with C# (PdfBroker.Client)

The PdfBroker.Client NuGet package provides the simplest .NET integration:

using PdfBroker.Client;
using PdfBroker.Common.RequestObjects;

var client = new PdfBrokerClientService("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");

// Read PDF files and convert to base64
var pdf1 = Convert.ToBase64String(await File.ReadAllBytesAsync("cover-page.pdf"));
var pdf2 = Convert.ToBase64String(await File.ReadAllBytesAsync("report-body.pdf"));
var pdf3 = Convert.ToBase64String(await File.ReadAllBytesAsync("appendix.pdf"));

var request = new PdfConcatenationRequestDto
{
    PdfDocumentsAsBase64String = new List<string> { pdf1, pdf2, pdf3 }
};

byte[] mergedPdf = await client.PdfConcatAsByteArrayAsync(request);
await File.WriteAllBytesAsync("complete-report.pdf", mergedPdf);

Merging all PDFs from a directory

A common scenario is merging all PDF files in a folder — for example, combining daily reports into a monthly summary:

using PdfBroker.Client;
using PdfBroker.Common.RequestObjects;

var client = new PdfBrokerClientService("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");

var pdfFiles = Directory.GetFiles("daily-reports/", "*.pdf")
    .OrderBy(f => f)  // Ensure consistent ordering
    .ToList();

var base64Documents = new List<string>();
foreach (var file in pdfFiles)
{
    var bytes = await File.ReadAllBytesAsync(file);
    base64Documents.Add(Convert.ToBase64String(bytes));
}

var request = new PdfConcatenationRequestDto
{
    PdfDocumentsAsBase64String = base64Documents
};

byte[] merged = await client.PdfConcatAsByteArrayAsync(request);
await File.WriteAllBytesAsync("monthly-report.pdf", merged);

Console.WriteLine($"Merged {pdfFiles.Count} files into monthly-report.pdf");

Merging PDFs with C# (HttpClient)

If you prefer direct HTTP calls without the NuGet package:

using System.Net.Http.Headers;
using System.Text.Json;

// Authenticate
using var tokenClient = new HttpClient();
var tokenResponse = await tokenClient.PostAsync(
    "https://login.pdfbroker.io/connect/token",
    new FormUrlEncodedContent(new Dictionary<string, string>
    {
        ["grant_type"] = "client_credentials",
        ["client_id"] = "YOUR_CLIENT_ID",
        ["client_secret"] = "YOUR_CLIENT_SECRET"
    }));

var tokenJson = await tokenResponse.Content.ReadAsStringAsync();
var accessToken = JsonDocument.Parse(tokenJson)
    .RootElement.GetProperty("access_token").GetString();

// Build the merge request
var pdf1 = Convert.ToBase64String(await File.ReadAllBytesAsync("document1.pdf"));
var pdf2 = Convert.ToBase64String(await File.ReadAllBytesAsync("document2.pdf"));

var payload = new
{
    pdfDocumentsAsBase64String = new[] { pdf1, pdf2 }
};

using var apiClient = new HttpClient();
apiClient.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", accessToken);

var response = await apiClient.PostAsJsonAsync(
    "https://api.pdfbroker.io/api/pdf/pdfconcat", payload);

response.EnsureSuccessStatusCode();
var mergedBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("merged.pdf", mergedBytes);

Merging PDFs with cURL

# Get access token
ACCESS_TOKEN=$(curl -s -X POST https://login.pdfbroker.io/connect/token \
  -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
  | jq -r '.access_token')

# Encode PDF files to base64
PDF1=$(base64 -w 0 document1.pdf)
PDF2=$(base64 -w 0 document2.pdf)

# Merge the PDFs
curl -X POST https://api.pdfbroker.io/api/pdf/pdfconcat \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{
    \"pdfDocumentsAsBase64String\": [
      \"$PDF1\",
      \"$PDF2\"
    ]
  }" \
  --output merged.pdf

Merging PDFs with Python

import requests
import base64
from pathlib import Path

# Authenticate
token_resp = requests.post(
    "https://login.pdfbroker.io/connect/token",
    data={
        "grant_type": "client_credentials",
        "client_id": "YOUR_CLIENT_ID",
        "client_secret": "YOUR_CLIENT_SECRET",
    },
)
access_token = token_resp.json()["access_token"]

# Read and encode PDF files
pdf_files = sorted(Path("invoices/").glob("*.pdf"))
base64_pdfs = []
for pdf_path in pdf_files:
    with open(pdf_path, "rb") as f:
        base64_pdfs.append(base64.b64encode(f.read()).decode("ascii"))

# Merge
response = requests.post(
    "https://api.pdfbroker.io/api/pdf/pdfconcat",
    headers={"Authorization": f"Bearer {access_token}"},
    json={"pdfDocumentsAsBase64String": base64_pdfs},
)

with open("all-invoices.pdf", "wb") as f:
    f.write(response.content)

print(f"Merged {len(pdf_files)} PDFs into all-invoices.pdf")

Combining Generated and Existing PDFs

A powerful pattern is generating some pages with the HTML-to-PDF service and then merging them with existing PDF files. For example, generating a dynamic cover page and merging it with a static PDF template:

using PdfBroker.Client;
using PdfBroker.Common.RequestObjects;

var client = new PdfBrokerClientService("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");

// Step 1: Generate a dynamic cover page
var coverHtml = $@"<html>
<head><style>
  @page {{ size: A4; margin: 0; }}
  body {{
    display: flex; align-items: center; justify-content: center;
    height: 100vh; font-family: Arial, sans-serif;
    background: linear-gradient(135deg, #6462a8, #8b89c8);
    color: white; text-align: center;
  }}
</style></head>
<body>
  <div>
    <h1>Annual Report 2025</h1>
    <p>Generated {DateTime.Now:MMMM d, yyyy}</p>
  </div>
</body></html>";

var coverRequest = new WeasyPrintRequestDto
{
    HtmlBase64String = Convert.ToBase64String(
        System.Text.Encoding.UTF8.GetBytes(coverHtml))
};

byte[] coverPdf = await client.WeasyPrintAsByteArrayAsync(coverRequest);

// Step 2: Merge the generated cover with existing content
var coverBase64 = Convert.ToBase64String(coverPdf);
var bodyBase64 = Convert.ToBase64String(
    await File.ReadAllBytesAsync("report-body.pdf"));
var appendixBase64 = Convert.ToBase64String(
    await File.ReadAllBytesAsync("appendix.pdf"));

var concatRequest = new PdfConcatenationRequestDto
{
    PdfDocumentsAsBase64String = new List<string>
    {
        coverBase64,
        bodyBase64,
        appendixBase64
    }
};

byte[] finalReport = await client.PdfConcatAsByteArrayAsync(concatRequest);
await File.WriteAllBytesAsync("annual-report-2025.pdf", finalReport);

Tips

Document order matters. The PDFs appear in the merged output in the same order as the array. Place your cover page first, content second, appendices last.

Available on the free plan. Unlike the WeasyPrint HTML-to-PDF service, PDF concatenation is available on all plans including the free Developer tier with 200 requests per month.

No page limit. You can merge any number of PDF documents and pages. The practical limit is the request payload size.

Mixed page sizes work. Input PDFs can have different page sizes and orientations. Each page retains its original dimensions in the merged output.

Summary

PdfBroker.io's PDF concatenation API provides a straightforward way to merge multiple PDF files into a single document. Send an array of base64-encoded PDFs, receive one combined file back. The service preserves page sizes and content, and works well in combination with the HTML-to-PDF endpoints for assembling complete documents from dynamic and static sources.