UNPKG

5.64 kBMarkdownView Raw
1# PDFKit
2
3A JavaScript PDF generation library for Node and the browser.
4
5[![](https://img.shields.io/gratipay/devongovett.svg)](https://gratipay.com/devongovett)
6
7## Description
8
9PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy.
10It's written in CoffeeScript, but you can choose to use the API in plain 'ol JavaScript if you like. The API embraces
11chainability, and includes both low level functions as well as abstractions for higher level functionality. The PDFKit API
12is designed to be simple, so generating complex documents is often as simple as a few function calls.
13
14Check out some of the [documentation and examples](http://pdfkit.org/docs/getting_started.html) to see for yourself!
15You can also read the guide as a [self-generated PDF](http://pdfkit.org/docs/guide.pdf) with example output displayed inline.
16If you'd like to see how it was generated, check out the README in the [docs](https://github.com/devongovett/pdfkit/tree/master/docs)
17folder.
18
19You can also try out an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
20
21## Installation
22
23Installation uses the [npm](http://npmjs.org/) package manager. Just type the following command after installing npm.
24
25 npm install pdfkit
26
27## Features
28
29* Vector graphics
30 * HTML5 canvas-like API
31 * Path operations
32 * SVG path parser for easy path creation
33 * Transformations
34 * Linear and radial gradients
35* Text
36 * Line wrapping
37 * Text alignments
38 * Bulleted lists
39* Font embedding
40 * Supports TrueType (.ttf), TrueType Collections (.ttc), and Datafork TrueType (.dfont) fonts
41 * Font subsetting
42* Image embedding
43 * Supports JPEG and PNG files (including indexed PNGs, and PNGs with transparency)
44* Annotations
45 * Links
46 * Notes
47 * Highlights
48 * Underlines
49 * etc.
50
51## Coming soon!
52
53* Patterns fills
54* Outlines
55* PDF Security
56* Higher level APIs for creating tables and laying out content
57* More performance optimizations
58* Even more awesomeness, perhaps written by you! Please fork this repository and send me pull requests.
59
60## Example
61
62```coffeescript
63PDFDocument = require 'pdfkit'
64
65# Create a document
66doc = new PDFDocument
67
68# Pipe it's output somewhere, like to a file or HTTP response
69# See below for browser usage
70doc.pipe fs.createWriteStream('output.pdf')
71
72# Embed a font, set the font size, and render some text
73doc.font('fonts/PalatinoBold.ttf')
74 .fontSize(25)
75 .text('Some text with an embedded font!', 100, 100)
76
77# Add another page
78doc.addPage()
79 .fontSize(25)
80 .text('Here is some vector graphics...', 100, 100)
81
82# Draw a triangle
83doc.save()
84 .moveTo(100, 150)
85 .lineTo(100, 250)
86 .lineTo(200, 250)
87 .fill("#FF3300")
88
89# Apply some transforms and render an SVG path with the 'even-odd' fill rule
90doc.scale(0.6)
91 .translate(470, -380)
92 .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
93 .fill('red', 'even-odd')
94 .restore()
95
96# Add some text with annotations
97doc.addPage()
98 .fillColor("blue")
99 .text('Here is a link!', 100, 100)
100 .underline(100, 100, 160, 27, color: "#0000FF")
101 .link(100, 100, 160, 27, 'http://google.com/')
102
103# Finalize PDF file
104doc.end()
105```
106
107[The PDF output from this example](http://pdfkit.org/demo/out.pdf) (with a few additions) shows the power of PDFKit — producing
108complex documents with a very small amount of code. For more, see the `demo` folder and the
109[PDFKit programming guide](http://pdfkit.org/docs/getting_started.html).
110
111## Browser Usage
112
113There are two ways to use PDFKit in the browser. The first is to use [Browserify](http://browserify.org/),
114which is a Node module packager for the browser with the familiar `require` syntax. The second is to use
115a prebuilt version of PDFKit, which you can [download from Github](https://github.com/devongovett/pdfkit/releases).
116
117In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a
118[Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object which can be used to store binary data, and
119get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to
120get a Blob from the output of PDFKit, you can use the [blob-stream](https://github.com/devongovett/blob-stream)
121module.
122
123The following example uses Browserify to load `PDFKit` and `blob-stream`, but if you're not using Browserify,
124you can load them in whatever way you'd like (e.g. script tags).
125
126```coffeescript
127# require dependencies
128PDFDocument = require 'pdfkit'
129blobStream = require 'blob-stream'
130
131# create a document the same way as above
132doc = new PDFDocument
133
134# pipe the document to a blob
135stream = doc.pipe(blobStream())
136
137# add your content to the document here, as usual
138
139# get a blob when you're done
140doc.end()
141stream.on 'finish', ->
142 # get a blob you can do whatever you like with
143 blob = stream.toBlob('application/pdf')
144
145 # or get a blob URL for display in the browser
146 url = stream.toBlobURL('application/pdf')
147 iframe.src = url
148```
149
150You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
151
152Note that in order to Browserify a project using PDFKit, you need to install the `brfs` module with npm,
153which is used to load built-in font data into the package. It is listed as a `devDependency` in
154PDFKit's `package.json`, so it isn't installed by default for Node users.
155If you forget to install it, Browserify will print an error message.
156
157## Documentation
158
159For complete API documentation and more examples, see the [PDFKit website](http://pdfkit.org/).
160
161## License
162
163PDFKit is available under the MIT license.