Skip to content

Modifications #53

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

Closed
wants to merge 13 commits into from
Closed
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
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@ 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
```
js
var pdfFiller = require('pdffiller');

// ...
```

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 +52,33 @@ 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,7 +91,7 @@ var FDF_data = pdfFiller.generateFDFTemplate( sourcePDF, nameRegex, function(err
console.log(fdfData);
});

````
```

This will print out this
```
Expand All @@ -97,7 +108,8 @@ 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,7 +168,7 @@ var fieldJson = [

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

This will print out the object below.
```
Expand All @@ -173,7 +185,8 @@ 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 +234,10 @@ var fieldJson = [

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

This will print out this
````
```
{
"last_name" : "John",
"first_name" : "Doe",
Expand All @@ -235,4 +248,4 @@ This will print out this
"hockey" : "Yes",
"nascar" : "Off"
};
````
```
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* 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');

opt = opt || {};

var pdffiller = {

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

Expand All @@ -76,12 +79,21 @@
}

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 Down
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