NPdf, a native node module for reading, parsing, modifying, and creating pdf documents with PoDoFo.
Still in early development.
NPdf requires:
For usage in AWS Lambda, PoDoFo prebuilt binaries are available here
To install Npdf run
npm i -S n_pdf
TypeDocs available here
Getting started
import {...} from 'npdf'
NPdf includes a definition file for developer happiness. If your editor does not detect the type file import dist/index.d.ts
into your editor of choice.
For Webstorm go to settings > language & frameworks > javascript > libraries and add and select the definitions file.
NPdf wraps PoDoFo::PdfMemDocument class as Document. Document is the core class for reading and modifying existing pdf documents.
Loading a document
import {Document} from 'npdf'
const pdf:Document
try {
pdf = new Document('/path/to/pdf') // if invalid password is thrown set password
} catch(e) {
if(e.message === 'Password required to modify this document') {
pdf.password = 'secret'
}
}
At this point the document has been loaded into memory and we can now make modifications to the document and write back to disk.
To start let's get a page, and rotate it 90 degrees and then save back to disk.
Note: The page definition is defined as an interface, not a concrete class.
import {Document, IPage} from 'npdf'
const pdf = new Document('/path/to/pdf')
/** Pages are zero indexed */
const pageCount = doc.getPageCount()
for(let i = 0; i < pageCount; i++) {
let page:IPage = doc.getPage(i)
page.setRotation(90) // value must be increment of 90 (valid:0,90,180, 270)
}
doc.write('/destination')
And that's all, we've loaded a pdf, iterated through it's pages setting the rotate value to 90 and then written the modified file back to disk.
Check a page for fields
import {Document, IPage} from 'npdf'
const pdf = new Document('/path/to/pdf')
const pageCount = doc.getPageCount()
for(let i = 0; i < pageCount; i++) {
let page:IPage = doc.getPage(i)
let fieldCount = page.getNumFields()
if(fieldCount > 0) {
let fieldsSummary:Array<IFieldInfo> = page.getFields()
}
}
Generated using TypeDoc