mirror of
https://github.com/foliojs/pdfkit.git
synced 2026-01-25 16:06:44 +00:00
155 lines
9.4 KiB
HTML
155 lines
9.4 KiB
HTML
<!DOCTYPE html><html><head><meta charset="utf-8"><title>PDFKit</title><link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Source+Code+Pro:400,700|Alegreya:700|Merriweather"><link rel="stylesheet" href="/docs/css/index.css"><link rel="stylesheet" href="/docs/css/github.css"></head><body><nav class="sidebar"><ul><li><a class="selected" href="/">Home</a></li><li><a href="/docs/../index.html">Documentation</a><ul><li><a href="/docs/getting_started.html">Getting Started </a></li><li><a href="/docs/paper_sizes.html">Paper Sizes</a></li><li><a href="/docs/vector.html">Vector Graphics </a></li><li><a href="/docs/text.html">Text </a></li><li><a href="/docs/images.html">Images </a></li><li><a href="/docs/outline.html">Outlines </a></li><li><a href="/docs/annotations.html">Annotations </a></li><li><a href="/docs/forms.html">Forms </a></li><li><a href="/docs/destinations.html">Destinations</a></li><li><a href="/docs/attachments.html">Attachments </a></li><li><a href="/docs/accessibility.html">Accessibility</a></li><li><a href="/docs/you_made_it.html">You made it!</a></li></ul></li><li><a href="/docs/guide.pdf">PDF Guide</a></li><li><a href="/examples/kitchen-sink.pdf">Example PDF</a></li><li><a href="/demo/browser.html">Interactive Browser Demo</a></li><li><a href="http://github.com/foliojs/pdfkit">Source Code</a></li></ul></nav><div class="main"><h1 id="pdfkit">PDFKit</h1>
|
|
|
|
<p>A JavaScript PDF generation library for Node and the browser.</p>
|
|
|
|
<h2 id="description">Description</h2>
|
|
|
|
<p>PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable
|
|
documents easy. The API embraces chainability, and includes both low level functions as well as abstractions for higher
|
|
level functionality. The PDFKit API is designed to be simple, so generating complex documents is often as simple as
|
|
a few function calls.</p>
|
|
|
|
<p>Check out some of the <a href="http://pdfkit.org/docs/getting_started.html">documentation and examples</a> to see for yourself!
|
|
You can also read the guide as a <a href="http://pdfkit.org/docs/guide.pdf">self-generated PDF</a> with example output displayed inline.
|
|
If you'd like to see how it was generated, check out the README in the <a href="https://github.com/foliojs/pdfkit/tree/master/docs">docs</a>
|
|
folder.</p>
|
|
|
|
<p>You can also try out an interactive in-browser demo of PDFKit <a href="http://pdfkit.org/demo/browser.html">here</a>.</p>
|
|
|
|
<h2 id="installation">Installation</h2>
|
|
|
|
<p>Installation uses the <a href="http://npmjs.org/">npm</a> package manager. Just type the following command after installing npm.</p>
|
|
|
|
<pre><code>npm install pdfkit</code></pre>
|
|
|
|
<h2 id="features">Features</h2>
|
|
|
|
<ul><li>Vector graphics<ul><li>HTML5 canvas-like API</li><li>Path operations</li><li>SVG path parser for easy path creation</li><li>Transformations</li><li>Linear and radial gradients</li></ul></li><li>Text<ul><li>Line wrapping (with soft hyphen recognition)</li><li>Text alignments</li><li>Bulleted lists</li></ul></li><li>Font embedding<ul><li>Supports TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collections (.ttc), and Datafork TrueType (.dfont) fonts</li><li>Font subsetting</li><li>See <a href="http://github.com/foliojs/fontkit">fontkit</a> for more details on advanced glyph layout support.</li></ul></li><li>Image embedding<ul><li>Supports JPEG and PNG files (including indexed PNGs, and PNGs with transparency)</li></ul></li><li>Annotations<ul><li>Links</li><li>Notes</li><li>Highlights</li><li>Underlines</li><li>etc.</li></ul></li><li>AcroForms</li><li>Outlines</li><li>PDF security<ul><li>Encryption</li><li>Access privileges (printing, copying, modifying, annotating, form filling, content accessibility, document assembly)</li></ul></li><li>Accessibility support (marked content, logical structure, Tagged PDF, PDF/UA)</li></ul>
|
|
|
|
<h2 id="coming_soon!">Coming soon!</h2>
|
|
|
|
<ul><li>Patterns fills</li><li>Higher level APIs for creating tables and laying out content</li><li>More performance optimizations</li><li>Even more awesomeness, perhaps written by you! Please fork this repository and send me pull requests.</li></ul>
|
|
|
|
<h2 id="example">Example</h2>
|
|
|
|
<p><code></code>`javascript
|
|
const PDFDocument = require('pdfkit');
|
|
const fs = require('fs');</p>
|
|
|
|
<p>// Create a document
|
|
const doc = new PDFDocument();</p>
|
|
|
|
<p>// Pipe its output somewhere, like to a file or HTTP response
|
|
// See below for browser usage
|
|
doc.pipe(fs.createWriteStream('output.pdf'));</p>
|
|
|
|
<p>// Embed a font, set the font size, and render some text
|
|
doc
|
|
.font('fonts/PalatinoBold.ttf')
|
|
.fontSize(25)
|
|
.text('Some text with an embedded font!', 100, 100);</p>
|
|
|
|
<p>// Add an image, constrain it to a given size, and center it vertically and horizontally
|
|
doc.image('path/to/image.png', {
|
|
fit: [250, 300],
|
|
align: 'center',
|
|
valign: 'center'
|
|
});</p>
|
|
|
|
<p>// Add another page
|
|
doc
|
|
.addPage()
|
|
.fontSize(25)
|
|
.text('Here is some vector graphics...', 100, 100);</p>
|
|
|
|
<p>// Draw a triangle
|
|
doc
|
|
.save()
|
|
.moveTo(100, 150)
|
|
.lineTo(100, 250)
|
|
.lineTo(200, 250)
|
|
.fill('#FF3300');</p>
|
|
|
|
<p>// Apply some transforms and render an SVG path with the 'even-odd' fill rule
|
|
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();</p>
|
|
|
|
<p>// Add some text with annotations
|
|
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/');</p>
|
|
|
|
<p>// Finalize PDF file
|
|
doc.end();
|
|
<code></code>`</p>
|
|
|
|
<p><a href="http://pdfkit.org/demo/out.pdf">The PDF output from this example</a> (with a few additions) shows the power of PDFKit — producing
|
|
complex documents with a very small amount of code. For more, see the <code>demo</code> folder and the
|
|
<a href="http://pdfkit.org/docs/getting_started.html">PDFKit programming guide</a>.</p>
|
|
|
|
<h2 id="browser_usage">Browser Usage</h2>
|
|
|
|
<p>There are three ways to use PDFKit in the browser:</p>
|
|
|
|
<ul><li>Use <a href="http://browserify.org/">Browserify</a>. See demo <a href="demo/browser.js">source code</a> and <a href="https://github.com/foliojs/pdfkit/blob/master/package.json#L56">build script</a></li><li>Use <a href="https://webpack.js.org/">webpack</a>. See <a href="examples/webpack">complete example</a>.</li><li>Use prebuilt version. Distributed as <code>pdfkit.standalone.js</code> file in the <a href="https://github.com/foliojs/pdfkit/releases">releases</a> or in the package <code>js</code> folder.</li></ul>
|
|
|
|
<p>In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a
|
|
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob">Blob</a> object which can be used to store binary data, and
|
|
get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to
|
|
get a Blob from the output of PDFKit, you can use the <a href="https://github.com/devongovett/blob-stream">blob-stream</a>
|
|
module.</p>
|
|
|
|
<p>The following example uses Browserify or webpack to load <code>PDFKit</code> and <code>blob-stream</code>. See <a href="https://codepen.io/blikblum/pen/gJNWMg?editors=1010">here</a> and <a href="https://codepen.io/blikblum/pen/YboVNq?editors=1010">here</a> for examples
|
|
of prebuilt version usage.</p>
|
|
|
|
<p><code></code>`javascript
|
|
// require dependencies
|
|
const PDFDocument = require('pdfkit');
|
|
const blobStream = require('blob-stream');</p>
|
|
|
|
<p>// create a document the same way as above
|
|
const doc = new PDFDocument();</p>
|
|
|
|
<p>// pipe the document to a blob
|
|
const stream = doc.pipe(blobStream());</p>
|
|
|
|
<p>// add your content to the document here, as usual</p>
|
|
|
|
<p>// 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');</p>
|
|
|
|
<p> // or get a blob URL for display in the browser
|
|
const url = stream.toBlobURL('application/pdf');
|
|
iframe.src = url;
|
|
});
|
|
<code></code>`</p>
|
|
|
|
<p>You can see an interactive in-browser demo of PDFKit <a href="http://pdfkit.org/demo/browser.html">here</a>.</p>
|
|
|
|
<p>Note that in order to Browserify a project using PDFKit, you need to install the <code>brfs</code> module with npm,
|
|
which is used to load built-in font data into the package. It is listed as a <code>devDependency</code> in
|
|
PDFKit's <code>package.json</code>, so it isn't installed by default for Node users.
|
|
If you forget to install it, Browserify will print an error message.</p>
|
|
|
|
<h2 id="documentation">Documentation</h2>
|
|
|
|
<p>For complete API documentation and more examples, see the <a href="http://pdfkit.org/">PDFKit website</a>.</p>
|
|
|
|
<h2 id="license">License</h2>
|
|
|
|
<p>PDFKit is available under the MIT license.</p><nav><a class="next" href="/docs/getting_started.html">Next</a></nav></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script><script src="/docs/js/scroll.js"></script><script src="/docs/js/highlight.pack.js"></script><script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
|
ga('create', 'UA-48340245-1', 'pdfkit.org');
|
|
ga('send', 'pageview');</script></body></html> |