Complete PHP examples for common Image to PDF tasks. See the PHP SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each PHP example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the PDF result. Stream examples use a binary file stream as their destination.
Upload an existing logo.png file.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->convertFileToFile('logo.png', 'logo.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $pdf = $client->convertFile('logo.png'); // binary string echo strlen($pdf), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $output_stream = fopen('logo.pdf', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertFileToStream('logo.png', $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Fetch an image from its URL.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->convertUrlToFile('https://your-server.com/logo.png', 'logo.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $pdf = $client->convertUrl('https://your-server.com/logo.png'); // binary string echo strlen($pdf), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $output_stream = fopen('logo.pdf', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertUrlToStream( 'https://your-server.com/logo.png', $output_stream ); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $image_data_1 = file_get_contents('logo.png'); // binary string if ($image_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $client->convertRawDataToFile($image_data_1, 'logo.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $image_data_1 = file_get_contents('logo.png'); // binary string if ($image_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $pdf = $client->convertRawData($image_data_1); // binary string echo strlen($pdf), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $output_stream = fopen('logo.pdf', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $image_data_1 = file_get_contents('logo.png'); // binary string if ($image_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $client->convertRawDataToStream($image_data_1, $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Open logo.png as a binary input stream and save the result to logo.pdf.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $input_stream = fopen('logo.png', 'rb'); if ($input_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertStreamToFile($input_stream, 'logo.pdf'); } finally { fclose($input_stream); }
These examples use scan.png, a 1224 × 1584 pixel image with text, a chart, and a uniform white border. The rotation example uses its sideways variant, scan-sideways.png.
Fit the whole image onto A4 portrait or Letter landscape paper with 12mm margins and 150 DPI. The fit mode preserves the image's aspect ratio. Setting a page size without it can clip a larger image.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(150); $client->setMarginBottom('12mm'); $client->setMarginLeft('12mm'); $client->setMarginRight('12mm'); $client->setMarginTop('12mm'); $client->setPageSize('A4'); $client->setPrintPageMode('fit'); $client->convertFileToFile('scan.png', 'a4.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(150); $client->setMarginBottom('12mm'); $client->setMarginLeft('12mm'); $client->setMarginRight('12mm'); $client->setMarginTop('12mm'); $client->setOrientation('landscape'); $client->setPageSize('Letter'); $client->setPrintPageMode('fit'); $client->convertFileToFile('scan.png', 'landscape.pdf');
Fit scan.png onto a 4 × 6 inch page and save custom-page.pdf. Preserve the aspect ratio and fill the unused page area with a pale blue background.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(150); $client->setPageBackgroundColor('EEF2F6'); $client->setPageHeight('6in'); $client->setPageWidth('4in'); $client->setPrintPageMode('fit'); $client->convertFileToFile('scan.png', 'custom-page.pdf');
Interpret scan.png at 300 pixels per inch without setting a fixed page size. Its 1224 × 1584 pixels produce a 4.08 × 5.28 inch page in print-size.pdf, with the original image pixels retained.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(300); $client->convertFileToFile('scan.png', 'print-size.pdf');
Extract the chart from scan.png into chart.pdf, or remove its uniform outer border and save trimmed.pdf.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match the source image pixels; adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setCropAreaHeight('384px'); $client->setCropAreaWidth('1024px'); $client->setCropAreaX('96px'); $client->setCropAreaY('370px'); $client->setDpi(96); $client->convertFileToFile('scan.png', 'chart.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(150); $client->setRemoveBorders(true); $client->convertFileToFile('scan.png', 'trimmed.pdf');
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.pdf.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(150); $client->setRotate('90'); $client->convertFileToFile('scan-sideways.png', 'upright.pdf');
Render scan.png onto A4 paper at 72 DPI and save compact.pdf. Keep the same fit and 12mm margins as the 150 DPI A4 example above, while reducing resolution for screen viewing. Smaller files come at the cost of fine detail.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\ImageToPdfClient('demo', 'demo'); $client->setDpi(72); $client->setMarginBottom('12mm'); $client->setMarginLeft('12mm'); $client->setMarginRight('12mm'); $client->setMarginTop('12mm'); $client->setPageSize('A4'); $client->setPrintPageMode('fit'); $client->convertFileToFile('scan.png', 'compact.pdf');