###介绍
起初,生成PDF文件似乎是件很繁琐的任务,但有了PDFKit,这个任务就容易多了。
但对于Node.js的PDFKit模块,知道的人并不多,这倒是很意外。有了PDFKit模块,处理PDF文件变得非常容易,它让你避免了所有的复杂工作,并提供用CoffeeScript(也可以作为普通版的Javascript使用)写成的简易的API。本篇当中,我们一起来生成一个服务器端的带文本内容的简易PDF文件,用的就是PDFKit模块和Node.js。现在开始吧: 首先,大家都知道,我们用npm安装模块:
npm install pdfkit
var PDF = require('pdfkit'); //including the pdfkit module
var fs = require('fs');
var text = 'ANY_TEXT_YOU_WANT_TO_WRITE_IN_PDF_DOC';
doc = new PDF(); //creating a new PDF object
doc.pipe(fs.createWriteStream('PATH_TO_PDF_FILE')); //creating a write stream
//to write the content on the file system
doc.text(text, 100, 100); //adding the text to be written,
// more things can be added here including new pages
doc.end(); //we end the document writing.
这样在你提供的路径应该创建了一个新的PDF文件。各位,就是这些!只用了大概5分钟就在服务器生成了一个PDF。
回复 (0)
暂无回复,成为第一个参与讨论的人。
参与回复
登录后即可参与回复。登录