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 Python.
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=None) reads a file; Document.from_bytes(data, password=None) takes
the bytes you already have, for example from an upload or object storage. nexpdf.open(path) is a
shorthand for Document.open.
import nexpdf
with nexpdf.Document.open("contract.pdf") as doc:
print(len(doc), "pages")
with open("contract.pdf", "rb") as f:
data = f.read()
with nexpdf.Document.from_bytes(data) as doc:
print(doc.page_count())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. Close it with close() or a with block; after close() any call
raises ValueError.
Encrypted documents
Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported.
try:
doc = nexpdf.Document.open("locked.pdf", password="s3cret")
except nexpdf.NexPdfError as e:
if str(e) in ("password required", "incorrect password"):
ask_the_user_again()
else:
raiseFiles that are only restricted by permissions (no open password) open without a password.
doc.security_json() reports the encryption and permissions; see Security.
Create a PDF from other formats
| Source | Function | Module |
|---|---|---|
| JPEG, PNG, TIFF | nexpdf.open_image(data) | none |
| HTML | nexpdf.from_html(html) | conversion |
| Word, Excel, PowerPoint | nexpdf.open_office(data) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | nexpdf.open_ebook(data) | conversion |
with open("scan.tiff", "rb") as f:
with nexpdf.open_image(f.read()) as doc: # one page per TIFF frame
doc.save("scan.pdf")
with nexpdf.from_html("<h1>Invoice 42</h1><p>Total: 120 EUR</p>") as doc:
doc.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 instead of writing a file.
doc.save("out.pdf")
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("web.pdf", linearized=True)
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
copy() returns an independent document with the same content and changes. Use it as an undo
snapshot, or to hand work to another thread:
snapshot = doc.copy()
doc.delete_page(0)
# ... undo:
doc.close()
doc = snapshotRelated
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory