Novis\Pdf\Reader::open
Novis\Pdf\Reader::open(Core\Bytes::Source $pdf): ReaderNovis\Pdf\Reader::open( Core\Bytes::Source $pdf, ): Reader
Opens an existing PDF file and reads its page count, its metadata and its attached files.
Reader::open takes a PDF file as bytes, an open Core\IO\File or an uploaded Core\Request\Part, and returns a Reader. pageCount is the number of pages. metadata has the title, author, subject, keywords, creator and producer of the file. Each of them is null when the file does not have it. The methods attachments, invoiceXml and text read the rest of the file.
A file with a password, or one that is not a PDF file, throws a ParseError. Parts of a PDF file are often compressed. A part that unpacks to more than 64 MB, or to more than 1000 times its packed size, throws a ParseError. The attached files together may also unpack to at most 64 MB. The server's [limits] max_decompressed and [limits] max_decompression_ratio set these two numbers.
Good to know: Reader::open reads the whole file and its attached files at once. The text of a page is read only when you call text.
The examples below read the title of an upload, show the error for an encrypted file, and check the page count of each upload.
Parameters
$pdfCore\Bytes::Source
Return value
Returns Reader.
Examples
Read an uploaded file
<?nvs// Shows how to read the page count and the title of an uploaded PDF file.use Novis\Pdf\Document;use Novis\Pdf\Reader;
// The uploaded file. Here the program renders it first, so the example has a file to read.var $upload = Document::fromHtml( "<html><head><title>Order 1042</title><meta name=\"author\" content=\"Shop\"></head>" . "<body><p>Order 1042</p><p style=\"break-before: page\">Delivery note</p></body></html>",) ->render()->bytes;
// `Reader::open` reads the file once. This prints `2 pages`, then the title and the author.var $reader = Reader::open($upload);echo $reader->pageCount, " pages\n";echo 'title: ', $reader->metadata->title, "\n";echo 'author: ', $reader->metadata->author, "\n";
// A value the file does not have is `null`.echo 'subject: ', $reader->metadata->subject ?? 'none', "\n";2 pagestitle: Order 1042author: Shopsubject: noneAn encrypted or broken file
<?nvs// Shows the error for an uploaded file with a password, and for a file that is not a PDF.use Novis\Pdf\Document;use Novis\Pdf\Reader;
var $locked = Document::fromHtml('<p>Salary list</p>')->withPassword('example-password')->render()->bytes;
// A file with a password throws a `ParseError`.try { Reader::open($locked); echo "opened\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 { Reader::open('Hello' as bytes); echo "opened\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 parseAccept short files only
<?nvs// Shows how to check the page count of each upload before the shop stores it.use Novis\Pdf\Document;use Novis\Pdf\Reader;
uint $maxPages = 3;
// Two uploads: a receipt with one page and a catalogue with five pages.var $receipt = Document::fromHtml('<p>Receipt 1042</p>')->render()->bytes;var $catalogue = Document::fromHtml( '<style>p { break-after: page }</style>' . '<p>Bikes</p><p>Helmets</p><p>Lights</p><p>Locks</p><p>Bells</p>',)->render()->bytes;
// This loop prints `accepted` for the receipt and `too long` for the catalogue.array<bytes> $uploads = [$receipt, $catalogue];foreach ($uploads as bytes $upload) { var $reader = Reader::open($upload); if ($reader->pageCount > $maxPages) { echo 'page count ', $reader->pageCount, ": too long\n"; } else { echo 'page count ', $reader->pageCount, ": accepted\n"; }}page count 1: acceptedpage count 5: too long