0

I've come across a rather annoying little Syntax Problem. I'm currently using Scissors a Node Module for handling pdf files.

The syntax for choosing some of the Pdfs pages is described in the Docs:

var scissors = require('scissors');
var pdf = scissors('in.pdf')
   .pages(4, 5, 6, 1, 12)

This actually works great for me, but I'd like to do this dynamically. How would I go about concatenating integers to commas in javascript? if I pass a string, the function doesn't work anymore.

Thanks a lot in advance

1

4 Answers 4

2

You are passing n values as arguments to a function. If you concatenate it into a String you will pass only one argument, the concatenated String.

Probably you want to use the spread operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

If you have the numbers into an array you want to pass them to the function like this:

var scissors = require('scissors');
var pages = [4, 5, 6, 1, 12];
var pdf = scissors('in.pdf')
   .pages(...pages);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use Function.prototype.apply for this.

var scissors = require('scissors');
var pdf = scissors('in.pdf'),
    args = [4, 5, 6, 1, 12];

scissors.pages.apply(pdf, args);

Comments

1

You should be able to pass an array of page numbers to the function. I took a look at the scissors source code and they seem to actually take care of the arguments themselves:

/**
 * Creates a copy of the pages with the given numbers
 * @param {(...Number|Array)} Page number, either as an array or as     arguments
 * @return {Command} A chainable Command instance
 */
Command.prototype.pages = function () {
  var args = (Array.isArray(arguments[0])) ?
    arguments[0] : Array.prototype.slice.call(arguments);
  var cmd = this._copy();
  return cmd._push([
    'pdftk', cmd._input(),
    'cat'].concat(args.map(Number), [
      'output', '-'
      ]));
};

You can pass multiple arguments that will be combined to an array with Array.prototype.slice or just pass an array that will be used directly.

var scissors = require('scissors');

var pages = [];

/* collect desired pages */
pages.push(23);
pages.push(42);
pages.push(1337);

var pdf = scissors('in.pdf').pages(pages);

Comments

0

I'm assuming you mean you want to pass an array of arguments to the pages function. You can achieve this with javascript's apply function

var scissors = require('scissors');
var pdf = scissors('in.pdf')

pdf.pages.apply(pdf, [4, 5, 6, 1, 12])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.