Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,56 @@ Installation uses the [npm](http://npmjs.org/) package manager. Just type the f

## Example

```coffeescript
PDFDocument = require 'pdfkit'
```javascript
const PDFDocument = require('pdfkit');

# Create a document
doc = new PDFDocument
// Create a document
const 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')
// 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
// 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)
.text('Some text with an embedded font!', 100, 100);

# Add an image, constrain it to a given size, and center it vertically and horizontally
// 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'
});

# Add another page
// Add another page
doc.addPage()
.fontSize(25)
.text('Here is some vector graphics...', 100, 100)
.text('Here is some vector graphics...', 100, 100);

# Draw a triangle
// Draw a triangle
doc.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill("#FF3300")
.fill("#FF3300");

# Apply some transforms and render an SVG path with the 'even-odd' fill rule
// 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()
.restore();

# Add some text with annotations
// 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/')
.underline(100, 100, 160, 27, {color: "#0000FF"})
.link(100, 100, 160, 27, 'http://google.com/');

# Finalize PDF file
doc.end()
// Finalize PDF file
doc.end();
```

[The PDF output from this example](http://pdfkit.org/demo/out.pdf) (with a few additions) shows the power of PDFKit — producing
Expand All @@ -133,28 +133,29 @@ module.
The following example uses Browserify to load `PDFKit` and `blob-stream`, but if you're not using Browserify,
you can load them in whatever way you'd like (e.g. script tags).

```coffeescript
# require dependencies
PDFDocument = require 'pdfkit'
blobStream = require 'blob-stream'
```javascript
// require dependencies
const PDFDocument = require('pdfkit');
const blobStream = require('blob-stream');

# create a document the same way as above
doc = new PDFDocument
// create a document the same way as above
const doc = new PDFDocument;

# pipe the document to a blob
stream = doc.pipe(blobStream())
// pipe the document to a blob
const stream = doc.pipe(blobStream());

# add your content to the document here, as usual
// 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')
// get a blob when you're done
doc.end();
stream.on('finish', function() {
// get a blob you can do whatever you like with
const blob = stream.toBlob('application/pdf');

# or get a blob URL for display in the browser
url = stream.toBlobURL('application/pdf')
iframe.src = url
// or get a blob URL for display in the browser
const url = stream.toBlobURL('application/pdf');
iframe.src = url;
});
```

You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
Expand Down
32 changes: 17 additions & 15 deletions docs/annotations.coffee.md → docs/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ covered by another annotation and the user won't be able to click it.

Here is an example that uses a few of the annotation types.

# Add the link text
// Add the link text
doc.fontSize(25)
.fillColor('blue')
.text('This is a link!', 20, 0)
.text('This is a link!', 20, 0);

# Measure the text
width = doc.widthOfString('This is a link!')
height = doc.currentLineHeight()
// Measure the text
const width = doc.widthOfString('This is a link!');
const height = doc.currentLineHeight();

# Add the underline and link annotations
doc.underline(20, 0, width, height, color: 'blue')
.link(20, 0, width, height, 'http://google.com/')
// Add the underline and link annotations
doc.underline(20, 0, width, height, {color: 'blue'})
.link(20, 0, width, height, 'http://google.com/');

# Create the highlighted text
// Create the highlighted text
doc.moveDown()
.fillColor('black')
.highlight(20, doc.y, doc.widthOfString('This text is highlighted!'), height)
.text('This text is highlighted!')
.text('This text is highlighted!');

# Create the crossed out text
// Create the crossed out text
doc.moveDown()
.strike(20, doc.y, doc.widthOfString('STRIKE!'), height)
.text('STRIKE!')
.text('STRIKE!');

The output of this example looks like this.

Expand All @@ -70,11 +70,13 @@ that is the fault of the PDF spec itself. Calculating a rectangle manually isn't
fun, but PDFKit makes it easier for a few common annotations applied to text, including
links, underlines, and strikes. Here's an example showing two of them:

doc.fontSize 20
.fillColor 'red'
.text 'Another link!', 20, 0,
doc.fontSize(20)
.fillColor('red')
.text('Another link!', 20, 0, {
link: 'http://apple.com/',
underline: true
}
);

The output is as you'd expect:

Expand Down
29 changes: 14 additions & 15 deletions docs/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
const fs = require('fs');
const vm = require('vm');
const md = require('markdown').markdown;
const coffee = require('coffee-script');
const CodeMirror = require('codemirror/addon/runmode/runmode.node');
const PDFDocument = require('../');

process.chdir(__dirname);

// setup code mirror coffeescript mode
const filename = require.resolve('codemirror/mode/coffeescript/coffeescript');
const coffeeMode = fs.readFileSync(filename, 'utf8');
vm.runInNewContext(coffeeMode, {CodeMirror});
// setup code mirror javascript mode
const filename = require.resolve('codemirror/mode/javascript/javascript');
const jsMode = fs.readFileSync(filename, 'utf8');
vm.runInNewContext(jsMode, {CodeMirror});

// style definitions for markdown
const styles = {
Expand Down Expand Up @@ -142,7 +141,7 @@ class Node {
// use code mirror to syntax highlight the code block
var code = this.content[0].text;
this.content = [];
CodeMirror.runMode(code, 'coffeescript', (text, style) => {
CodeMirror.runMode(code, 'javascript', (text, style) => {
const color = colors[style] || colors.default;
const opts = {
color,
Expand All @@ -158,11 +157,11 @@ class Node {

case 'img':
// images are used to generate inline example output
// compiles the coffeescript to JS so it can be run
// stores the JS so it can be run
// in the render method
this.type = 'example';
code = codeBlocks[this.attrs.alt];
if (code) { this.code = coffee.compile(code); }
if (code) { this.code = code; }
this.height = +this.attrs.title || 0;
break;
}
Expand Down Expand Up @@ -266,7 +265,7 @@ class Node {
}
}

// reads and renders a markdown/literate coffeescript file to the document
// reads and renders a markdown/literate javascript file to the document
const render = function(doc, filename) {
codeBlocks = [];
const tree = md.parse(fs.readFileSync(filename, 'utf8'));
Expand Down Expand Up @@ -317,12 +316,12 @@ const renderTitlePage = function(doc) {
const doc = new PDFDocument;
doc.pipe(fs.createWriteStream('guide.pdf'));
renderTitlePage(doc);
render(doc, 'getting_started.coffee.md');
render(doc, 'vector.coffee.md');
render(doc, 'text.coffee.md');
render(doc, 'images.coffee.md');
render(doc, 'outline.coffee.md');
render(doc, 'annotations.coffee.md');
render(doc, 'getting_started.md');
render(doc, 'vector.md');
render(doc, 'text.md');
render(doc, 'images.md');
render(doc, 'outline.md');
render(doc, 'annotations.md');
return doc.end();
})();

Expand Down
Loading