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-fill-form

v5.1.0

Published

Fill PDF forms and return rendered PDF in buffer. No temporary files created.

Downloads

1,386

Readme

PDF Fill Form (pdf-fill-form) is Node.js native C++ library for filling PDF forms. Created PDF file is returned back as Node.js Buffer object for further processing or saving - whole process is done in memory. Library offers methods to return filled PDF also as PDF file where pages are converted to images.

Libary uses internally Poppler QT5 for PDF form reading and filling. Cairo is used for PDF creation from page images (when parameter { "save": "imgpdf" } is used).

Features

  • Supports reading and writing the following PDF form field types: TextField, Checkbox, Radio button

  • You can write following files:

    • PDF
    • PDF where pages are converted to images
  • All the work is done in memory - no temporary files created

  • Results are returned in Node.js Buffer -object

  • Not using the PDFtk -executable - instead we use the Poppler library

Examples

Using promises

Read from file

var pdfFillForm = require('pdf-fill-form');

pdfFillForm.read('test.pdf')
.then(function(result) {
    console.log(result);
}, function(err) {
	console.log(err);
});

Read from file buffer

var pdfFillForm = require('pdf-fill-form');

pdfFillForm.readBuffer(fs.readFileSync('test.pdf'))
.then(function(result) {
    console.log(result);
}, function(err) {
	console.log(err);
});

Write from file

var pdfFillForm = require('pdf-fill-form');
var fs = require('fs');

pdfFillForm.write('test.pdf', { "myField": "myField fill value" }, { "save": "pdf", 'cores': 4, 'scale': 0.2, 'antialias': true } )
.then(function(result) {
	fs.writeFile("test123.pdf", result, function(err) {
		if(err) {
	   		return console.log(err);
	   	}
	   	console.log("The file was saved!");
	}); 
}, function(err) {
  	console.log(err);
});

Write from file buffer

var pdfFillForm = require('pdf-fill-form');
var fs = require('fs');

pdfFillForm.writeBuffer(fs.readFileSync('test.pdf'), { "myField": "myField fill value" }, { "save": "pdf", 'cores': 4, 'scale': 0.2, 'antialias': true } )
.then(function(result) {
	fs.writeFile("test123.pdf", result, function(err) {
		if(err) {
	   		return console.log(err);
	   	}
	   	console.log("The file was saved!");
	}); 
}, function(err) {
  	console.log(err);
});

Using callbacks

To read all form fields:

var pdfFillForm = require('pdf-fill-form');

var pdfFields = pdfFillForm.readSync('test.pdf');
console.log(pdfFields);

To write form fields (synchronous) to PDF:

var pdfFillForm = require('pdf-fill-form');
var fs = require('fs');

// Use here the field names you got from read
var pdf = pdfFillForm.writeSync('test.pdf', 
	{ "myField": "myField fill value" }, { "save": "pdf" } );
fs.writeFileSync('filled_test.pdf', pdf);

To write form fields (aynchronous) to PDF:

var pdfFillForm = require('pdf-fill-form');
var fs = require('fs');

// Use here the field names you got from read
pdfFillForm.writeAsync('test.pdf', 
	{ "myField": "myField fill value" }, { "save": "pdf" }, 
	function(err, pdf) {
		fs.writeFile("filled_test.pdf", pdf, function(err){});
	}
);

To write form fields to PDF where pages are converted to images:

Use parameter { "save": "imgpdf" }

Notes about using radio buttons (@mttchrry)

Just set the radio button field Value to the caption of the item you want to select.

For example if you have a radio button for gender called "Gender1" with options captioned as "Male" or "Female" then passing in the field {"Gender1": "Male"} will select the male radio button, as expected.

Installation

macOS

Preferable method to install library dependencies is via Homebrew

First, make sure the XCode Command Line Tools are installed correctly

xcode-select --install

Also make sure pkg-config is installed correctly

brew install pkg-config

Then install the Poppler and Cairo formulae using brew

NB: Poppler contains all the necessary qt version 5 depencencies.

brew install poppler cairo

Also export environment variables used by the compiler during the pdf-fill-form installation

export LDFLAGS=-L/usr/local/opt/qt/lib
export CPPFLAGS=-I/usr/local/opt/qt/include
export PKG_CONFIG_PATH=/usr/local/opt/qt/lib/pkgconfig

NB: You may also append the above export commands to your ~/.bash_profile or ~/.zshrc file for persistency.

Finally, install the package using npm or yarn

$ npm install pdf-fill-form
$ yarn add pdf-fill-form

Troubleshooting

Homebrew users who get error regarding xcb-shm
The fix is to add this to your bash profile / environment: export PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig

Linux - Ubuntu (trusty)

$ sudo apt-get install libpoppler-qt5-dev libcairo2-dev
$ npm install pdf-fill-form

Linux - Debian (jessie)

To be sure to have the required packages, re-synchronize the package index files from their sources :

$ sudo apt-get update

Then install packages :

$ sudo apt-get install libcairo2-dev libpoppler-qt5-dev 
$ npm install pdf-fill-form

I mostly recommand to install this package to have better support with fonts :

$ sudo apt-get install poppler-data

Linux - CENTOS 8

First, enable the PowerTools repository:

$ yum config-manager --set-enabled PowerTools

Install the dependencies :

$ dnf install poppler-qt5-devel cairo cairo-devel

Then install the package using npm or yarn :

$ npm install pdf-fill-form
$ yarn add pdf-fill-form

Windows

Not currently supported

Todo

  • Tests
  • Refactoring
  • Support for other form field types than CheckBox, Radio button and TextField

Changelog

v5.1.0 (15.3.2021)

  • Fix the v8 namespace (by @gedaiu)

v5.0.0 (6.11.2019)

  • New options startPage and endPage for the imgpdf feature to limit which pages are generated. Page numbers indexed from 0 (by Derick Naef @ochimo).
  • Allow source to come in a a buffer an not a filename (by Dustin Harmon @dfharmon)
  • Support for Node 13 (by @florianbepunkt)

v4.1.0 (10.9.2018)

  • Support for Node 10 (by @florianbepunkt)

v4.0.0 (14.12.2017)

  • #45 Set radio button "value" to the poppler button state (by Albert Astals Cid @tsdgeos)
  • Added feature allowing for parallelization of the imgpdf feature, also allows for settings scale and whether antialiasing should be used (by Albert Astals Cid @tsdgeos).

v3.3.0 (14.12.2017)

  • #49 Set radio button "value" to the poppler button state (by Mihai Saru @MitzaCoder)

v3.2.0 (24.5.2017)

  • Support for radio buttons (by Matt Cherry @mttchrry)

Authors

Contibutors

  • Ethan Goldblum
  • Tyler Iguchi
  • Rob Davarnia
  • Fabrice Ongenae
  • Juwan Yoo
  • Andreas Gruenbacher
  • Andrei Dracea
  • Emil Sedgh
  • Matt Cherry
  • Mario Ferreira
  • Mihai Saru @MitzaCoder
  • Albert Astals Cid @tsdgeos
  • Florian Bischoff @florianbepunkt
  • Derick Naef @ochimo
  • Dustin Harmon @dfharmon
  • Szabo Bogdan @gedaiu

License

MIT

NOTE ABOUT LIBRARY DEPENDENCIES! Poppler has GPL license. Cairo has LGPL.