Merge PDFs and Write Footnotes
This demo adds the filename as a footnote to the merged pages.
PHP
<?php
use setasign\SetaPDF2\Core\Font\TrueType\Subset;
use setasign\SetaPDF2\Core\Text\TextBlock;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\Merger\Merger;
// load and register the autoload function
require_once __DIR__ . '/../../../../../bootstrap.php';
$files = glob($assetsDirectory . '/pdfs/tektown/invoices/1*.pdf');
$files = array_merge($files, glob($assetsDirectory . '/pdfs/misc/rotated/*.pdf'));
$files = array_merge($files, glob($assetsDirectory . '/pdfs/misc/boxes/*.pdf'));
$paths = displayFiles($files, true, true);
// create a merger instance
$merger = new Merger();
// we need some variable to record page numbers and the added file path
$currentPage = 1;
$pagesToFiles = [];
// iterate through paths...
foreach ($paths as $path) {
$pageCount = $merger->getPageCount($path);
$pagesToFiles[$currentPage] = [$currentPage + $pageCount, $path];
$currentPage += $pageCount;
// ... add them to the merger instance
$merger->addFile($path);
}
// merge
$merger->merge();
// get access to the document instance
$document = $merger->getDocument();
// we need a font instance
$font = new Subset(
$document,
$assetsDirectory . '/fonts/DejaVu/ttf/DejaVuSans.ttf'
);
// access the pages
$pages = $document->getCatalog()->getPages();
// re-iterate through the merged files and pages
foreach ($pagesToFiles as $pageNo => list($nextPage, $path)) {
for (; $pageNo < $nextPage; $pageNo++) {
// access the page
$page = $pages->getPage($pageNo);
// ensure a clean graphic state
$page->getStreamProxy()->encapsulateExistingContentInGraphicState();
// get access to the pages canvas
$canvas = $page->getCanvas();
$canvas->saveGraphicState();
// let's normalize the rotation and the origin
$canvas->normalizeRotationAndOrigin($page->getRotation(), $page->getBoundary());
// create a text block
$textBlock = new TextBlock($font, 5);
$textBlock->setText(basename($path));
// and draw it onto the canvas
$textBlock->draw($canvas, $page->getWidth() - $textBlock->getWidth() - 5, 5);
$canvas->restoreGraphicState();
}
}
// set a writer instance
$document->setWriter(new HttpWriter('merged-with-footnotes.pdf', true));
// and save the result to the writer
$document->save()->finish();
