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.
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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 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; } ?>
<?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}\n"); throw $why; } ?>
<?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}\n"); throw $why; } ?>
<?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 require 'pdfcrowd.php'; // The recommended method is POST. if($_SERVER['REQUEST_METHOD'] == 'POST') { 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. $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}"; } } ?>
<?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", "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}"; } } ?>
<?php // The recommended method is POST. Route::post('/', function () { 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(); // 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"); } }); ?>
<?php // The recommended method is POST. Route::post('/', function () { 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 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->getMessage(), $why->getCode()) ->header("Content-Type", "text/plain"); } }); ?>
<?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 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(); // 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"]); } } }
<?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 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 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->getMessage(), $why->getCode(), ["Content-Type" => "text/plain"]); } } }