What is @types/pdfkit?
@types/pdfkit provides TypeScript type definitions for the pdfkit library, which is a powerful tool for creating PDF documents in Node.js.
What are @types/pdfkit's main functionalities?
Create a PDF Document
This feature allows you to create a new PDF document and add text to it. The document is then saved to a file named 'output.pdf'.
const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Hello, world!');
doc.end();
Add Images
This feature allows you to add images to your PDF document. The image is added with specific dimensions and alignment.
const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc.image('path/to/image.png', { fit: [250, 300], align: 'center', valign: 'center' });
doc.end();
Draw Shapes
This feature allows you to draw shapes, such as rectangles, in your PDF document. The rectangle is drawn with specified dimensions and position.
const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc.rect(100, 100, 200, 200).stroke();
doc.end();
Add Links
This feature allows you to add hyperlinks to your PDF document. The text 'Click here' is linked to 'http://example.com' and is underlined.
const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc.text('Click here', { link: 'http://example.com', underline: true });
doc.end();
Other packages similar to @types/pdfkit
pdf-lib
pdf-lib is a library for creating and modifying PDF documents in JavaScript. It offers a more modern API and is written in TypeScript, which provides better type safety compared to pdfkit.
jspdf
jspdf is a popular library for generating PDF documents in JavaScript. It is often used in web applications and has a wide range of features, including support for adding text, images, and shapes. However, it is primarily focused on client-side usage.
pdfmake
pdfmake is a library for creating PDF documents in JavaScript. It provides a declarative syntax for defining the content and layout of the PDF, making it easier to create complex documents. It is similar to pdfkit but offers a different approach to document creation.