bauhausjs / phantom-html2pdf

Node module to generate PDFs from HTML via PhantomJS
MIT License
112 stars 47 forks source link

Added an optional runningsArgs option to pass params to the runnings file #37

Closed OccamsCat closed 8 years ago

OccamsCat commented 8 years ago

The runningsArgs option passes an object of parameters to the runnings file, thereby allowing the creation of headers and footers containing variables passed in from elsewhere in the app.

In order for the runnings file to accept this argument, the returned object needs to be wrapped in a function with one parameter like so:

module.exports = function runnings(args) {
    return {
        footer: {
            height: '1cm',
            contents: function(pageNum, totalPages) {
                return [
                    '<footer>',
                    '<p>'+ args.userName + ' (Page ' + pageNum + ' of '+ totalPages +')</p>',
                    '</footer>'
                ].join('');
            }
        }
    };
};

The above example assumes that the runningsArgs option was passed an object, containing the property userName. E.g.:

var params = {
    userName: 'some.user'
};

var options = {
    html: '<h1>Sample</h1>',
    paperSize: {format: 'Letter', orientation: 'portrait', border: '1cm'},
    runnings: 'runnings.js',
    runningsArgs: params
};

pdf.convert(options, function(err, result){});
OccamsCat commented 8 years ago

Hi, @dustin-H,

Thanks for the quick review! I added the change you requested (except I'm just setting it to an empty string, because that way it's easier to check, whether this option exists inside of phantom-script.js) and also made a modification, which should make this feature a non-breaking change. E.g. now, if a runningsArgs option is not passed, then the regular runnings file will still work, (without it having to be wrapped in a function). But perhaps it's still a good idea to increment the version, that is obviously up to you.

dustin-H commented 8 years ago

@OccamsCat Thanks for your work... I will merge and publish this weekend.

dustin-H commented 8 years ago

Why did you close @OccamsCat ? I cannot merge when it is closed.

OccamsCat commented 8 years ago

Oops, I'm sorry, that was unintentional. (I was writing a comment and clicked the wrong button on accident (:

OccamsCat commented 8 years ago

Thanks for the quick communication. Looking forward to the new release!