In this post we are going to discuss about creating PDF's in NodeJS using pdfkit. If you want to create a PDF or want to modify the existing PDF's, there are Node modules that provide PDF manipulation capability. The best among them is PDFKit (A PDF generation library for Node.js).
According to PDFKit, it is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy. This module provides you with functionality like create a PDF document, add pages to it, writing text, adding graphics and images.
Installation Using npm : npm install pdfkit
Creating PDF Document Example :
var pdf = require('pdfkit');
var fs = require('fs');
var myDoc = new pdf;
myDoc.pipe(fs.createWriteStream('output.pdf'));
myDoc.font('Times-Roman')
.fontSize(50)
.text('My First PDF using PDFKit', 100, 100);
myDoc.end();
Result : A output.pdf file is created with a text "My First PDF using PDFKit".
In the above example at first we have created a new PDF document by using the command var myDoc = new pdf; After that we have added a font named 'Times-Roman' with a font size of 50. Then the text is drawn at X, Y coordinates of 100, 100.
When using the example from the PDFKit Website you might get an error as Syntax error: Unexpected Identifier.
To solve this error you need to write fs.createStream() within the braces of myDoc.pipe() for ex : myDoc.pipe(fs.createWriteStream('output.pdf')); along with that you need to add var fs = require('fs').
To Play with the PDFKit, you can check An Interactive Browser Demo. Please Like and Share the Blog, if you find it interesting and helpful.
According to PDFKit, it is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy. This module provides you with functionality like create a PDF document, add pages to it, writing text, adding graphics and images.
Installation Using npm : npm install pdfkit
Creating PDF Document Example :
var pdf = require('pdfkit');
var fs = require('fs');
var myDoc = new pdf;
myDoc.pipe(fs.createWriteStream('output.pdf'));
myDoc.font('Times-Roman')
.fontSize(50)
.text('My First PDF using PDFKit', 100, 100);
myDoc.end();
Result : A output.pdf file is created with a text "My First PDF using PDFKit".
In the above example at first we have created a new PDF document by using the command var myDoc = new pdf; After that we have added a font named 'Times-Roman' with a font size of 50. Then the text is drawn at X, Y coordinates of 100, 100.
When using the example from the PDFKit Website you might get an error as Syntax error: Unexpected Identifier.
To solve this error you need to write fs.createStream() within the braces of myDoc.pipe() for ex : myDoc.pipe(fs.createWriteStream('output.pdf')); along with that you need to add var fs = require('fs').
To Play with the PDFKit, you can check An Interactive Browser Demo. Please Like and Share the Blog, if you find it interesting and helpful.
Thank you!
ReplyDeletePDFKit does not provide modifying feature. This article is misleading and not up to the mark.
ReplyDelete