PDF to PDF PHP Examples

This page contains various examples of using the PDF to PDF API in PHP. The examples are complete and fully functional. Read more about how to convert PDF to PDF in PHP.

Basic examples
PDF manipulation examples
PHP website examples
Laravel examples
Symfony examples

Basic examples

Multiple PDFs to PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/path/to/cover.pdf");
    $client->addPdfFile("/path/to/proposal.pdf");
    $client->addPdfFile("/path/to/price.pdf");
    $client->addPdfFile("/path/to/contact.pdf");

    // run the conversion and write the result to a file
    $client->convertToFile("offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Multiple PDFs to in-memory PDF

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/path/to/cover.pdf");
    $client->addPdfFile("/path/to/proposal.pdf");
    $client->addPdfFile("/path/to/price.pdf");
    $client->addPdfFile("/path/to/contact.pdf");

    // run the conversion and store the result into the "pdf" variable
    $pdf = $client->convert();

    // at this point the "pdf" variable contains PDF raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Multiple PDFs to PDF stream

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/path/to/cover.pdf");
    $client->addPdfFile("/path/to/proposal.pdf");
    $client->addPdfFile("/path/to/price.pdf");
    $client->addPdfFile("/path/to/contact.pdf");

    // create an output stream for the conversion result
    $output_stream = fopen("offer.pdf", "wb");

    // check for a file creation error
    if (!$output_stream)
        throw new \Exception(error_get_last()['message']);

    // run the conversion and write the result into the output stream
    $client->convertToStream($output_stream);

    // close the output stream
    fclose($output_stream);
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Multiple in-memory PDFs to PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfRawData(file_get_contents("/path/to/cover.pdf"));
    $client->addPdfRawData(file_get_contents("/path/to/proposal.pdf"));
    $client->addPdfRawData(file_get_contents("/path/to/price.pdf"));
    $client->addPdfRawData(file_get_contents("/path/to/contact.pdf"));

    // run the conversion and write the result to a file
    $client->convertToFile("offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Join 2 in-memory PDFs together with 2 local PDF files to PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfRawData(file_get_contents("/path/to/cover.pdf"));
    $client->addPdfFile("/path/to/proposal.pdf");
    $client->addPdfRawData(file_get_contents("/path/to/price.pdf"));
    $client->addPdfFile("/path/to/contact.pdf");

    // run the conversion and write the result to a file
    $client->convertToFile("offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Watermark a PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/path/to/proposal.pdf");
    $client->setPageWatermark("/path/to/watermark.pdf");

    // run the conversion and write the result to a file
    $client->convertToFile("company_offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Linearize a PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/path/to/not_linearized.pdf");
    $client->setLinearize(true);

    // run the conversion and write the result to a file
    $client->convertToFile("linearized.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Get info about the current conversion

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->setDebugLog(true);
    $client->addPdfRawData(file_get_contents("/path/to/cover.pdf"));
    $client->addPdfRawData(file_get_contents("/path/to/proposal.pdf"));

    // run the conversion and write the result to a file
    $client->convertToFile("offer.pdf");
    
    // print URL of the debug log
    echo "Debug log url: " . $client->getDebugLogUrl() . "\n";
    
    // print the number of conversion credits remaining in your account
    echo "Remaining credit count: " . $client->getRemainingCreditCount() . "\n";
    
    // print the number of credits used for the conversion
    echo "Consumed credit count: " . $client->getConsumedCreditCount() . "\n";
    
    // print the unique identifier for the conversion
    echo "Job id: " . $client->getJobId() . "\n";
    
    // print total number of pages in the output document
    echo "Page count: " . $client->getPageCount() . "\n";
    
    // print size of the output data in bytes
    echo "Output size: " . $client->getOutputSize() . "\n";
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

PDF manipulation examples

Extract page 3 and all pages from 7 to the end from the PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/your-path-to/pdfs/13_pages.pdf");
    $client->setAction("extract");
    $client->setPageRange("3,7-");

    // run the conversion and write the result to a file
    $client->convertToFile("output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Delete the first 3 pages and the 10th page from the PDF file

<?php
require "pdfcrowd.php";

try
{
    // create the API client instance
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

    // configure the conversion
    $client->addPdfFile("/your-path-to/pdfs/13_pages.pdf");
    $client->setAction("delete");
    $client->setPageRange("1-3,10");

    // run the conversion and write the result to a file
    $client->convertToFile("output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

Split the PDF file into two separate files at the 10th page

<?php
require "pdfcrowd.php";

try
{
    $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");
    $client->addPdfFile("/your-path-to/pdfs/13_pages.pdf");
    $client->setAction("extract");
    $client->setPageRange("1-10");
    $client->convertToFile("pages1-10.pdf");
    $client->setPageRange("11-");
    $client->convertToFile("pages11-end.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("Pdfcrowd Error: {$why}\n");
    throw $why;
}

?>

PHP website examples

Multiple PDFs to PDF in PHP website

<?php
require 'pdfcrowd.php';

// the recommended method is POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->addPdfFile("/path/to/cover.pdf");
        $client->addPdfFile("/path/to/proposal.pdf");
        $client->addPdfFile("/path/to/price.pdf");
        $client->addPdfFile("/path/to/contact.pdf");

        // run the conversion
        $pdf = $client->convert();

        // set HTTP response headers
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode("offer.pdf"));

        echo $pdf;
    }
    catch(\Pdfcrowd\Error $why) {
        // report the error
        header("Content-Type: text/plain");
        http_response_code($why->getCode());
        echo "Pdfcrowd Error: {$why}";
    }
}

?>

Multiple in-memory PDFs to PDF in PHP website

<?php
require 'pdfcrowd.php';

// the recommended method is POST
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->addPdfRawData(file_get_contents("/path/to/cover.pdf"));
        $client->addPdfRawData(file_get_contents("/path/to/proposal.pdf"));
        $client->addPdfRawData(file_get_contents("/path/to/price.pdf"));
        $client->addPdfRawData(file_get_contents("/path/to/contact.pdf"));

        // run the conversion
        $pdf = $client->convert();

        // set HTTP response headers
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode("offer.pdf"));

        echo $pdf;
    }
    catch(\Pdfcrowd\Error $why) {
        // report the error
        header("Content-Type: text/plain");
        http_response_code($why->getCode());
        echo "Pdfcrowd Error: {$why}";
    }
}

?>

Laravel examples

Multiple PDFs to PDF in Laravel

<?php

// the recommended method is POST
Route::post('/', function () {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->addPdfFile("/path/to/cover.pdf");
        $client->addPdfFile("/path/to/proposal.pdf");
        $client->addPdfFile("/path/to/price.pdf");
        $client->addPdfFile("/path/to/contact.pdf");

        // run the conversion and store the result into the "pdf" variable
        $pdf = $client->convert();

        // send the result and set HTTP response headers
        return response($pdf)
            ->header("Content-Type", "application/pdf")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("offer.pdf"));
    }
    catch(\Pdfcrowd\Error $why) {
        // send the error in the HTTP response
        return response($why->getMessage(), $why->getCode())
            ->header("Content-Type", "text/plain");
    }
});

