Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.

Commit eda7bc5

Browse files
authored
Resolve conflicts with foliojs master
2 parents 147ebfd + b1a15b2 commit eda7bc5

101 files changed

Lines changed: 94280 additions & 1678 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [["env", {"modules": false, "targets": {"node": "6.10"}}]],
3+
"env": {
4+
"test": {
5+
"presets": [["env", {"targets": {"node": "6.10"}}]]
6+
}
7+
}
8+
}

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "eslint:recommended"
3+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ demo/bundle.js
1010
!demo/browser.html
1111
dist
1212
*.b64.afm
13+
.vscode
14+
coverage

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ demo/
44
lib/
55
docs/
66
playground/
7+
.vscode/
8+
coverage/
9+
tests/
710
index.js
11+
index.html
12+
yarn.lock
13+
rollup.config.js
14+
.eslintrc

README.md

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
A JavaScript PDF generation library for Node and the browser.
44

5-
[![](https://img.shields.io/gratipay/devongovett.svg)](https://gratipay.com/devongovett)
6-
75
## Description
86

9-
PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy.
10-
It's written in CoffeeScript, but you can choose to use the API in plain 'ol JavaScript if you like. The API embraces
11-
chainability, and includes both low level functions as well as abstractions for higher level functionality. The PDFKit API
7+
PDFKit is a PDF document generation library for Node and the browser that makes creating complex, multi-page, printable documents easy.
8+
It's written in CoffeeScript, but you can choose to use the API in plain 'ol JavaScript if you like. The API embraces
9+
chainability, and includes both low level functions as well as abstractions for higher level functionality. The PDFKit API
1210
is designed to be simple, so generating complex documents is often as simple as a few function calls.
1311

1412
Check out some of the [documentation and examples](http://pdfkit.org/docs/getting_started.html) to see for yourself!
@@ -48,72 +46,74 @@ Installation uses the [npm](http://npmjs.org/) package manager. Just type the f
4846
* Highlights
4947
* Underlines
5048
* etc.
51-
49+
* Outlines
50+
* PDF security
51+
* Encryption
52+
* Access privileges (printing, copying, modifying, annotating, form filling, content accessibility, document assembly)
53+
5254
## Coming soon!
5355

5456
* Patterns fills
55-
* Outlines
56-
* PDF Security
5757
* Higher level APIs for creating tables and laying out content
5858
* More performance optimizations
5959
* Even more awesomeness, perhaps written by you! Please fork this repository and send me pull requests.
60-
60+
6161
## Example
6262

63-
```coffeescript
64-
PDFDocument = require 'pdfkit'
63+
```javascript
64+
const PDFDocument = require('pdfkit');
6565

66-
# Create a document
67-
doc = new PDFDocument
66+
// Create a document
67+
const doc = new PDFDocument;
6868

69-
# Pipe its output somewhere, like to a file or HTTP response
70-
# See below for browser usage
71-
doc.pipe fs.createWriteStream('output.pdf')
69+
// Pipe its output somewhere, like to a file or HTTP response
70+
// See below for browser usage
71+
doc.pipe(fs.createWriteStream('output.pdf'));
7272

73-
# Embed a font, set the font size, and render some text
73+
// Embed a font, set the font size, and render some text
7474
doc.font('fonts/PalatinoBold.ttf')
7575
.fontSize(25)
76-
.text('Some text with an embedded font!', 100, 100)
76+
.text('Some text with an embedded font!', 100, 100);
7777

78-
# Add an image, constrain it to a given size, and center it vertically and horizontally
78+
// Add an image, constrain it to a given size, and center it vertically and horizontally
7979
doc.image('path/to/image.png', {
8080
fit: [250, 300],
8181
align: 'center',
8282
valign: 'center'
8383
});
8484

85-
# Add another page
85+
// Add another page
8686
doc.addPage()
8787
.fontSize(25)
88-
.text('Here is some vector graphics...', 100, 100)
88+
.text('Here is some vector graphics...', 100, 100);
8989

90-
# Draw a triangle
90+
// Draw a triangle
9191
doc.save()
9292
.moveTo(100, 150)
9393
.lineTo(100, 250)
9494
.lineTo(200, 250)
95-
.fill("#FF3300")
95+
.fill("#FF3300");
9696

97-
# Apply some transforms and render an SVG path with the 'even-odd' fill rule
97+
// Apply some transforms and render an SVG path with the 'even-odd' fill rule
9898
doc.scale(0.6)
9999
.translate(470, -380)
100100
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
101101
.fill('red', 'even-odd')
102-
.restore()
102+
.restore();
103103

104-
# Add some text with annotations
104+
// Add some text with annotations
105105
doc.addPage()
106106
.fillColor("blue")
107107
.text('Here is a link!', 100, 100)
108-
.underline(100, 100, 160, 27, color: "#0000FF")
109-
.link(100, 100, 160, 27, 'http://google.com/')
108+
.underline(100, 100, 160, 27, {color: "#0000FF"})
109+
.link(100, 100, 160, 27, 'http://google.com/');
110110

111-
# Finalize PDF file
112-
doc.end()
111+
// Finalize PDF file
112+
doc.end();
113113
```
114-
115-
[The PDF output from this example](http://pdfkit.org/demo/out.pdf) (with a few additions) shows the power of PDFKit — producing
116-
complex documents with a very small amount of code. For more, see the `demo` folder and the
114+
115+
[The PDF output from this example](http://pdfkit.org/demo/out.pdf) (with a few additions) shows the power of PDFKit — producing
116+
complex documents with a very small amount of code. For more, see the `demo` folder and the
117117
[PDFKit programming guide](http://pdfkit.org/docs/getting_started.html).
118118

119119
## Browser Usage
@@ -122,44 +122,45 @@ There are two ways to use PDFKit in the browser. The first is to use [Browserif
122122
which is a Node module packager for the browser with the familiar `require` syntax. The second is to use
123123
a prebuilt version of PDFKit, which you can [download from Github](https://github.com/devongovett/pdfkit/releases).
124124

125-
In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a
125+
In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a
126126
[Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object which can be used to store binary data, and
127-
get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to
127+
get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to
128128
get a Blob from the output of PDFKit, you can use the [blob-stream](https://github.com/devongovett/blob-stream)
129129
module.
130130

131-
The following example uses Browserify to load `PDFKit` and `blob-stream`, but if you're not using Browserify,
131+
The following example uses Browserify to load `PDFKit` and `blob-stream`, but if you're not using Browserify,
132132
you can load them in whatever way you'd like (e.g. script tags).
133133

134-
```coffeescript
135-
# require dependencies
136-
PDFDocument = require 'pdfkit'
137-
blobStream = require 'blob-stream'
134+
```javascript
135+
// require dependencies
136+
const PDFDocument = require('pdfkit');
137+
const blobStream = require('blob-stream');
138138

139-
# create a document the same way as above
140-
doc = new PDFDocument
139+
// create a document the same way as above
140+
const doc = new PDFDocument;
141141

142-
# pipe the document to a blob
143-
stream = doc.pipe(blobStream())
142+
// pipe the document to a blob
143+
const stream = doc.pipe(blobStream());
144144

145-
# add your content to the document here, as usual
145+
// add your content to the document here, as usual
146146

147-
# get a blob when you're done
148-
doc.end()
149-
stream.on 'finish', ->
150-
# get a blob you can do whatever you like with
151-
blob = stream.toBlob('application/pdf')
147+
// get a blob when you're done
148+
doc.end();
149+
stream.on('finish', function() {
150+
// get a blob you can do whatever you like with
151+
const blob = stream.toBlob('application/pdf');
152152

153-
# or get a blob URL for display in the browser
154-
url = stream.toBlobURL('application/pdf')
155-
iframe.src = url
153+
// or get a blob URL for display in the browser
154+
const url = stream.toBlobURL('application/pdf');
155+
iframe.src = url;
156+
});
156157
```
157158

158159
You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
159160

160-
Note that in order to Browserify a project using PDFKit, you need to install the `brfs` module with npm,
161-
which is used to load built-in font data into the package. It is listed as a `devDependency` in
162-
PDFKit's `package.json`, so it isn't installed by default for Node users.
161+
Note that in order to Browserify a project using PDFKit, you need to install the `brfs` module with npm,
162+
which is used to load built-in font data into the package. It is listed as a `devDependency` in
163+
PDFKit's `package.json`, so it isn't installed by default for Node users.
163164
If you forget to install it, Browserify will print an error message.
164165

165166
## Documentation

demo/out.pdf

640 KB
Binary file not shown.

demo/png-benchmark.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const PDFDocument = require('../');
2+
const fs = require('fs');
3+
4+
const doc = new PDFDocument();
5+
6+
// files with alpha channel -> uses zlib.deflate
7+
const files = [
8+
'test.png',
9+
'test3.png'
10+
];
11+
12+
const filesData = files.map(fileName => {
13+
return fs.readFileSync(`images/${fileName}`);
14+
});
15+
16+
const iterationCount = 100;
17+
18+
console.time('png-bench')
19+
20+
for (let i = 0; i < iterationCount; i++) {
21+
filesData.forEach(data => {
22+
doc.image(data)
23+
doc.addPage()
24+
})
25+
}
26+
27+
doc.on('data', () => {})
28+
29+
doc.on('end', () => {
30+
console.timeEnd('png-bench');
31+
});
32+
33+
doc.end();

demo/test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var PDFDocument = require('../');
2+
var tiger = require('./tiger');
3+
var fs = require('fs');
4+
5+
// Create a new PDFDocument
6+
var doc = new PDFDocument;
7+
8+
doc.pipe(fs.createWriteStream('out.pdf'));
9+
10+
// Set some meta data
11+
doc.info['Title'] = 'Test Document';
12+
13+
doc.info['Author'] = 'Devon Govett';
14+
15+
// Register a font name for use later
16+
doc.registerFont('Palatino', 'fonts/PalatinoBold.ttf');
17+
18+
// Set the font, draw some text, and embed an image
19+
doc.font('Palatino').fontSize(25).text('Some text with an embedded font!', 100, 100).fontSize(18).text('PNG and JPEG images:').image('images/test.png', 100, 160, {
20+
width: 412
21+
}).image('images/test.jpeg', 190, 400, {
22+
height: 300
23+
});
24+
25+
// Add another page
26+
doc.addPage().fontSize(25).text('Here is some vector graphics...', 100, 100);
27+
28+
// Draw a triangle and a circle
29+
doc.save().moveTo(100, 150).lineTo(100, 250).lineTo(200, 250).fill("#FF3300");
30+
31+
doc.circle(280, 200, 50).fill("#6600FF");
32+
33+
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(); // render an SVG path // fill using the even-odd winding rule
34+
35+
var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit. Ut nec accumsan nisl. Suspendisse rhoncus nisl posuere tortor tempus et dapibus elit porta. Cras leo neque, elementum a rhoncus ut, vestibulum non nibh. Phasellus pretium justo turpis. Etiam vulputate, odio vitae tincidunt ultricies, eros odio dapibus nisi, ut tincidunt lacus arcu eu elit. Aenean velit erat, vehicula eget lacinia ut, dignissim non tellus. Aliquam nec lacus mi, sed vestibulum nunc. Suspendisse potenti. Curabitur vitae sem turpis. Vestibulum sed neque eget dolor dapibus porttitor at sit amet sem. Fusce a turpis lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;\nMauris at ante tellus. Vestibulum a metus lectus. Praesent tempor purus a lacus blandit eget gravida ante hendrerit. Cras et eros metus. Sed commodo malesuada eros, vitae interdum augue semper quis. Fusce id magna nunc. Curabitur sollicitudin placerat semper. Cras et mi neque, a dignissim risus. Nulla venenatis porta lacus, vel rhoncus lectus tempor vitae. Duis sagittis venenatis rutrum. Curabitur tempor massa tortor.';
36+
37+
// Draw some text wrapped to 412 points wide
38+
doc.text('And here is some wrapped text...', 100, 300).font('Helvetica', 13).moveDown().text(loremIpsum, { // move down 1 line
39+
width: 412,
40+
align: 'justify',
41+
indent: 30,
42+
paragraphGap: 5
43+
});
44+
45+
// Add another page, and set the font back
46+
doc.addPage().font('Palatino', 25).text('Rendering some SVG paths...', 100, 100).translate(220, 300);
47+
48+
var i, len, part;
49+
// Render each path that makes up the tiger image
50+
for (i = 0, len = tiger.length; i < len; i++) {
51+
part = tiger[i];
52+
doc.save();
53+
doc.path(part.path); // render an SVG path
54+
if (part['stroke-width']) {
55+
doc.lineWidth(part['stroke-width']);
56+
}
57+
if (part.fill !== 'none' && part.stroke !== 'none') {
58+
doc.fillAndStroke(part.fill, part.stroke);
59+
} else {
60+
if (part.fill !== 'none') {
61+
doc.fill(part.fill);
62+
}
63+
if (part.stroke !== 'none') {
64+
doc.stroke(part.stroke);
65+
}
66+
}
67+
doc.restore();
68+
}
69+
70+
// Add some text with annotations
71+
doc.addPage().fillColor("blue").text('Here is a link!', 100, 100, {
72+
link: 'http://google.com/',
73+
underline: true
74+
});
75+
76+
77+
// Add a list with a font loaded from a TrueType collection file
78+
doc.fillColor('#000').font('fonts/Chalkboard.ttc', 'Chalkboard', 16).list(['One', 'Two', 'Three'], 100, 150);
79+
80+
doc.end();

docs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# PDFKit Guide
2+
3+
The PDFKit guide can be read a number of ways. The first is online at [pdfkit.org](http://pdfkit.org/).
4+
You can also read the guide in PDF form, in this directory or [online](http://pdfkit.org/docs/guide.pdf).
5+
6+
Both the website and the PDF guide are generated from the Literate CoffeeScript (runnable Markdown) files
7+
in this directory. The examples are actually run when generating the PDF in order to show the results inline.
8+
The `generate.js` file in this directory is actually quite short. It parses the markdown files into a
9+
tree structure using [markdown-js](https://github.com/evilstreak/markdown-js), syntax highlights the code
10+
examples using [codemirror](https://github.com/marijnh/codemirror), compiles and runs the code examples and puts the results
11+
inline, and generates the PDF using PDFKit. You can read the generator script source code to get a feeling for
12+
how you might do something slightly more complex than the guide itself shows.
13+
14+
The markdown syntax used is pretty much standard, with a couple tweaks.
15+
16+
1. Code example output is references using the image notation, using the alt text as the example number starting from
17+
zero in the current file, and the title as the example output height. E.g. `![x](name "height")`.
18+
19+
2. Page breaks are added before `h1` and `h2`s, unless there are two in a row. `h3` is treated the same as `h2` but
20+
can be used to avoid this in the case you need multiple `h2`s on the same page.
21+
22+
3. The horizontal rule syntax (`* * *`) denotes an explicit page break

0 commit comments

Comments
 (0)