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 Rust.
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) reads a file; Document::from_bytes(data) takes the bytes you already have,
for example from an upload or object storage.
use nexpdf_sdk::Document;
fn main() -> nexpdf_sdk::Result<()> {
let doc = Document::open("contract.pdf")?;
println!("{} pages", doc.page_count()?);
let data = std::fs::read("contract.pdf")?;
let doc = Document::from_bytes(&data)?;
println!("{} pages", doc.page_count()?);
Ok(())
}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. nexpdf_sdk::zip_kind(&data) tells which route a buffer takes:
ZipKind::NotZip (a PDF or a picture), ZipKind::Office or ZipKind::Ebook. It only reads the
entry names of the ZIP and never decompresses anything.
A document holds native memory and is freed when it is dropped. A missing file is Error::Io; a
file the engine cannot open is Error::Engine.
Encrypted documents
Document::open_with_password(path, password) and Document::from_bytes_with_password(data, password) take the user (open) password or the owner password. RC4, AES-128 and AES-256 files are
supported. Office files, books and comics carry no PDF password: they are converted and the
password is ignored.
use nexpdf_sdk::{Document, Error};
let doc = match Document::open_with_password("locked.pdf", "s3cret") {
Ok(doc) => doc,
Err(Error::Engine(m)) if m == "password required" || m == "incorrect password" => {
return ask_the_user_again();
}
Err(e) => return Err(e),
};Files that are only restricted by permissions (no open password) open with Document::open.
doc.security_json() reports the encryption and permissions; see Security.
Create a PDF from other formats
| Source | Function | Module |
|---|---|---|
| JPEG, PNG, TIFF | Document::open_image(data) | none |
| HTML | Document::from_html(html) | conversion |
| Word, Excel, PowerPoint | Document::open_office(data) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | Document::open_ebook(data) | conversion |
let scan = Document::open_image(&std::fs::read("scan.tiff")?)?; // one page per TIFF frame
scan.save("scan.pdf")?;
let invoice = Document::from_html("<h1>Invoice 42</h1><p>Total: 120 EUR</p>")?;
invoice.save("invoice.pdf")?;To add pictures to an existing document, use insert_image_pages(index, data); see
Editing.
Save
save(path) writes the document with every change as a complete new file. save_bytes() returns the
same bytes as a Vec<u8> instead of writing a file.
doc.save("out.pdf")?;
let pdf_bytes = doc.save_bytes()?; // e.g. to return from a web handlerFast Web View
A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:
doc.save_linearized_to("web.pdf")?;
let web_bytes = doc.save_linearized()?;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
try_clone() returns an independent document with the same content and changes. Use it as an undo
snapshot, or to hand work to another thread:
let snapshot = doc.try_clone()?;
doc.delete_page(0)?;
// ... undo:
doc = snapshot; // the edited document is dropped and freedRelated
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory