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 an 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 save the result to a file.
    $client->convertToFile("offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Multiple PDFs to in-memory PDF

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 in 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}");
    throw $why;
}

?>

Multiple PDFs to PDF stream

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 to the output stream.
    $client->convertToStream($output_stream);

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

?>

Multiple in-memory PDFs to PDF file

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 save the result to a file.
    $client->convertToFile("offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

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

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 save the result to a file.
    $client->convertToFile("offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Watermark a PDF file

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 save the result to a file.
    $client->convertToFile("company_offer.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Linearize a PDF file

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 save the result to a file.
    $client->convertToFile("linearized.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

Get info about the current conversion

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 save the result to a file.
    $client->convertToFile("offer.pdf");
    
    // print URL pointing to the debug log for this request.
    echo "Debug log url: " . $client->getDebugLogUrl() . "\n";
    
    // print Number of conversion credits remaining in your account.
    echo "Remaining credit count: " . $client->getRemainingCreditCount() . "\n";
    
    // print Number of credits consumed for this conversion.
    echo "Consumed credit count: " . $client->getConsumedCreditCount() . "\n";
    
    // print Unique identifier assigned to this conversion job.
    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}");
    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 an 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 save the result to a file.
    $client->convertToFile("output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    throw $why;
}

?>

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

<?php
require "pdfcrowd.php";

try
{
    // Create an 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 save the result to a file.
    $client->convertToFile("output.pdf");
}
catch(\Pdfcrowd\Error $why)
{
    error_log("PDFCrowd Error: {$why}");
    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}");
    throw $why;
}

?>

PHP website examples

Laravel examples

Symfony examples