GithubHelp home page GithubHelp logo

doc22940 / pdffiller Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pdffillerjs/pdffiller

1.0 1.0 0.0 1.16 MB

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

License: MIT License

JavaScript 100.00%

pdffiller's Introduction

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.

Quick start

First, run npm install pdffiller --save for your app.

Import the module using:

var pdfFiller = require('pdffiller');

// ...

Examples

1.Fill PDF with existing FDF Data

var pdfFiller   = require('pdffiller');

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.

2. Generate FDF Template from PDF

var pdfFiller   = require('pdffiller');

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" : ""
};

3. Map form fields to PDF fields

var pdfFiller = require('pdffiller');

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"
};

4. Convert fieldJson to FDF data

var pdfFiller   = require('pdffiller');

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"
};

pdffiller's People

Contributors

anperez78 avatar g3k0 avatar jaakkol avatar johntayl avatar kevitan avatar madsnedergaard avatar mnort9 avatar rhaseven7h avatar scotato avatar somombo avatar whitef0x0 avatar yoz avatar ztzven avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.