如何使用javascript和pdfkit将文本添加到pdf?

时间:2013-01-27 00:23:32

标签: node.js pdf-generation

我安装了pdfkit,现在我正在尝试将文本添加到pdf并将其保存到磁盘。我正在关注http://blog.nodeknockout.com/post/9134350698/countdown-to-ko-8-introduction-to-pdfkit的教程,但我在下面遇到错误。如何使用node.js和pdfkit添加文本?​​

谢谢你。 源代码:

 doc = new PDFDocument();
doc.moveTo(300, 75)
   .lineTo(373, 301)
   .lineTo(181, 161)
   .lineTo(419, 161)
   .lineTo(227, 301)
   .fill('red', 'even-odd');

    var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in...';

    doc.y = 320;
    doc.fillColor('black')
   .text(loremIpsum, {
       paragraphGap: 10,
       indent: 20,
       align: 'justify',
       columns: 2
   });


    doc.write('out.pdf');

错误

/Users/sadmin/Dropbox/Node JS/wordlets/wordCloud.js:61
   .text(loremIpsum, {
    ^
TypeError: Property 'text' of object [object PDFDocument] is not a function
    at Object.createPDF (/Users/sadmin/Dropbox/Node JS/wordlets/wordCloud.js:61:5)
    at /Users/sadmin/Dropbox/Node JS/wordlets/requestHandlers.js:62:15
    at Request._callback (/Users/sadmin/Dropbox/Node JS/wordlets/wordCloud.js:116:5)
    at Request.init.self.callback (/Users/sadmin/node_modules/request/main.js:122:22)
    at Request.EventEmitter.emit (events.js:99:17)
    at Request.<anonymous> (/Users/sadmin/node_modules/request/main.js:661:16)
    at Request.EventEmitter.emit (events.js:126:20)
    at IncomingMessage.Request.start.self.req.self.httpModule.request.buffer (/Users/sadmin/node_modules/request/main.js:623:14)
    at IncomingMessage.EventEmitter.emit (events.js:126:20)
    at IncomingMessage._emitEnd (http.js:366:10)

1 个答案:

答案 0 :(得分:2)

它应该是doc.text,因为您在.fill调用和.text之间有其他代码行,从而产生语法错误。