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

Commit c09cd64

Browse files
authored
Merge pull request foliojs#884 from blikblum/cleanup-docs
Complete coffee -> JS conversion
2 parents d5d8e3c + 3eae185 commit c09cd64

12 files changed

Lines changed: 297 additions & 303 deletions

README.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,56 +62,56 @@ Installation uses the [npm](http://npmjs.org/) package manager. Just type the f
6262

6363
## Example
6464

65-
```coffeescript
66-
PDFDocument = require 'pdfkit'
65+
```javascript
66+
const PDFDocument = require('pdfkit');
6767

68-
# Create a document
69-
doc = new PDFDocument
68+
// Create a document
69+
const doc = new PDFDocument;
7070

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

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

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

87-
# Add another page
87+
// Add another page
8888
doc.addPage()
8989
.fontSize(25)
90-
.text('Here is some vector graphics...', 100, 100)
90+
.text('Here is some vector graphics...', 100, 100);
9191

92-
# Draw a triangle
92+
// Draw a triangle
9393
doc.save()
9494
.moveTo(100, 150)
9595
.lineTo(100, 250)
9696
.lineTo(200, 250)
97-
.fill("#FF3300")
97+
.fill("#FF3300");
9898

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

106-
# Add some text with annotations
106+
// Add some text with annotations
107107
doc.addPage()
108108
.fillColor("blue")
109109
.text('Here is a link!', 100, 100)
110-
.underline(100, 100, 160, 27, color: "#0000FF")
111-
.link(100, 100, 160, 27, 'http://google.com/')
110+
.underline(100, 100, 160, 27, {color: "#0000FF"})
111+
.link(100, 100, 160, 27, 'http://google.com/');
112112

113-
# Finalize PDF file
114-
doc.end()
113+
// Finalize PDF file
114+
doc.end();
115115
```
116116

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

136-
```coffeescript
137-
# require dependencies
138-
PDFDocument = require 'pdfkit'
139-
blobStream = require 'blob-stream'
136+
```javascript
137+
// require dependencies
138+
const PDFDocument = require('pdfkit');
139+
const blobStream = require('blob-stream');
140140

141-
# create a document the same way as above
142-
doc = new PDFDocument
141+
// create a document the same way as above
142+
const doc = new PDFDocument;
143143

144-
# pipe the document to a blob
145-
stream = doc.pipe(blobStream())
144+
// pipe the document to a blob
145+
const stream = doc.pipe(blobStream());
146146

147-
# add your content to the document here, as usual
147+
// add your content to the document here, as usual
148148

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

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

160161
You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ covered by another annotation and the user won't be able to click it.
3737

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

40-
# Add the link text
40+
// Add the link text
4141
doc.fontSize(25)
4242
.fillColor('blue')
43-
.text('This is a link!', 20, 0)
43+
.text('This is a link!', 20, 0);
4444

45-
# Measure the text
46-
width = doc.widthOfString('This is a link!')
47-
height = doc.currentLineHeight()
45+
// Measure the text
46+
const width = doc.widthOfString('This is a link!');
47+
const height = doc.currentLineHeight();
4848

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

53-
# Create the highlighted text
53+
// Create the highlighted text
5454
doc.moveDown()
5555
.fillColor('black')
5656
.highlight(20, doc.y, doc.widthOfString('This text is highlighted!'), height)
57-
.text('This text is highlighted!')
57+
.text('This text is highlighted!');
5858

59-
# Create the crossed out text
59+
// Create the crossed out text
6060
doc.moveDown()
6161
.strike(20, doc.y, doc.widthOfString('STRIKE!'), height)
62-
.text('STRIKE!')
62+
.text('STRIKE!');
6363

6464
The output of this example looks like this.
6565

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

73-
doc.fontSize 20
74-
.fillColor 'red'
75-
.text 'Another link!', 20, 0,
73+
doc.fontSize(20)
74+
.fillColor('red')
75+
.text('Another link!', 20, 0, {
7676
link: 'http://apple.com/',
7777
underline: true
78+
}
79+
);
7880

7981
The output is as you'd expect:
8082

docs/generate.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
const fs = require('fs');
1010
const vm = require('vm');
1111
const md = require('markdown').markdown;
12-
const coffee = require('coffee-script');
1312
const CodeMirror = require('codemirror/addon/runmode/runmode.node');
1413
const PDFDocument = require('../');
1514

1615
process.chdir(__dirname);
1716

18-
// setup code mirror coffeescript mode
19-
const filename = require.resolve('codemirror/mode/coffeescript/coffeescript');
20-
const coffeeMode = fs.readFileSync(filename, 'utf8');
21-
vm.runInNewContext(coffeeMode, {CodeMirror});
17+
// setup code mirror javascript mode
18+
const filename = require.resolve('codemirror/mode/javascript/javascript');
19+
const jsMode = fs.readFileSync(filename, 'utf8');
20+
vm.runInNewContext(jsMode, {CodeMirror});
2221

2322
// style definitions for markdown
2423
const styles = {
@@ -142,7 +141,7 @@ class Node {
142141
// use code mirror to syntax highlight the code block
143142
var code = this.content[0].text;
144143
this.content = [];
145-
CodeMirror.runMode(code, 'coffeescript', (text, style) => {
144+
CodeMirror.runMode(code, 'javascript', (text, style) => {
146145
const color = colors[style] || colors.default;
147146
const opts = {
148147
color,
@@ -158,11 +157,11 @@ class Node {
158157

159158
case 'img':
160159
// images are used to generate inline example output
161-
// compiles the coffeescript to JS so it can be run
160+
// stores the JS so it can be run
162161
// in the render method
163162
this.type = 'example';
164163
code = codeBlocks[this.attrs.alt];
165-
if (code) { this.code = coffee.compile(code); }
164+
if (code) { this.code = code; }
166165
this.height = +this.attrs.title || 0;
167166
break;
168167
}
@@ -266,7 +265,7 @@ class Node {
266265
}
267266
}
268267

269-
// reads and renders a markdown/literate coffeescript file to the document
268+
// reads and renders a markdown/literate javascript file to the document
270269
const render = function(doc, filename) {
271270
codeBlocks = [];
272271
const tree = md.parse(fs.readFileSync(filename, 'utf8'));
@@ -317,12 +316,12 @@ const renderTitlePage = function(doc) {
317316
const doc = new PDFDocument;
318317
doc.pipe(fs.createWriteStream('guide.pdf'));
319318
renderTitlePage(doc);
320-
render(doc, 'getting_started.coffee.md');
321-
render(doc, 'vector.coffee.md');
322-
render(doc, 'text.coffee.md');
323-
render(doc, 'images.coffee.md');
324-
render(doc, 'outline.coffee.md');
325-
render(doc, 'annotations.coffee.md');
319+
render(doc, 'getting_started.md');
320+
render(doc, 'vector.md');
321+
render(doc, 'text.md');
322+
render(doc, 'images.md');
323+
render(doc, 'outline.md');
324+
render(doc, 'annotations.md');
326325
return doc.end();
327326
})();
328327

0 commit comments

Comments
 (0)