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 Go.
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
nexpdf.Open(path, password...) reads a file; nexpdf.OpenBytes(data, password...) takes the bytes
you already have, for example from an upload or object storage. The password is optional.
doc, err := nexpdf.Open("contract.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
n, _ := doc.PageCount()
fmt.Println(n, "pages")
data, err := os.ReadFile("contract.pdf")
if err != nil {
log.Fatal(err)
}
fromBytes, err := nexpdf.OpenBytes(data)
if err != nil {
log.Fatal(err)
}
defer fromBytes.Close()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(), usually with defer; after Close every
method returns an error. Close is safe to call more than once.
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.
doc, err := nexpdf.Open("locked.pdf", "s3cret")
if err != nil {
var e *nexpdf.Error
if errors.As(err, &e) && (e.Message == "password required" || e.Message == "incorrect password") {
askTheUserAgain()
return
}
log.Fatal(err)
}
defer doc.Close()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
| Source | Function | Module |
|---|---|---|
| JPEG, PNG, TIFF | nexpdf.OpenImage(data) | none |
| HTML | nexpdf.FromHTML(html) | conversion |
| Word, Excel, PowerPoint | nexpdf.OpenOffice(data) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | nexpdf.OpenEbook(data) | conversion |
scan, _ := os.ReadFile("scan.tiff")
images, err := nexpdf.OpenImage(scan) // one page per TIFF frame
if err != nil {
log.Fatal(err)
}
defer images.Close()
images.Save("scan.pdf", false)
invoice, err := nexpdf.FromHTML("<h1>Invoice 42</h1><p>Total: 120 EUR</p>")
if err != nil {
log.Fatal(err)
}
defer invoice.Close()
invoice.Save("invoice.pdf", false)To add pictures to an existing document, use InsertImagePages(index, data); see
Editing.
Save
Save(path, linearized) writes the document with every change as a complete new file.
SaveBytes() returns the same bytes instead of writing a file.
if err := doc.Save("out.pdf", false); err != nil {
log.Fatal(err)
}
pdf, err := doc.SaveBytes() // e.g. to write to an http.ResponseWriter
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/pdf")
w.Write(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", true) // to a file
webBytes, err := doc.SaveLinearized() // or as bytesSigned 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 goroutine:
snapshot, err := doc.Copy()
if err != nil {
log.Fatal(err)
}
doc.DeletePage(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