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

Convert different source formats and merge their PDF results in an ordered PHP workflow using managed resource IDs.
Share this page

This step-by-step guide demonstrates how to convert different file formats and merge them as one PDF from a PHP application through the Merge PDFs API Tool. Along the way, we'll distinguish the PHP syntax from the Merge Different Formats Together as a PDF API contract and show how its returned data supports the next step.

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

Merge PDFs accepts PDF inputs, while real business packets often begin as a mixture of images, presentations, and existing documents. Converting the non-PDF sources first gives every item a common format for ordered assembly.

A PHP claims system could receive a damage photograph and a PowerPoint assessment from different contributors. The sample converts each source through Convert to PDF, captures both output IDs, and merges them into one review packet without transferring the intermediate PDFs back to the application.

This is a three-request workflow rather than a single conversion call. Each conversion must succeed before its resource ID is added to the merge payload, and the order of those payload groups determines the order in the final document.

PHP Code Example

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

/* 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.
$apiUrl = "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
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client();

$headers = [
  'Api-Key' => 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$imageToPDFOptions = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/image.png', 'r'),
      'filename' => 'image.png',
      'headers' => [
        'Content-Type' => ''
      ]
    ]
  ]
];

$imageToPDFRequest = new Request('POST', $apiUrl.'/pdf', $headers);

$imageToPDFResponse = $client->sendAsync($imageToPDFRequest, $imageToPDFOptions)->wait();

$convertedImageID = json_decode($imageToPDFResponse->getBody())->{'outputId'};



$powerpointToPDFOptions = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/powerpoint.ppt', 'r'),
      'filename' => 'powerpoint.ppt',
      'headers' => [
        'Content-Type' => ''
      ]
    ]
  ]
];

$powerpointToPDFRequest = new Request('POST', $apiUrl.'/pdf', $headers);

$powerpointToPDFResponse = $client->sendAsync($powerpointToPDFRequest, $powerpointToPDFOptions)->wait();

$convertedPowerpointID = json_decode($powerpointToPDFResponse->getBody())->{'outputId'};


$mergeOptions = [
  'multipart' => [
    [
      'name' => 'id[]',
      'contents' => $convertedImageID
    ],
    [
      'name' => 'pages[]',
      'contents' => '1-last'
    ],
    [
      'name' => 'type[]',
      'contents' => 'id'
    ],
    [
      'name' => 'id[]',
      'contents' => $convertedPowerpointID

    ],
    [
      'name' => 'pages[]',
      'contents' => '1-last'
    ],
    [
      'name' => 'type[]',
      'contents' => 'id'
    ]
  ]
];

$mergeRequest = new Request('POST', $apiUrl.'/merged-pdf', $headers);

$mergeResponse = $client->sendAsync($mergeRequest, $mergeOptions)->wait();

echo $mergeResponse->getBody();

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

Breaking Down the Code

Load Guzzle and its stream helpers

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

Guzzle supplies the HTTP client and PSR-7 request objects used throughout the Merge Different Formats Together as a PDF example. For this PHP Merge Different Formats Together as a PDF flow, Utils::tryFopen creates readable multipart streams without loading every uploaded byte into one string.

Select the pdfRest service region

// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "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
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client();

The active $apiUrl selects the US service for Merge Different Formats Together as a PDF; the commented assignment shows the EU alternative. Keep the PHP uploads, request IDs, and generated resources for Merge Different Formats Together as a PDF on that same regional host.

Convert the image to PDF

$imageToPDFOptions = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/image.png', 'r'),
      'filename' => 'image.png',
      'headers' => [
        'Content-Type' => ''
      ]
    ]
  ]
];

$imageToPDFRequest = new Request('POST', $apiUrl.'/pdf', $headers);

$imageToPDFResponse = $client->sendAsync($imageToPDFRequest, $imageToPDFOptions)->wait();

$convertedImageID = json_decode($imageToPDFResponse->getBody())->{'outputId'};

The first PHP conversion sends the image to /pdf and captures its outputId. That ID becomes the first Merge Different Formats Together as a PDF input without an intermediate download back to PHP.

Convert the presentation to PDF

$powerpointToPDFOptions = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/powerpoint.ppt', 'r'),
      'filename' => 'powerpoint.ppt',
      'headers' => [
        'Content-Type' => ''
      ]
    ]
  ]
];

$powerpointToPDFRequest = new Request('POST', $apiUrl.'/pdf', $headers);

$powerpointToPDFResponse = $client->sendAsync($powerpointToPDFRequest, $powerpointToPDFOptions)->wait();

$convertedPowerpointID = json_decode($powerpointToPDFResponse->getBody())->{'outputId'};

The second PHP conversion repeats the normalization step for the presentation. The Merge Different Formats Together as a PDF workflow must receive this presentation outputId before PHP constructs the complete merge request.

Assemble and send the merge request

$mergeOptions = [
  'multipart' => [
    [
      'name' => 'id[]',
      'contents' => $convertedImageID
    ],
    [
      'name' => 'pages[]',
      'contents' => '1-last'
    ],
    [
      'name' => 'type[]',
      'contents' => 'id'
    ],
    [
      'name' => 'id[]',
      'contents' => $convertedPowerpointID

    ],
    [
      'name' => 'pages[]',
      'contents' => '1-last'
    ],
    [
      'name' => 'type[]',
      'contents' => 'id'
    ]
  ]
];

$mergeRequest = new Request('POST', $apiUrl.'/merged-pdf', $headers);

$mergeResponse = $client->sendAsync($mergeRequest, $mergeOptions)->wait();

echo $mergeResponse->getBody();

The PHP merge body groups each resource ID with its input type and selected pages. Their repeated-field order controls how the converted sources appear in the Merge Different Formats Together as a PDF result returned to PHP.

Beyond the Tutorial

You have followed the complete PHP sequence from source conversion through resource-ID handoff and final PDF assembly.

Associate every returned ID with its original filename and check both conversion responses before merging. That bookkeeping makes ordering explicit and prevents a partial job from producing a packet with the wrong source or a missing section.

Try the Merge Different Formats Together as a PDF workflow with a representative file in API Lab, then adapt the request in PHP. The Merge PDFs API Tool documentation provides the complete PHP context for Merge Different Formats Together as a PDF values, defaults, and limitations.

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