?>

Multiple in-memory PDFs to PDF in Laravel

<?php

// the recommended method is POST
Route::post('/', function () {
    try {
        // create the API client instance
        $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

        // configure the conversion
        $client->addPdfRawData(file_get_contents("/path/to/cover.pdf"));
        $client->addPdfRawData(file_get_contents("/path/to/proposal.pdf"));
        $client->addPdfRawData(file_get_contents("/path/to/price.pdf"));
        $client->addPdfRawData(file_get_contents("/path/to/contact.pdf"));

        // run the conversion and store the result into the "pdf" variable
        $pdf = $client->convert();

        // send the result and set HTTP response headers
        return response($pdf)
            ->header("Content-Type", "application/pdf")
            ->header("Cache-Control", "no-cache")
            ->header("Accept-Ranges", "none")
            ->header("Content-Disposition",
                     "attachment; filename*=UTF-8''" . rawurlencode("offer.pdf"));
    }
    catch(\Pdfcrowd\Error $why) {
        // send the error in the HTTP response
        return response($why->getMessage(), $why->getCode())
            ->header("Content-Type", "text/plain");
    }
});

?>

Symfony examples

Multiple PDFs to PDF in Symfony

<?php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class DemoController
{
    /**
     * @Route("/", methods={"POST"})
     * the recommended method is POST
     */
    public function convert()
    {
        try {
            // create the API client instance
            $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

            // configure the conversion
            $client->addPdfFile("/path/to/cover.pdf");
            $client->addPdfFile("/path/to/proposal.pdf");
            $client->addPdfFile("/path/to/price.pdf");
            $client->addPdfFile("/path/to/contact.pdf");

            // run the conversion and store the result into the "pdf" variable
            $pdf = $client->convert();

            // send the result and set HTTP response headers
            return new Response(
                $pdf,
                Response::HTTP_OK,
                ["Content-Type" => "application/pdf",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("offer.pdf")]);
        }
        catch(\Pdfcrowd\Error $why) {
            // send the error in the HTTP response
            return new Response($why->getMessage(),
                                $why->getCode(),
                                ["Content-Type" => "text/plain"]);
        }
    }
}

Multiple in-memory PDFs to PDF in Symfony

<?php
namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class DemoController
{
    /**
     * @Route("/", methods={"POST"})
     * the recommended method is POST
     */
    public function convert()
    {
        try {
            // create the API client instance
            $client = new \Pdfcrowd\PdfToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");

            // configure the conversion
            $client->addPdfRawData(file_get_contents("/path/to/cover.pdf"));
            $client->addPdfRawData(file_get_contents("/path/to/proposal.pdf"));
            $client->addPdfRawData(file_get_contents("/path/to/price.pdf"));
            $client->addPdfRawData(file_get_contents("/path/to/contact.pdf"));

            // run the conversion and store the result into the "pdf" variable
            $pdf = $client->convert();

            // send the result and set HTTP response headers
            return new Response(
                $pdf,
                Response::HTTP_OK,
                ["Content-Type" => "application/pdf",
                 "Cache-Control" => "no-cache",
                 "Accept-Ranges" => "none",
                 "Content-Disposition" => "attachment; filename*=UTF-8''" . rawurlencode("offer.pdf")]);
        }
        catch(\Pdfcrowd\Error $why) {
            // send the error in the HTTP response
            return new Response($why->getMessage(),
                                $why->getCode(),
                                ["Content-Type" => "text/plain"]);
        }
    }
}