Opening and saving
Open PDFs from base64, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save and linearize documents with NexPDF for React Native.
License module
Opening PDFs and pictures and saving 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 document
NexPdf.open(base64, password?) takes the file as a base64 string and resolves to a PdfDocument.
The SDK takes no file paths: read the file with your file-system library's base64 function, or use
the base64 you already have from a picker, a download or your server.
import { NexPdf } from '@nexdevit/nexpdf-react-native';
const doc = await NexPdf.open(base64Pdf);
try {
console.log(await doc.pageCount(), 'pages');
} finally {
await doc.close();
}NexPdf.open looks at the start of the data and picks the right call:
| Input | Opened with | Module |
|---|---|---|
NexPdf.api.openBytes, or openBytesWithPassword when you pass a password | none | |
| JPEG, PNG, TIFF | NexPdf.api.openBytes (one page per picture) | none |
| Word, Excel, PowerPoint | NexPdf.api.openOffice | conversion |
| EPUB, zipped FictionBook, XPS/OpenXPS, CBZ | NexPdf.api.openEbook | conversion |
A document holds native memory until you call await doc.close(). After close(), any call on it
rejects with unknown or closed document handle.
Encrypted documents
Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported.
import { NexPdf, NexPdfError } from '@nexdevit/nexpdf-react-native';
try {
const doc = await NexPdf.open(base64Pdf, 's3cret');
} catch (e) {
if (e instanceof NexPdfError && (e.message === 'password required' || e.message === 'incorrect password')) {
askTheUserAgain();
} else {
throw e;
}
}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
| Source | Function | Module |
|---|---|---|
| JPEG, PNG, TIFF | NexPdf.api.openImage(base64) | none |
| HTML | NexPdf.fromHtml(html) | conversion |
| Word, Excel, PowerPoint | NexPdf.api.openOffice(base64) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | NexPdf.api.openEbook(base64) | conversion |
const scan = await NexPdf.api.openImage(tiffBase64); // one page per TIFF frame
const scanPdf = await scan.saveBytes();
await scan.close();
const invoice = await NexPdf.fromHtml('<h1>Invoice 42</h1><p>Total: 120 EUR</p>');
const invoicePdf = await invoice.saveBytes();
await invoice.close();To add pictures to an existing document, use insertImagePages(index, base64); see
Editing.
Save
saveBytes() resolves to the document with every change, as a complete new file in base64. Write
it with your file-system library, upload it, or hand it to a share sheet.
const pdfBase64 = await doc.saveBytes();Fast Web View
A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:
const webBase64 = await doc.saveLinearized();Signed files
Signing resolves to the finished signed file. Keep that base64 as it is: saving the document again rewrites the file and breaks the signature. See Signatures.
Snapshots
PdfDocument has no copy method. To keep an undo snapshot, save the document and open the bytes
again; the new document is independent of the first one:
let current = doc;
const snapshot = await NexPdf.open(await current.saveBytes());
await current.deletePage(0);
// ... undo:
await current.close();
current = snapshot;Related
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory
- Platform notes: the cost of large files