mirror of
https://github.com/foliojs/pdfkit.git
synced 2026-01-25 16:06:44 +00:00
139 lines
8.0 KiB
HTML
139 lines
8.0 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 href="/" class="selected">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/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/annotations.html">Annotations </a></li></ul></li><li><a href="/docs/guide.pdf">PDF Guide</a></li><li><a href="/demo/out.pdf">Example PDF</a></li><li><a href="/demo/browser.html">Interactive Browser Demo</a></li><li><a href="http://github.com/devongovett/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>
|
|
|
|
<p><a href="https://gratipay.com/devongovett"><img alt="" src="https://img.shields.io/gratipay/devongovett.svg"/></a></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.
|
|
It's written in CoffeeScript, but you can choose to use the API in plain 'ol JavaScript if you like. 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/devongovett/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</li><li>Text alignments</li><li>Bulleted lists</li></ul></li><li>Font embedding<ul><li>Supports TrueType (.ttf), TrueType Collections (.ttc), and Datafork TrueType (.dfont) fonts</li><li>Font subsetting</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></ul>
|
|
|
|
<h2 id="coming_soon!">Coming soon!</h2>
|
|
|
|
<ul><li>Patterns fills</li><li>Outlines</li><li>PDF Security</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>
|
|
|
|
<pre><code>PDFDocument = require 'pdfkit'
|
|
|
|
# Create a document
|
|
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')
|
|
.fontSize(25)
|
|
.text('Some text with an embedded font!', 100, 100)
|
|
|
|
# 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' 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()
|
|
|
|
# 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/')
|
|
|
|
# Finalize PDF file
|
|
doc.end()</code></pre>
|
|
|
|
<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 two ways to use PDFKit in the browser. The first is to use <a href="http://browserify.org/">Browserify</a>,
|
|
which is a Node module packager for the browser with the familiar <code>require</code> syntax. The second is to use
|
|
a prebuilt version of PDFKit, which you can <a href="https://github.com/devongovett/pdfkit/releases">download from Github</a>.</p>
|
|
|
|
<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 to load <code>PDFKit</code> and <code>blob-stream</code>, but if you're not using Browserify,
|
|
you can load them in whatever way you'd like (e.g. script tags).</p>
|
|
|
|
<pre><code># require dependencies
|
|
PDFDocument = require 'pdfkit'
|
|
blobStream = require 'blob-stream'
|
|
|
|
# create a document the same way as above
|
|
doc = new PDFDocument
|
|
|
|
# pipe the document to a blob
|
|
stream = doc.pipe(blobStream())
|
|
|
|
# add your content to the document here, as usual
|
|
|
|
# get a blob when you're done
|
|
doc.end()
|
|
stream.on 'finish', ->
|
|
# get a blob you can do whatever you like with
|
|
blob = stream.toBlob('application/pdf')
|
|
|
|
# or get a blob URL for display in the browser
|
|
url = stream.toBlobURL('application/pdf')
|
|
iframe.src = url</code></pre>
|
|
|
|
<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 href="/docs/getting_started.html" class="next">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> |