0% found this document useful (0 votes)
98 views9 pages

PDFKit Guide for Developers

Pdfkit is a Node.js library for creating PDF documents. It allows embedding fonts, adding images and vector graphics, and generating PDF files or blobs. The document shows how to create a PDF document, add text with different fonts and styles, insert images with positioning, and output the final PDF to a file or blob. Key methods include doc.font(), doc.text(), doc.image(), doc.addPage(), and doc.end().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views9 pages

PDFKit Guide for Developers

Pdfkit is a Node.js library for creating PDF documents. It allows embedding fonts, adding images and vector graphics, and generating PDF files or blobs. The document shows how to create a PDF document, add text with different fonts and styles, insert images with positioning, and output the final PDF to a file or blob. Key methods include doc.font(), doc.text(), doc.image(), doc.addPage(), and doc.end().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

pdfkit - npm https://www.npmjs.

com/package/pdfkit

2 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

const PDFDocument = require('pdfkit');


const fs = require('fs');

// Create a document
const doc = new PDFDocument();

// Pipe its output somewhere, like to a file or HTTP response


// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));

// Embed a font, set the font size, and render some text
doc
.font('fonts/PalatinoBold.ttf')

3 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

.fontSize(25)
.text('Some text with an embedded font!', 100, 100);

// Add an image, constrain it to a given size, and center it vertical


doc.image('path/to/image.png', {
fit: [250, 300],
align: 'center',
valign: 'center'
});

// Add another page


doc
.addPage()
.fontSize(25)
.text('Here is some vector graphics...', 100, 100);

// Draw a triangle
doc
.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill('#FF3300');

// Apply some transforms and render an SVG path with the 'even-odd' f
doc
.scale(0.6)
.translate(470, -380)
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
.fill('red', 'even-odd')
.restore();

// Add some text with annotations

4 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

doc
.addPage()
.fillColor('blue')
.text('Here is a link!', 100, 100)
.underline(100, 100, 160, 27, { color: '#0000FF' })
.link(100, 100, 160, 27, 'http://google.com/');

// Finalize PDF file


doc.end();

// require dependencies
const PDFDocument = require('pdfkit');
const blobStream = require('blob-stream');

5 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

// create a document the same way as above


const doc = new PDFDocument();

// pipe the document to a blob


const stream = doc.pipe(blobStream());

// add your content to the document here, as usual

// get a blob when you are done


doc.end();
stream.on('finish', function() {
// get a blob you can do whatever you like with
const blob = stream.toBlob('application/pdf');

// or get a blob URL for display in the browser


const url = stream.toBlobURL('application/pdf');
iframe.src = url;
});

6 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

7 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

8 de 9 15/12/2021 10:34
pdfkit - npm https://www.npmjs.com/package/pdfkit

9 de 9 15/12/2021 10:34

You might also like