|
1 | 1 | # PDFKit
|
2 |
| -### A PDF generation library for Node.js. |
| 2 | + |
| 3 | +A JavaScript PDF generation library for Node and the browser. |
3 | 4 |
|
4 | 5 | ## Description
|
5 | 6 |
|
6 |
| -PDFKit is a PDF document generation library for Node that makes creating complex, multi-page, printable documents easy. |
| 7 | +PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy. |
7 | 8 | It's written in CoffeeScript, but you can choose to use the API in plain 'ol JavaScript if you like. The API embraces
|
8 | 9 | chainability, and includes both low level functions as well as abstractions for higher level functionality. The PDFKit API
|
9 | 10 | is designed to be simple, so generating complex documents is often as simple as a few function calls.
|
@@ -54,52 +55,101 @@ Installation uses the [npm](http://npmjs.org/) package manager. Just type the f
|
54 | 55 |
|
55 | 56 | ## Example
|
56 | 57 |
|
57 |
| - PDFDocument = require 'pdfkit' |
58 |
| - |
59 |
| - # Create a document |
60 |
| - doc = new PDFDocument |
61 |
| - |
62 |
| - # Pipe it's output somewhere, like to a file or HTTP response |
63 |
| - doc.pipe fs.createWriteStream('output.pdf') |
64 |
| - |
65 |
| - # Embed a font, set the font size, and render some text |
66 |
| - doc.font('fonts/PalatinoBold.ttf') |
67 |
| - .fontSize(25) |
68 |
| - .text('Some text with an embedded font!', 100, 100) |
69 |
| - |
70 |
| - # Add another page |
71 |
| - doc.addPage() |
72 |
| - .fontSize(25) |
73 |
| - .text('Here is some vector graphics...', 100, 100) |
74 |
| - |
75 |
| - # Draw a triangle |
76 |
| - doc.save() |
77 |
| - .moveTo(100, 150) |
78 |
| - .lineTo(100, 250) |
79 |
| - .lineTo(200, 250) |
80 |
| - .fill("#FF3300") |
81 |
| - |
82 |
| - # Apply some transforms and render an SVG path with the 'even-odd' fill rule |
83 |
| - doc.scale(0.6) |
84 |
| - .translate(470, -380) |
85 |
| - .path('M 250,75 L 323,301 131,161 369,161 177,301 z') |
86 |
| - .fill('red', 'even-odd') |
87 |
| - .restore() |
88 |
| - |
89 |
| - # Add some text with annotations |
90 |
| - doc.addPage() |
91 |
| - .fillColor("blue") |
92 |
| - .text('Here is a link!', 100, 100) |
93 |
| - .underline(100, 100, 160, 27, color: "#0000FF") |
94 |
| - .link(100, 100, 160, 27, 'http://google.com/') |
95 |
| - |
96 |
| - # Finalize PDF file |
97 |
| - doc.end() |
98 |
| - |
| 58 | +```coffeescript |
| 59 | +PDFDocument = require 'pdfkit' |
| 60 | + |
| 61 | +# Create a document |
| 62 | +doc = new PDFDocument |
| 63 | + |
| 64 | +# Pipe it's output somewhere, like to a file or HTTP response |
| 65 | +# See below for browser usage |
| 66 | +doc.pipe fs.createWriteStream('output.pdf') |
| 67 | + |
| 68 | +# Embed a font, set the font size, and render some text |
| 69 | +doc.font('fonts/PalatinoBold.ttf') |
| 70 | + .fontSize(25) |
| 71 | + .text('Some text with an embedded font!', 100, 100) |
| 72 | + |
| 73 | +# Add another page |
| 74 | +doc.addPage() |
| 75 | + .fontSize(25) |
| 76 | + .text('Here is some vector graphics...', 100, 100) |
| 77 | + |
| 78 | +# Draw a triangle |
| 79 | +doc.save() |
| 80 | + .moveTo(100, 150) |
| 81 | + .lineTo(100, 250) |
| 82 | + .lineTo(200, 250) |
| 83 | + .fill("#FF3300") |
| 84 | + |
| 85 | +# Apply some transforms and render an SVG path with the 'even-odd' fill rule |
| 86 | +doc.scale(0.6) |
| 87 | + .translate(470, -380) |
| 88 | + .path('M 250,75 L 323,301 131,161 369,161 177,301 z') |
| 89 | + .fill('red', 'even-odd') |
| 90 | + .restore() |
| 91 | + |
| 92 | +# Add some text with annotations |
| 93 | +doc.addPage() |
| 94 | + .fillColor("blue") |
| 95 | + .text('Here is a link!', 100, 100) |
| 96 | + .underline(100, 100, 160, 27, color: "#0000FF") |
| 97 | + .link(100, 100, 160, 27, 'http://google.com/') |
| 98 | + |
| 99 | +# Finalize PDF file |
| 100 | +doc.end() |
| 101 | +``` |
| 102 | + |
99 | 103 | [The PDF output from this example](http://pdfkit.org/demo/out.pdf) (with a few additions) shows the power of PDFKit — producing
|
100 | 104 | complex documents with a very small amount of code. For more, see the `demo` folder and the
|
101 | 105 | [PDFKit programming guide](http://pdfkit.org/docs/getting_started.html).
|
102 | 106 |
|
| 107 | +## Browser Usage |
| 108 | + |
| 109 | +There are two ways to use PDFKit in the browser. The first is to use [Browserify](http://browserify.org/), |
| 110 | +which is a Node module packager for the browser with the familiar `require` syntax. The second is to use |
| 111 | +a prebuilt version of PDFKit, which you can [download from Github](https://github.com/devongovett/pdfkit/releases). |
| 112 | + |
| 113 | +In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a |
| 114 | +[Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object which can be used to store binary data, and |
| 115 | +get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to |
| 116 | +get a Blob from the output of PDFKit, you can use the [blob-stream](https://github.com/devongovett/blob-stream) |
| 117 | +module. |
| 118 | + |
| 119 | +The following example uses Browserify to load `PDFKit` and `blob-stream`, but if you're not using Browserify, |
| 120 | +you can load them in whatever way you'd like (e.g. script tags). |
| 121 | + |
| 122 | +```coffeescript |
| 123 | +# require dependencies |
| 124 | +PDFDocument = require 'pdfkit' |
| 125 | +blobStream = require 'blob-stream' |
| 126 | + |
| 127 | +# create a document the same way as above |
| 128 | +doc = new PDFDocument |
| 129 | + |
| 130 | +# pipe the document to a blob |
| 131 | +stream = doc.pipe(blobStream()) |
| 132 | + |
| 133 | +# add your content to the document here, as usual |
| 134 | + |
| 135 | +# get a blob when you're done |
| 136 | +doc.end() |
| 137 | +stream.on 'finish', -> |
| 138 | + # get a blob you can do whatever you like with |
| 139 | + blob = stream.toBlob('application/pdf') |
| 140 | + |
| 141 | + # or get a blob URL for display in the browser |
| 142 | + url = stream.toBlobURL('application/pdf') |
| 143 | + iframe.src = url |
| 144 | +``` |
| 145 | + |
| 146 | +You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html). |
| 147 | + |
| 148 | +Note that in order to Browserify a project using PDFKit, you need to install the `brfs` module with npm, |
| 149 | +which is used to load built-in font data into the package. It is listed as a `devDependency` in |
| 150 | +PDFKit's `package.json`, so it isn't installed by default for Node users. |
| 151 | +If you forget to install it, Browserify will print an error message. |
| 152 | + |
103 | 153 | ## Documentation
|
104 | 154 |
|
105 | 155 | For complete API documentation and more examples, see the [PDFKit website](http://pdfkit.org/).
|
|
0 commit comments