0.0.3 • Published 7 years ago

n_pdf v0.0.3

Weekly downloads
5
License
ISC
Repository
github
Last release
7 years ago

N_Pdf

NPdf, a native node module for reading, parsing, modifying, and creating pdf documents with PoDoFo.

Still in early development.

New Features!

  • Get / Set Form Fields

In Progress:

  • Add Images
  • Save to Buffer
  • Add Digital Signature

Installation

Currently tested on Mac and Linux

NPdf has been developed against the PoDoFo trunk or github master( v0.9.6 ). It is recommended that when using NPdf you compile PoDoFo from source. Thankfully this is incredibly simple on OSX and Linux, instructions are provided on the PoDoFo source README.html.

NPdf requires PoDoFo built with dependencies: zlib, openssl, libpng, libtiff, libjpeg, fontconfig, and freetype2

NPdf requires:

For usage in AWS Lambda, PoDoFo prebuilt binaries are available here

To install Npdf run npm i -S n_pdf

ToDo

  • Add build dependencies to project repo
  • Add older Node.js version (v4.x, v6.x) support

Usage

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.

Document

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.

Page

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()
    }
}

PoDoFo Install Instructions: