Skip to Content
NexPDF

Opening and saving

Open PDFs from files and bytes, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, linearize and copy documents with NexPDF for PHP.

English

License module

Opening PDFs and pictures, saving and copying are part of the viewer module and work without a key. Creating a PDF from HTML, Office or e-book files needs the conversion module (Startup tier and above).

Open a file or bytes

Document::open($path, $password = null) reads a file; Document::openBytes($data, $password = null) takes the bytes you already have as a binary string, for example from an upload or object storage.

use Nexdevit\NexPdf\Document;

$doc = Document::open('contract.pdf');
echo $doc->pageCount(), " pages\n";

$upload = Document::openBytes(file_get_contents($_FILES['pdf']['tmp_name']));

Both accept JPEG, PNG and TIFF pictures too (one page per picture). With the conversion module, Word, Excel and PowerPoint files, EPUB and zipped FictionBook books, XPS files and CBZ comics are detected and converted on open.

A document holds native memory. It is freed when the object is destroyed, or at once with close(); after close() any call throws LogicException, and isClosed() returns true.

Encrypted documents

Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported. Word, Excel and PowerPoint files, e-books and comics are still detected and converted when you pass a password; the password is used only for PDFs.

use Nexdevit\NexPdf\NexPdfException;

try {
    $doc = Document::open('locked.pdf', 's3cret');
} catch (NexPdfException $e) {
    if (in_array($e->getMessage(), ['password required', 'incorrect password'], true)) {
        askTheUserAgain();
    } else {
        throw $e;
    }
}

NexPdf::openBytesWithPassword($data, $password) always opens the bytes as a PDF. Files that are only restricted by permissions (no open password) open without a password. $doc->securityJson() reports the encryption and permissions; see Security.

Create a PDF from other formats

SourceMethodModule
JPEG, PNG, TIFFNexPdf::openImage($data)none
HTMLDocument::fromHtml($html) or NexPdf::fromHtml($html)conversion
Word, Excel, PowerPointNexPdf::openOffice($data)conversion
EPUB, FictionBook, XPS/OpenXPS, CBZNexPdf::openEbook($data)conversion
use Nexdevit\NexPdf\NexPdf;

$scan = NexPdf::openImage(file_get_contents('scan.tiff'));   // one page per TIFF frame
$scan->save('scan.pdf');

$invoice = Document::fromHtml('<h1>Invoice 42</h1><p>Total: 120 EUR</p>');
$invoice->save('invoice.pdf');

To add pictures to an existing document, use insertImagePages($index, $data); see Editing.

Save

save($path) writes the document with every change as a complete new file. saveBytes() returns the same bytes as a binary string instead of writing a file.

$doc->save('out.pdf');

$pdf = $doc->saveBytes();               // e.g. to return from a controller
header('Content-Type: application/pdf');
echo $pdf;

Fast Web View

A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:

$doc->save('web.pdf', linearized: true);   // to a file
$web = $doc->saveLinearized();             // or as bytes

Signed files

Signing returns the finished signed file. Write those bytes as they are: saving the document object again rewrites the file and breaks the signature. See Signatures.

Copy a document

copy() returns an independent document with the same content and changes. Use it as an undo snapshot. clone is not supported and throws LogicException.

$snapshot = $doc->copy();
$doc->deletePage(0);
// ... undo:
$doc->close();
$doc = $snapshot;

On this page