PDF to PDF PHP Examples
These examples demonstrate common PDF to PDF API workflows in PHP.
Basic examples
Multiple PDFs to PDF file
<?php require "pdfcrowd.php"; try { // Create an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); // 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", "demo"); $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
Multiple PDFs to PDF in PHP website
<?php require 'pdfcrowd.php'; // The recommended method is POST. if($_SERVER['REQUEST_METHOD'] == 'POST') { try { // Create an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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->getStatusCode()); 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 an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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->getStatusCode()); echo "PDFCrowd Error: {$why}"; } } ?>
Laravel examples
Multiple PDFs to PDF in Laravel
<?php // The recommended method is POST. Route::post('/', function () { try { // Create an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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(); // 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, $why->getStatusCode()) ->header("Content-Type", "text/plain"); } }); ?>
Multiple in-memory PDFs to PDF in Laravel
<?php // The recommended method is POST. Route::post('/', function () { try { // Create an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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 in 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, $why->getStatusCode()) ->header("Content-Type", "text/plain"); } }); ?>
Symfony examples
Multiple PDFs to PDF in Symfony
<?php namespace App\Controller; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Response; class DemoController { // The recommended method is POST. #[Route('/', methods: ['POST'])] public function convert() { try { // Create an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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(); // 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, $why->getStatusCode(), ["Content-Type" => "text/plain"]); } } }
Multiple in-memory PDFs to PDF in Symfony
<?php namespace App\Controller; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\Response; class DemoController { // The recommended method is POST. #[Route('/', methods: ['POST'])] public function convert() { try { // Create an API client instance. $client = new \Pdfcrowd\PdfToPdfClient("demo", "demo"); // 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 in 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, $why->getStatusCode(), ["Content-Type" => "text/plain"]); } } }