user3574621 user3574621 - 1 month ago 24
CoffeeScript Question

NodeJS and PDFkit: 'margin' property gives "Unexpected identifier" error?

I have successfully gotten my code to output a pdf, but when I attempt to adjust the margins using the 'margin' property listed in the documentation using the following code,

var pdf = require ('pdfkit');
var fs = require('fs');

var doc = new pdf(
{
size: [288,144]

}
);

doc.pipe(fs.createWriteStream('run.pdf'));

doc.font('Times-Roman')
.text('Hello different Times Roman!')

doc.addPage({
size: [288,144]
margin : 10
});

doc.end();


I get this error:

margin : 10
^^^^^^

SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3


Am I missing something obvious here?

Answer

Just to ensure any future reader come here with this specific problem, this is a JavaScript Object. When listing JavaScript Objects, every property must be followed by a , except the last instance.

As an example, this:

doc.addPage({
    size: [288,144]
    margin : 10
});

becomes this:

doc.addPage({
    size: [288,144], 
    margin : 10
});