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

Read field values: attached an extension to your code to do this #70

Open
calexanderbynum opened this issue May 2, 2018 · 0 comments
Open

Comments

@calexanderbynum
Copy link

calexanderbynum commented May 2, 2018

Figured you might want to add this in. Super helpful for sending out fillable pdf's and then reading the data.

Some extras in here, such as the include, because I overrode your function with mine so when/if I update your code it won't break.

I am kinda bad at regex so please criticize and make changes as you see fit.

pdfFiller.generateFieldJson = function( sourceFile, nameRegex, callback){
var execFile = require('child_process').execFile;
var regName = /FieldName: ([^\n])/,
regType = /FieldType: ([A-Za-z\t .]+)/,
regFlags = /FieldFlags: ([0-9\t .]+)/,
regValue = /FieldValue: ([A-Za-z0-9!@#$%^&
()_+-=[]{};':"\|,.<>/?\t .]+)/,
fieldArray = [],
currField = {};

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

execFile( "pdftk", [sourceFile, "dump_data_fields_utf8"], function (error, stdout, stderr) {
    if (error) {
        console.log('exec error: ' + error);
        return callback(error, null);
    }
	
    fields = stdout.toString().split("---").slice(1);
    fields.forEach(function(field){
        currField = {};

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

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

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


        fieldArray.push(currField);
    });

    return callback(null, fieldArray);
});

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant