Opening and saving
Open PDFs from bytes and files, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save and linearize documents with NexPDF for Flutter.
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 bytes or a file
sdk.open(bytes, password:) takes the bytes you already have, for example from an asset, a picker
or a download. sdk.openFile(path, password:) reads a file first and returns a Future.
import 'package:flutter/services.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
final sdk = NexPdfFlutter();
final bytes = (await rootBundle.load('assets/contract.pdf')).buffer.asUint8List();
final doc = sdk.open(bytes);
print('${doc.pageCount} pages');
doc.dispose();
final fromDisk = await sdk.openFile('/path/to/contract.pdf');Both accept JPEG, PNG and TIFF pictures too (one page per picture). They do not convert Office or e-book files; see below.
A document holds native memory. Call dispose() when you are done; after it, any call throws
StateError. A finalizer frees forgotten documents, but not at a predictable time.
Encrypted documents
Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported.
try {
final doc = sdk.open(bytes, password: 's3cret');
} on NexPdfException catch (e) {
if (e.message == 'password required' || e.message == 'incorrect password') {
askTheUserAgain();
} else {
rethrow;
}
}Files that are only restricted by permissions (no open password) open without a password.
nexpdf_security_json reports the encryption and permissions; see
Security. The native reader asks the user
for the password itself.
Create a PDF from other formats
These C functions create a new native document. The Dart NexPdfDocument cannot wrap it, so save
it with nexpdf_save_bytes, free it with nexpdf_free, and open the bytes with sdk.open, using
the helpers from Calling the C API:
| Source | C function | Module |
|---|---|---|
| JPEG, PNG, TIFF | sdk.open(bytes), or nexpdf_open_image(data, len) | none |
| HTML | nexpdf_from_html(html) | conversion |
| Word, Excel, PowerPoint | nexpdf_open_office(data, len) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | nexpdf_open_ebook(data, len) | conversion |
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
import 'nexpdf_ffi.dart';
// NexPdfDocument *nexpdf_open_ebook(const uint8_t *data, size_t len);
final openEbook = nexpdfLib.lookupFunction<Pointer<Void> Function(Pointer<Uint8>, Size),
Pointer<Void> Function(Pointer<Uint8>, int)>('nexpdf_open_ebook');
// NexPdfDocument *nexpdf_from_html(const char *html);
final fromHtml = nexpdfLib.lookupFunction<Pointer<Void> Function(Pointer<Utf8>),
Pointer<Void> Function(Pointer<Utf8>)>('nexpdf_from_html');
final saveBytes = nexpdfLib.lookupFunction<NexPdfBytes Function(Pointer<Void>),
NexPdfBytes Function(Pointer<Void>)>('nexpdf_save_bytes');
final free = nexpdfLib.lookupFunction<Void Function(Pointer<Void>), void Function(Pointer<Void>)>('nexpdf_free');
/// Saves and frees a raw document returned by a C function.
Uint8List saveAndFree(Pointer<Void> raw) {
try {
return takeBytes(saveBytes(raw));
} finally {
free(raw);
}
}
final book = sdk.open(using((a) => saveAndFree(nexpdfCall(() => openEbook(toNative(epubBytes, a), epubBytes.length)))));
final invoice = sdk.open(using((a) => saveAndFree(nexpdfCall(
() => fromHtml('<h1>Invoice 42</h1><p>Total: 120 EUR</p>'.toNativeUtf8(allocator: a))))));The native reader also opens Word, Excel and PowerPoint files directly. For Office input from code, see Conversion.
Save
save() returns the document with every change as a complete new file. Write the bytes with
dart:io, upload them, or share them.
import 'dart:io';
final pdfBytes = doc.save();
await File('${dir.path}/out.pdf').writeAsBytes(pdfBytes);save() throws NexPdfException if the document cannot be written.
Fast Web View
A linearized file shows its first page before the download ends, which helps when PDFs are served
over HTTP. nexpdf_save_linearized has no Dart method:
// NexPdfBytes nexpdf_save_linearized(const NexPdfDocument *doc);
final saveLinearized = nexpdfLib.lookupFunction<NexPdfBytes Function(Pointer<Void>),
NexPdfBytes Function(Pointer<Void>)>('nexpdf_save_linearized');
final webBytes = nexpdfCall(() => takeBytes(saveLinearized(doc.handle)));Signed files
Signing returns the finished signed file. Write those bytes as they are: saving the document again rewrites the file and breaks the signature. See Signatures.
Snapshots
NexPdfDocument 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:
var current = doc;
final snapshot = sdk.open(current.save());
// ... edit current through dart:ffi ...
// undo:
current.dispose();
current = snapshot;Related
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory