PHP code example of tudorr89 / pdfcombiner

1. Go to this page and download the library: Download tudorr89/pdfcombiner library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tudorr89 / pdfcombiner example snippets




use PdfCombine\PDFCombiner;

$combiner = new PDFCombiner;

$combiner = new \PdfCombine\PDFCombiner();

$combiner
    ->addFile('/path/to/report.pdf')
    ->addFile('/path/to/appendix.pdf')
    ->save('/path/to/combined.pdf');

$combiner
    ->addFile('/path/to/large.pdf', [1, 5, 7])  // pages 1, 5, 7
    ->addFile('/path/to/summary.pdf')            // all pages
    ->save('/path/to/result.pdf');

$combiner
    ->addFile('/path/to/doc.pdf')
    ->addRaw($pdfBinaryString, 'attachment.pdf')
    ->download('final-report.pdf');

$content = $combiner
    ->addFile('/path/to/doc.pdf')
    ->blob();

// or
$content = $combiner->stream();

use PdfCombine\Contracts\PDFCombinerInterface;

class ReportController
{
    public function combine(PDFCombinerInterface $combiner)
    {
        $combiner
            ->addFile(storage_path('reports/january.pdf'))
            ->addFile(storage_path('reports/february.pdf'))
            ->save(storage_path('reports/q1.pdf'));

        return response()->download(storage_path('reports/q1.pdf'));
    }
}

use PDFCombine;

PDFCombine::addFile('/path/to/a.pdf')
    ->addFile('/path/to/b.pdf')
    ->download('combined.pdf');

$combiner->addFile('/path/to/doc.pdf');                // all pages
$combiner->addFile('/path/to/doc.pdf', [1, 3]);        // pages 1 and 3 only
$combiner->addFile('/path/to/doc.pdf', range(5, 10));  // pages 5 through 10

$combiner->addFiles([
    '/path/to/cover.pdf',
    ['/path/to/body.pdf', [1, 2, 3]],
    '/path/to/back.pdf',
]);

$rawPdf = file_get_contents('https://example.com/remote.pdf');
$combiner->addRaw($rawPdf, 'remote.pdf');

$combiner->addFile('a.pdf')->addFile('b.pdf')->save('combined.pdf');

$combiner->download('quarterly-report.pdf');

> return response()->streamDownload(
>     fn () => print($combiner->stream()),
>     'quarterly-report.pdf',
>     ['Content-Type' => 'application/pdf'],
> );
> 

$pdf = $combiner->stream();
// store in S3, send via email attachment, etc.

$combiner
    ->addFile('doc.pdf', [2, 4])
    ->addFile('summary.pdf');

echo $combiner->getPageCount(); // 2 + all pages of summary.pdf

$combiner->reset();

$combiner->resetConfig();

use PdfCombine\Contracts\PDFCombinerInterface;

$combiner->withOrientation(PDFCombinerInterface::ORIENTATION_PORTRAIT);   // 'P'
$combiner->withOrientation(PDFCombinerInterface::ORIENTATION_LANDSCAPE);  // 'L'

$combiner->withUnit(PDFCombinerInterface::UNIT_MM);
$combiner->withUnit(PDFCombinerInterface::UNIT_CM);
$combiner->withUnit(PDFCombinerInterface::UNIT_IN);
$combiner->withUnit(PDFCombinerInterface::UNIT_PT);

// Named constants
$combiner->withPaperSize(PDFCombinerInterface::SIZE_A4);
$combiner->withPaperSize(PDFCombinerInterface::SIZE_A5);
$combiner->withPaperSize(PDFCombinerInterface::SIZE_A6);
$combiner->withPaperSize(PDFCombinerInterface::SIZE_LETTER);
$combiner->withPaperSize(PDFCombinerInterface::SIZE_LEGAL);

// Or any TCPDF-compatible format string
$combiner->withPaperSize('A3');
$combiner->withPaperSize('Legal');

// Or custom [width, height] array (in the unit defined above)
$combiner->withPaperSize([210, 297]); // A4 in mm

return [
    'orientation' => env('PDFCOMBINE_ORIENTATION', 'P'),
    'unit'        => env('PDFCOMBINE_UNIT', 'mm'),
    'paper_size'  => env('PDFCOMBINE_PAPER_SIZE', 'A4'),
];

$content = $combiner
    ->withOrientation(PDFCombinerInterface::ORIENTATION_LANDSCAPE)
    ->withPaperSize(PDFCombinerInterface::SIZE_A3)
    ->addFile('cover.pdf')
    ->addFile('body.pdf', range(2, 10))
    ->addFile('back.pdf')
    ->stream();

use PdfCombine\Exceptions\PDFCombineException;

try {
    $combiner->addFile('/missing.pdf')->save('/out.pdf');
} catch (PDFCombineException $e) {
    logger()->error('PDF combine failed: ' . $e->getMessage());
}
bash
php artisan vendor:publish --tag=pdfcombine-config