Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Option and Testing For KeepExistingFields #54

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Testing Files
./test/test_complete*
temp_data*

# iOS / Apple
# ===========
.DS_Store
Expand Down
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Testing Files
./test/test_complete*
temp_data*

node_modules/*
!node_modules/pdffiller/
*.fdf
Expand Down
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,47 @@ PDF Filler (Node.js)

A node.js PDF form field data filler and FDF generator toolkit. This essentially is a wrapper around the PDF Toolkit library <a target="_blank" href="http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/">PDF ToolKit</a>.


Quick start
-----------

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

Import the module using:

```js
```javascript
var pdfFiller = require('pdffiller');

// ...
```

If you want to keep existing values from a form when using pdffiller make sure to specify this before using the module with the code below.

```javascript
pdfFiller.setOptions({keepExistingValues: true});

// ...
```

Development
-----------

First clone this repository.

Then, run tests with `npm run test` before commencing development.
=======
If you'd like to list out the contents of the form values in your source PDF, import the module as follows:

```
js
var pdfFiller = require('pdffiller')({ keep: true });

// ...
```

## Examples

#### 1.Fill PDF with existing FDF Data
````javascript
```javascript
var pdfFiller = require('pdffiller');

var sourcePDF = "test/test.pdf";
Expand All @@ -43,31 +65,31 @@ pdfFiller.fillForm( sourcePDF, destinationPDF, data, function(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,

````javascript
```javascript

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
````javascript
```javascript
var pdfFiller = require('pdffiller');

var sourcePDF = "test/test.pdf";
Expand All @@ -80,10 +102,10 @@ var FDF_data = pdfFiller.generateFDFTemplate( sourcePDF, nameRegex, function(err
console.log(fdfData);
});

````
```

This will print out this
```
```json
{
"last_name" : "",
"first_name" : "",
Expand All @@ -97,7 +119,7 @@ This will print out this
```

#### 3. Map form fields to PDF fields
````javascript
```javascript
var pdfFiller = require('pdffiller');

var convMap = {
Expand Down Expand Up @@ -156,10 +178,10 @@ var fieldJson = [

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

This will print out the object below.
```
```json
{
"last_name" : "John",
"first_name" : "Doe",
Expand All @@ -173,7 +195,7 @@ This will print out the object below.
```

#### 4. Convert fieldJson to FDF data
````javascript
```javascript
var pdfFiller = require('pdffiller');

var fieldJson = [
Expand Down Expand Up @@ -221,10 +243,10 @@ var fieldJson = [

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

This will print out this
````
```json
{
"last_name" : "John",
"first_name" : "Doe",
Expand All @@ -235,4 +257,4 @@ This will print out this
"hockey" : "Yes",
"nascar" : "Off"
};
````
```
Empty file added git
Empty file.
43 changes: 32 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
* Description: This PDF filler module takes a data set and creates a filled out
* PDF file with the form fields populated.
*/
(function(){
(function(opt){
var child_process = require('child_process'),
execFile = require('child_process').execFile,
fdf = require('utf8-fdf-generator'),
_ = require('lodash'),
fs = require('fs');

var defaultOpts = {
keepExistingValues: false
};

opt = opt || {};

var pdffiller = {
currOpts: {},
setOptions: function(opts){
this.currOpts = opts;
},

mapForm2PDF: function( formFields, convMap ){
var tmpFDFData = this.convFieldJson2FDF(formFields);
Expand Down Expand Up @@ -52,8 +62,10 @@
var regName = /FieldName: ([^\n]*)/,
regType = /FieldType: ([A-Za-z\t .]+)/,
regFlags = /FieldFlags: ([0-9\t .]+)/,
regValue = /FieldValue: ([^\n]*)/,
fieldArray = [],
currField = {};
currField = {},
that = this;

if(nameRegex !== null && (typeof nameRegex) == 'object' ) regName = nameRegex;

Expand All @@ -69,19 +81,28 @@

currField['title'] = field.match(regName)[1].trim() || '';

if(field.match(regType)){
if(that.currOpts.keepExistingValues && field.match(regType)){
currField['fieldType'] = field.match(regType)[1].trim() || '';
}else {
currField['fieldType'] = '';
}

if(field.match(regFlags)){
currField['fieldFlags'] = field.match(regFlags)[1].trim()|| '';
currField['fieldFlags'] = field.match(regFlags)[1].trim() || '';
}else{
currField['fieldFlags'] = '';
}

currField['fieldValue'] = '';

if (opt.keep) {
if(field.match(regValue)){
currField['fieldValue'] = field.match(regValue)[1].trim() || '';
}else{
currField['fieldValue'] = '';
}
}else{
currField['fieldValue'] = '';
}


fieldArray.push(currField);
});
Expand All @@ -103,22 +124,21 @@
},

fillFormWithOptions: function( sourceFile, destinationFile, fieldValues, shouldFlatten, tempFDFPath, callback ) {


//Generate the data from the field values.
var randomSequence = Math.random().toString(36).substring(7);
var currentTime = new Date().getTime();
var tempFDFFile = "temp_data" + currentTime + randomSequence + ".fdf",
tempFDF = (typeof tempFDFPath !== "undefined"? tempFDFPath + '/' + tempFDFFile: tempFDFFile),

formData = fdf.generator( fieldValues, tempFDF );

var args = [sourceFile, "fill_form", tempFDF, "output", destinationFile];
if (shouldFlatten) {
args.push("flatten");
}
debugger;
execFile( "pdftk", args, function (error, stdout, stderr) {

debugger;
if ( error ) {
console.log('exec error: ' + error);
return callback(error);
Expand All @@ -129,7 +149,6 @@
if ( err ) {
return callback(err);
}
// console.log( 'Sucessfully deleted temp file ' + tempFDF );
return callback();
});
} );
Expand All @@ -145,6 +164,8 @@

};

pdffiller.setOptions(defaultOpts);

module.exports = pdffiller;

}())
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdffiller",
"version": "0.0.10",
"name": "pdffiller-keepfields",
"version": "0.0.18",
"private": false,
"description": "Take an existing PDF Form and data and PDF Filler will create a new PDF with all given fields populated.",
"main": "index.js",
Expand All @@ -16,15 +16,15 @@
],
"dependencies": {
"lodash": "~3.8.0",
"utf8-fdf-generator": "0.0.3"
"utf8-fdf-generator": "0.0.3",
"node-flags": "0.1.9"
},
"author": {
"name": "John Taylor and David Baldwynn"
"name": "John Taylor, David Baldwynn and Bill Butler"
},
"license": "MIT",
"readmeFilename": "README.md",
"gitHead": "dc344fd81d90d0434dcc253d9ee14e4328b680dd",
"readme": "PDF Filler (Node.js)\n======\n\nA node.js PDF form field data filler and FDF generator toolkit. This essentially is a wrapper around the PDF Toolkit library <a target=\"_blank\" href=\"http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/\">PDF ToolKit</a>.\n\nPDF Filler requires the PDF ToolKit which can be found here: <a target=\"_blank\" href=\"http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/\">PDF ToolKit</a>\n\n\n##Examples\n\n#### 1.Fill PDF with existing FDF Data\n````javascript\nvar pdfFiller = require( 'pdffiller' );\n\nvar sourcePDF = \"test/test.pdf\";\nvar destinationPDF = \"test/test_complete.pdf\";\n\nvar data = {\n \"last_name\" : \"John\",\n \"first_name\" : \"Doe\",\n \"date\" : \"Jan 1, 2013\",\n \"football\" : \"Off\",\n \"baseball\" : \"Yes\",\n \"basketball\" : \"Off\",\n \"hockey\" : \"Yes\",\n \"nascar\" : \"Off\"\n};\n\npdfFiller.fillForm( sourcePDF, destinationPDF, data, function(err) { \n if (err) throw err;\n console.log(\"In callback (we're done).\"); \n});\n\n````\n\nThis will take the test.pdf, fill the fields with the data values\nand create a complete filled in PDF (test_filled_in.pdf)\n\n\n#### 2. Generate FDF Template from PDF\n````javascript\nvar pdfFiller = require( 'pdffiller' );\n\nvar sourcePDF = \"test/test.pdf\";\n\nvar FDF_data = pdfFiller.generateFDFTemplate( sourcePDF, function(err, fdfData) { \n if (err) throw err;\n console.log(fdfData);\n});\n\n````\n\nThis will print out this \n```{\n \"last_name\" : \"\",\n \"first_name\" : \"\",\n \"date\" : \"\",\n \"football\" : \"\",\n \"baseball\" : \"\",\n \"basketball\" : \"\",\n \"hockey\" : \"\",\n \"nascar\" : \"\"\n};```\n\n#### 3. Generate FDF Template from PDF\n````javascript\nvar pdfFiller = require( 'pdffiller' );\n\nvar sourcePDF = \"test/test.pdf\";\n\nvar FDF_data = pdfFiller.generateFDFTemplate( sourcePDF, function(err, fdfData) { \n if (err) throw err;\n console.log(fdfData);\n});\n\n````\n\nThis will print out this \n```\n{\n \"last_name\" : \"\",\n \"first_name\" : \"\",\n \"date\" : \"\",\n \"football\" : \"\",\n \"baseball\" : \"\",\n \"basketball\" : \"\",\n \"hockey\" : \"\",\n \"nascar\" : \"\"\n};\n```\n\n#### 4. Map form fields to PDF fields\n````javascript\nvar pdfFiller = require( 'pdffiller' ),\n sourcePDF = \"test/test.pdf\",\n FDF_data,\n destinationPDF = \"test/test_complete.pdf\";\n\nvar conversionMap = {\n \"lastName\": \"last_name\",\n \"firstName\": \"first_name\",\n \"Date\": \"date\",\n \"lastName\": \"last_name\",\n \"footballField\": \"football\",\n \"bballField\": \"basketball\",\n \"baseballField\": \"baseball\",\n \"hockeyField\": \"hockey\",\n \"nascarField\": \"nascar\"\n};\n\nvar FormFields = {\n \"lastName\" : \"John\",\n \"firstName\" : \"Doe\",\n \"Date\" : \"Jan 1, 2013\",\n \"footballField\" : \"Off\",\n \"baseballField\" : \"Yes\",\n \"bballField\" : \"Off\",\n \"hockeyField\" : \"Yes\",\n \"nascarField\" : \"Off\"\n};\n\npdfFiller.mapForm2PDF( data, convMap, function(err, mappedFields) { \n if (err) throw err;\n\n console.log(mappedFields);\n});\n````\n\nThis will print out the object below.\n```{\n \"last_name\" : \"John\",\n \"first_name\" : \"Doe\",\n \"date\" : \"Jan 1, 2013\",\n \"football\" : \"Off\",\n \"baseball\" : \"Yes\",\n \"basketball\" : \"Off\",\n \"hockey\" : \"Yes\",\n \"nascar\" : \"Off\"\n};```\n\n#### 5. Convert fieldJson to FDF data\n````javascript\nvar pdfFiller = require( 'pdffiller' );\n\nvar sourcePDF = \"test/test.pdf\";\nvar fieldJson = [\n {\n \"title\" : \"last_name\",\n \"fieldfieldType\": \"Text\",\n \"fieldValue\": \"Doe\"\n },\n {\n \"title\" : \"first_name\",\n \"fieldfieldType\": \"Text\",\n \"fieldValue\": \"John\"\n },\n {\n \"title\" : \"date\",\n \"fieldType\": \"Text\",\n \"fieldValue\": \"Jan 1, 2013\"\n },\n {\n \"title\" : \"football\",\n \"fieldType\": \"Button\",\n \"fieldValue\": false\n },\n {\n \"title\" : \"baseball\",\n \"fieldType\": \"Button\",\n \"fieldValue\": true\n },\n {\n \"title\" : \"basketball\",\n \"fieldType\": \"Button\"\n \"fieldValue\": false\n },\n {\n \"title\" : \"hockey\",\n \"fieldType\": \"Button\"\n \"fieldValue\": true\n },\n {\n \"title\" : \"nascar\",\n \"fieldType\": \"Button\"\n \"fieldValue\": false\n }\n];\n\nvar FDFData = pdfFiller.convFieldJson2FDF( data );\nconsole.log(FDFData)\n````\n\nThis will print out this \n````\n{\n \"last_name\" : \"John\",\n \"first_name\" : \"Doe\",\n \"date\" : \"Jan 1, 2013\",\n \"football\" : \"Off\",\n \"baseball\" : \"Yes\",\n \"basketball\" : \"Off\",\n \"hockey\" : \"Yes\",\n \"nascar\" : \"Off\"\n};\n````\n",
"_id": "[email protected]",
"_shasum": "416c724b748a5e0caf494fb66b043d450b8a3b9a",
"_from": "whitef0x0/pdffiller",
Expand Down
Loading