Novis\Pdf\Pages::merge
Novis\Pdf\Pages::merge(array<Pages> $parts): tainted bytesNovis\Pdf\Pages::merge( array<Pages> $parts, ): tainted bytes
Puts the chosen pages of several PDF files into one new PDF file.
Pages::merge takes a list of parts. Each part is a Pages::of with a file and the pages to take from it. The new file has the pages of the first part, then the pages of the second part, and so on. You can add terms to a contract, split a file, or take pages out of it.
A page number past the end of its file throws a LogicError. A file that is encrypted, or that is not a PDF file, throws a ParseError. A compressed part that is larger than the [limits] in the server's configuration also throws a ParseError. An empty list of parts also throws an error.
The result is tainted bytes, because a PDF file may be an upload. Write tainted bytes on the variable that keeps it.
Good to know: two merges of the same parts give the same bytes. A test can compare the new file with a saved copy.
The examples below add the terms to a contract on a letterhead, show the error for a page past the end, and show the error for an encrypted file.
Parameters
$partsarray<Pages>
Return value
Returns tainted bytes.
Changelog
| Version | Change |
|---|---|
0.0.6 | Image::encode, the pixels of Image::raw, each file of Image::encodeVariants and the result of Pages::merge are tainted bytes. An image or a PDF file may be an upload. Font::fromBytes takes tainted bytes. |
Examples
A contract with its terms
<?nvs// Shows a contract on a letterhead PDF, with the terms of the shop added at the end.use Novis\Pdf\Document;use Novis\Pdf\Pages;use Novis\Pdf\Reader;
// The letterhead and the terms are PDF files that the shop already has.var $letterhead = Document::fromHtml('<p>Shop, 1 Example Street</p>')->render()->bytes;var $terms = Document::fromHtml( '<style>p { break-after: page }</style>' . '<p>Terms, part 1</p><p>Terms, part 2</p>',)->render()->bytes;
// `background-image` draws the letterhead behind every page of the contract.var $contract = Document::fromHtml( '<style>@page { background-image: url(letterhead.pdf) }</style>' . '<h1>Contract</h1><p>Ada Lovelace rents a bike for one week.</p>',) ->withAsset('letterhead.pdf', $letterhead) ->render() ->bytes;
// The new file is the contract and then every page of the terms.var $reader = Reader::open(Pages::merge([Pages::of($contract), Pages::of($terms)]));for (uint $page = 1; $page <= $reader->pageCount; $page++) { echo 'page ', $page, ': ', Core\Str::replace($reader->text($page), "\n", ' / '), "\n";}page 1: Shop, 1 Example Street / Contract / Ada Lovelace rents a bike for one week.page 2: Terms, part 1page 3: Terms, part 2A page past the end
<?nvs// Shows the error for a page number that the file does not have.use Novis\Pdf\Document;use Novis\Pdf\Pages;
var $receipt = Document::fromHtml('<p>Receipt 1042</p>')->render()->bytes;
// The receipt has one page, so page 2 throws a `LogicError`.try { Pages::merge([Pages::of($receipt, [1, 2])]); echo "merged\n";} catch (LogicError $error) { echo 'LogicError: ', $error->message, "\n";}LogicError: page 2 is outside the PDF, which has 1 pageAn encrypted or broken file
<?nvs// Shows the error for a file with a password, and for a file that is not a PDF.use Novis\Pdf\Document;use Novis\Pdf\Pages;
var $invoice = Document::fromHtml('<p>Invoice 1042</p>')->render()->bytes;var $locked = Document::fromHtml('<p>Salary list</p>')->withPassword('example-password')->render()->bytes;
// A file with a password throws a `ParseError`.try { Pages::merge([Pages::of($invoice), Pages::of($locked)]); echo "merged\n";} catch (ParseError $encrypted) { echo 'ParseError: ', $encrypted->message, "\n";}
// These bytes are text and not a PDF file, so they throw a `ParseError` too.try { Pages::merge([Pages::of($invoice), Pages::of('Hello' as bytes)]); echo "merged\n";} catch (ParseError $broken) { echo 'ParseError: ', $broken->message, "\n";}ParseError: the PDF is encrypted. Only a PDF without encryption can be read, merged or placedParseError: the PDF document does not parse