npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdf-form-fill

v0.2.1

Published

PDF form fill with UTF-8 support, using pdftk

Downloads

1,874

Readme

pdf-form-fill

Simple PDF form fill, using PDFtk. Uses field data in xfdf format to help guarantee UTF-8 support.

All operations are non-blocking. Written in a rather ES6-ish style; use on Node 5.x and earlier will require transpiling.

Release as open source under the ISC license.

Installation

  1. Make sure pdftk is available in your environment, preferably at least version 2.02.
  2. npm install pdf-form-fill

Usage

The API is minimal:

  • fields(pdf) will return a Promise resolving with a simple object describing the PDF's available fields.
  • fill(pdf, fields[, options]) will return a promise resolving with a Readable sream of the output PDF. the following options are supported:
    • flatten: Flatten the resulting PDF (default true)
    • info: Info fields to be set in the output, such as CreationTime, ModTime, Title, Author, etc. Time values should be Date objects; all others should be strings.
    • verbose: Print stuff to the console (default false)

On error, the promises returned by both functions will reject with an Error object. For more details, read the source code.

Setting any info values will require spawning a second pdftk instance and writing an intermediate PDF to a temporary file (piping pdf output to pdftk turns out to be rather flaky). This will slow down the processing a bit; use options.verbose to test the execution time on your systems.

Example

const fs = require('fs')
const { fields, fill } = require('pdf-form-fill')

const srcPdf = '...'
const tgtPdf = '...'
const fields = { name1: 'Value 1', checkbox2: 'Yes' }

fields(srcPdf)
  .then(shape => console.log(shape))
  .catch(err => console.error(err))

const output = fs.createWriteStream(tgtPdf)
fill(scrPdf, fields)
  .then(stream => stream.pipe(output))
  .catch(err => console.error(err))

For a more complete example, see the pdf-form-fill-server source code.

Notes

This tool is the product of frustration and incredulity, as none of the other PDF form-filling tools appeared to work for my particular use case, requiring UTF-8 support in a macOS environment. For some reason, that works with xfdf form data, but not with fdf. But then, of course, PDFtk isn't able to read that from stdin (unlike fdf, of course), so it needs to be written to a temporary file for it.

Some aspects of this code were inspired by pdffiller-stream, but all the code was written from scratch for this.