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

pdffiller-keepfields

v1.1.0

Published

Take an existing PDF Form and data and PDF Filler will create a new PDF with all given fields populated.

Downloads

19

Readme

PDF Filler (Node.js)

NPM

A node.js PDF form field data filler and FDF generator toolkit. This essentially is a wrapper around the PDF Toolkit library PDF ToolKit.

NOTE: pdffiller failed to keep data if it was already included on a form. I added some logic to retain this data if it exists and released pdffiller-keepfields as a substitute.

Quick start

npm install pdffiller-keepfields --save

Import the module using:

var pdfFiller = require('pdffiller-keepfields');

// ...

Examples

Fill PDF with existing FDF Data

var pdfFiller   = require('pdffiller-keepfields');

var sourcePDF = "test/test.pdf";
var destinationPDF =  "test/test_complete.pdf";
var data = {
    "last_name" : "John",
    "first_name" : "Doe",
    "date" : "Jan 1, 2013",
    "football" : "Off",
    "baseball" : "Yes",
    "basketball" : "Off",
    "hockey" : "Yes",
    "nascar" : "Off"
};

pdfFiller.fillForm( sourcePDF, destinationPDF, data, function(err) {
    if (err) throw err;
    console.log("In callback (we're done).");
});

This will take the test.pdf, fill the fields with the data values and create a complete filled in PDF (test_filled_in.pdf). Note that the resulting PDF will be read-only.

Alternatively,


var shouldFlatten = false;

pdfFiller.fillFormWithFlatten( sourcePDF, destinationPDF, data, shouldFlatten, function(err) {
    if (err) throw err;
    console.log("In callback (we're done).");
})

Calling fillFormWithFlatten() with shouldFlatten = false will leave any unmapped fields still editable, as per the pdftk command specification.

Generate FDF Template from PDF

var pdfFiller   = require('pdffiller-keepfields');

var sourcePDF = "test/test.pdf";

// Override the default field name regex. Default: /FieldName: ([^\n]*)/
var nameRegex = null;  

var FDF_data = pdfFiller.generateFDFTemplate( sourcePDF, nameRegex, function(err, fdfData) {
    if (err) throw err;
    console.log(fdfData);
});

This will print out this

{
    "last_name" : "",
    "first_name" : "",
    "date" : "",
    "football" : "",
    "baseball" : "",
    "basketball" : "",
    "hockey" : "",
    "nascar" : ""
};

Map form fields to PDF fields

var pdfFiller = require('pdffiller-keepfields');

var convMap = {
    "lastName": "last_name",
    "firstName": "first_name",
    "Date": "date",
    "footballField": "football",
    "baseballField": "baseball",
    "bballField": "basketball",
    "hockeyField": "hockey",
    "nascarField": "nascar"
};

var fieldJson = [
    {
        "title" : "lastName",
        "fieldfieldType": "Text",
        "fieldValue": "John"
    },
    {
        "title" : "firstName",
        "fieldfieldType": "Text",
        "fieldValue": "Doe"
    },
    {
        "title" : "Date",
        "fieldType": "Text",
        "fieldValue": "Jan 1, 2013"
    },
    {
        "title" : "footballField",
        "fieldType": "Button",
        "fieldValue": false
    },
    {
        "title" : "baseballField",
        "fieldType": "Button",
        "fieldValue": true
    },
    {
        "title" : "bballField",
        "fieldType": "Button",
        "fieldValue": false
    },
    {
        "title" : "hockeyField",
        "fieldType": "Button",
        "fieldValue": true
    },
    {
        "title" : "nascarField",
        "fieldType": "Button",
        "fieldValue": false
    }
];

var mappedFields = pdfFiller.mapForm2PDF( fieldJson, convMap );
console.log(mappedFields);

This will print out the object below.

{
    "last_name" : "John",
    "first_name" : "Doe",
    "date" : "Jan 1, 2013",
    "football" : "Off",
    "baseball" : "Yes",
    "basketball" : "Off",
    "hockey" : "Yes",
    "nascar" : "Off"
};

Convert fieldJson to FDF data

var pdfFiller   = require('pdffiller-keepfields');

var fieldJson = [
    {
        "title" : "last_name",
        "fieldfieldType": "Text",
        "fieldValue": "Doe"
    },
    {
        "title" : "first_name",
        "fieldfieldType": "Text",
        "fieldValue": "John"
    },
    {
        "title" : "date",
        "fieldType": "Text",
        "fieldValue": "Jan 1, 2013"
    },
    {
        "title" : "football",
        "fieldType": "Button",
        "fieldValue": false
    },
    {
        "title" : "baseball",
        "fieldType": "Button",
        "fieldValue": true
    },
    {
        "title" : "basketball",
        "fieldType": "Button"
        "fieldValue": false
    },
    {
        "title" : "hockey",
        "fieldType": "Button"
        "fieldValue": true
    },
    {
        "title" : "nascar",
        "fieldType": "Button"
        "fieldValue": false
    }
];

var FDFData = pdfFiller.convFieldJson2FDF( fieldJson );
console.log(FDFData)

This will print out this

{
    "last_name" : "John",
    "first_name" : "Doe",
    "date" : "Jan 1, 2013",
    "football" : "Off",
    "baseball" : "Yes",
    "basketball" : "Off",
    "hockey" : "Yes",
    "nascar" : "Off"
};