Skip to content

Novis\Pdf\Pages::of

Chooses pages of an existing PDF file, so Pages::merge can put them into a new file.

Pages::of takes a PDF file and a list of page numbers. The file is bytes, an open Core\IO\File or an uploaded Core\Request\Part. Pages count from 1. The new file has the pages in the order of the list. A page listed twice is in the new file twice, and a page not in the list is left out. Without a list, every page is chosen, in its own order.

Good to know: Pages::of does not read the file. Pages::merge reads it, so the file must still be open then. A page number past the end, or a file that is not a PDF, throws an error in merge.

The examples below reorder the pages of a file, take every page of one file and one page of another, and take the first page of each invoice for a summary.

Parameters

  • $pdfCore\Bytes::Source
  • $pages?array<uint>default null

Return value

Returns Pages.

Examples

Reorder the pages

<?nvs
// Shows how to put the pages of a PDF file in a new order and leave one page out.
use Novis\Pdf\Document;
use Novis\Pdf\Pages;
use Novis\Pdf\Reader;
// A report of four pages.
var $report = Document::fromHtml(
'<style>p { break-after: page }</style>'
. '<p>Summary</p><p>Sales</p><p>Costs</p><p>Notes</p>',
)->render()->bytes;
// The new file has page 4 first, then pages 1 and 2. Page 3 is left out.
var $pdf = Pages::merge([Pages::of($report, [4, 1, 2])]);
var $reader = Reader::open($pdf);
for (uint $page = 1; $page <= $reader->pageCount; $page++) {
echo 'page ', $page, ': ', $reader->text($page), "\n";
}
Output
page 1: Notes
page 2: Summary
page 3: Sales

Every page or one page twice

<?nvs
// Shows the pages that `Pages::of` chooses with no list, and with a page listed twice.
use Novis\Pdf\Document;
use Novis\Pdf\Pages;
use Novis\Pdf\Reader;
var $letter = Document::fromHtml('<style>p { break-after: page }</style><p>Letter one</p><p>Letter two</p>')->render()->bytes;
var $form = Document::fromHtml('<p>Return form</p>')->render()->bytes;
// Without a list, every page of the letter is chosen.
// The return form is listed twice, so the customer gets two copies.
var $pdf = Pages::merge([Pages::of($letter), Pages::of($form, [1, 1])]);
var $reader = Reader::open($pdf);
for (uint $page = 1; $page <= $reader->pageCount; $page++) {
echo 'page ', $page, ': ', $reader->text($page), "\n";
}
Output
page 1: Letter one
page 2: Letter two
page 3: Return form
page 4: Return form

The first page of each invoice

<?nvs
// Shows how to make one file with the first page of each invoice, for a monthly summary.
use Novis\Pdf\Document;
use Novis\Pdf\Pages;
use Novis\Pdf\Reader;
// Each invoice has a first page with the total and a second page with the terms.
array<int> $numbers = [1042, 1043, 1044];
array<bytes> $invoices = [];
foreach ($numbers as int $number) {
$invoices[] = Document::fromHtml(
'<style>h2 { break-before: page }</style>'
. "<h1>Invoice {$number}</h1><h2>Terms</h2><p>Pay within 14 days.</p>",
)->render()->bytes;
}
// `[1]` chooses page 1 of each file.
array<Pages> $firstPages = [];
foreach ($invoices as bytes $invoice) {
$firstPages[] = Pages::of($invoice, [1]);
}
var $reader = Reader::open(Pages::merge($firstPages));
echo $reader->pageCount, " pages\n";
for (uint $page = 1; $page <= $reader->pageCount; $page++) {
echo 'page ', $page, ': ', $reader->text($page), "\n";
}
Output
3 pages
page 1: Invoice 1042
page 2: Invoice 1043
page 3: Invoice 1044