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

Documentation for "execute" #5

Open
jonlachlan opened this issue Jun 4, 2015 · 1 comment
Open

Documentation for "execute" #5

jonlachlan opened this issue Jun 4, 2015 · 1 comment

Comments

@jonlachlan
Copy link

Hi,
It didn't take too long to figure this out, but it would be helpful to have an example of how to use the execute command. For example, pointing out that the args is an array of strings that should be ordered just like the pdftk CLI.
Example:

PDFTK.execute(["/path/to/in.pdf","commandToExecute"],function(err,stdout,stderr) {
 // stdout is a default output unless output file path is specified in args array
 // stdout is a Node Buffer (https://nodejs.org/api/buffer.html)
 console.log(stdout.toString('utf8'));
}

BTW the command I'll primarily be using is dump_data_fields which I will use to parse the field values of a PDF file. I'm parsing the values using the SO answers here: http://stackoverflow.com/questions/30362686/parsing-a-colon-separated-string-into-json-in-node-javascript. I'm storing PDFs using Collection FS using the filesystem storage option (cfs:filesystem). So far it works, so I'm super appreciative of this package. Thanks!!!

@jwjordan
Copy link

jwjordan commented Mar 9, 2017

I know this is old, but your comment was very helpful. I have been able to use dump_data_fields on a PDF and parse the results (using the SO link you provided). Problem is, I have the parsed results on the server, and I'd like to pass those back to the client, and it won't work synchronously. Meaning, I can't get the values back to the client because the server is still processing them when the function returns the callback. I have tried, and failed, to use futures. This is what I have so far:

`
var future = new Future();

   PDFTK.execute([file,"dump_data_fields"],function(err,stdout,stderr) {
        // stdout is a default output unless output file path is specified in args array
        // stdout is a Node Buffer (https://nodejs.org/api/buffer.html)
        console.log("ERR", err);
        console.log("STDOUT", stdout);
        console.log("STDERR", stderr);
       
        var input = stdout.toString('utf8').trim();
       
        // Parse the data into an array
        var fields = [];
        var field_strings = input.split(/[\r\n]*---[\r\n]*/);
        for (var i = 0; i < field_strings.length; i++) {
            if (field_strings[i] == '') { // Skip blank field at beginning
                continue;
            }
            var obj = {};
            var props_strings = field_strings[i].split('\n');
            for (var j = 0; j < props_strings.length; j++) {
                var keyvalue = props_strings[j].split(':');
                obj[keyvalue[0]] = keyvalue[1].trim();
            }
            fields.push(obj);
        }
        console.log(fields);
       
        future.return(fields);
    });

`

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

2 participants