0% found this document useful (0 votes)
110 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)
110 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

pdfkit - npm [Link]

com/package/pdfkit

2 de 9 15/12/2021 10:34
pdfkit - npm [Link]

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
[Link]([Link]('[Link]'));

// Embed a font, set the font size, and render some text
doc
.font('fonts/[Link]')

3 de 9 15/12/2021 10:34
pdfkit - npm [Link]

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

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


[Link]('path/to/[Link]', {
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 [Link]

doc
.addPage()
.fillColor('blue')
.text('Here is a link!', 100, 100)
.underline(100, 100, 160, 27, { color: '#0000FF' })
.link(100, 100, 160, 27, '[Link]

// Finalize PDF file


[Link]();

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

5 de 9 15/12/2021 10:34
pdfkit - npm [Link]

// create a document the same way as above


const doc = new PDFDocument();

// pipe the document to a blob


const stream = [Link](blobStream());

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

// get a blob when you are done


[Link]();
[Link]('finish', function() {
// get a blob you can do whatever you like with
const blob = [Link]('application/pdf');

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


const url = [Link]('application/pdf');
[Link] = url;
});

6 de 9 15/12/2021 10:34
pdfkit - npm [Link]

7 de 9 15/12/2021 10:34
pdfkit - npm [Link]

8 de 9 15/12/2021 10:34
pdfkit - npm [Link]

9 de 9 15/12/2021 10:34

You might also like