How to Merge Different File Formats Together as a PDF with cURL

Learn how to convert different source formats and merge the resulting PDF resources in one cURL workflow.
Share this page

In this tutorial, we'll walk through how to merge different file formats together as a PDF using cURL and the Merge PDFs API Tool. The complete shell command stays visible, and each pdfRest-specific field is explained before you adapt the Merge Different Formats Together as a PDF request to your own workflow.

Why Merge Different File Formats Together as a PDF with cURL?

Business packets often combine images, presentations, office documents, and existing PDFs even though Merge PDFs accepts PDF inputs. Converting each non-PDF source first provides a common representation that can be assembled in a controlled order.

A project-delivery script might receive a PNG diagram and a PowerPoint briefing. The sample converts both files through /pdf, captures their output IDs, and submits those IDs to /merged-pdf to create one deliverable.

The example uses jq to read each outputId from JSON. Keeping the IDs in shell variables avoids downloading and uploading the intermediate PDFs between API calls.

cURL Code Example

#!/bin/sh

# In this sample, we will show how to merge different file types together as
# discussed in https://pdfrest.com/solutions/merge-multiple-types-of-files-together/.
# First, we will upload an image file to the /pdf route and capture the output ID.
# Next, we will upload a PowerPoint file to the /pdf route and capture its output
# ID. Finally, we will pass both IDs to the /merged-pdf route to combine both inputs
# into a single PDF.
#
# Note that there is nothing special about an image and a PowerPoint file, and
# this sample could be easily used to convert and combine any two file types
# that the /pdf route takes as inputs.

# By default, we use the US-based API service. This is the primary endpoint for global use.
API_URL="https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
# API_URL="https://eu-api.pdfrest.com"

API_KEY="xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # place your api key here

IMAGE_ID=$(curl -X POST "$API_URL/pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: $API_KEY" \
  -F "file=@/path/to/file.png" \
  -F "output=example_out" \
  | jq -r '.outputId')


PPT_ID=$(curl -X POST "$API_URL/pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: $API_KEY" \
  -F "file=@/path/to/powerpoint.ppt" \
  -F "output=example_out" \
  | jq -r '.outputId')


curl -X POST "$API_URL/merged-pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: $API_KEY" \
  -F "id=$IMAGE_ID" \
  -F "pages[]=all" -F "type[]=id" \
  -F "id=$PPT_ID" \
  -F "pages[]=all" -F "type[]=id" \
  -F "output=example_out"

Source for Merge Different Formats Together as a PDF: View the cURL sample on GitHub.

Breaking Down the Code

Choose the pdfRest service region

# By default, we use the US-based API service. This is the primary endpoint for global use.
API_URL="https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
# API_URL="https://eu-api.pdfrest.com"

The active API_URL uses the US service. The commented alternative switches the whole multi-request workflow to the EU service without changing any endpoint paths.

Keep the API key available for every stage

API_KEY="xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # place your api key here

The same placeholder credential is reused for both conversions and the final merge. Replace it securely at runtime rather than committing a real API key to the script.

Convert the image and capture its PDF resource ID

IMAGE_ID=$(curl -X POST "$API_URL/pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: $API_KEY" \
  -F "file=@/path/to/file.png" \
  -F "output=example_out" \
  | jq -r '.outputId')

The first command uploads a PNG to Convert to PDF. Command substitution assigns the resulting outputId to IMAGE_ID, and jq -r returns the string without JSON quotes.

Convert the presentation and retain its output ID

PPT_ID=$(curl -X POST "$API_URL/pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: $API_KEY" \
  -F "file=@/path/to/powerpoint.ppt" \
  -F "output=example_out" \
  | jq -r '.outputId')

The second conversion repeats the pattern for PowerPoint. Nothing is downloaded between steps because pdfRest can accept the managed output ID directly in the merge request.

Merge the converted resources in order

curl -X POST "$API_URL/merged-pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: $API_KEY" \
  -F "id=$IMAGE_ID" \
  -F "pages[]=all" -F "type[]=id" \
  -F "id=$PPT_ID" \
  -F "pages[]=all" -F "type[]=id" \
  -F "output=example_out"

Each group of id, pages[], and type[] fields describes one merge input. Their order in the multipart request controls the order of content in the final PDF.

Beyond the Tutorial

This complete flow shows how cURL can orchestrate conversion and merging across several API calls while passing managed resource IDs between stages.

Check every conversion response before starting the merge, and associate each ID with its original filename so ordering remains explicit. Save the final PDF and clean up intermediate resources according to the workflow retention policy.

Continue exploring the Merge Different Formats Together as a PDF workflow in API Lab. For the complete Merge Different Formats Together as a PDF request contract, accepted values, defaults, and limitations, consult the Merge PDFs API Tool documentation.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.