@@ -13,21 +13,21 @@ Creating a PDFKit document is quite simple. Just require the `pdfkit` module
1313in your CoffeeScript or JavaScript source file and create an instance of the
1414`PDFDocument` class.
1515
16- PDFDocument = require ' pdfkit'
17- doc = new PDFDocument
16+ const PDFDocument = require ( ' pdfkit' );
17+ const doc = new PDFDocument ;
1818
1919`PDFDocument` instances are readable Node streams. They don't get saved anywhere automatically,
2020but you can call the `pipe` method to send the output of the PDF document to another
2121writable Node stream as it is being written. When you're done with your document, call
2222the `end` method to finalize it. Here is an example showing how to pipe to a file or an HTTP response.
2323
24- doc .pipe fs .createWriteStream (' /path/to/file.pdf' ) # write to PDF
25- doc .pipe res # HTTP response
24+ doc .pipe ( fs .createWriteStream (' /path/to/file.pdf' )); // write to PDF
25+ doc .pipe ( res); // HTTP response
2626
27- # add stuff to PDF here using methods described below...
27+ // add stuff to PDF here using methods described below...
2828
29- # finalize the PDF and end the stream
30- doc .end ()
29+ // finalize the PDF and end the stream
30+ doc .end ();
3131
3232The `write` and `output` methods found in PDFKit before version 0.5 are now deprecated.
3333
@@ -48,27 +48,28 @@ To get a Blob from a `PDFDocument`, you should pipe it to a [blob-stream](https:
4848which is a module that generates a Blob from any Node-style stream. The following example uses Browserify to load
4949`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).
5050
51- # require dependencies
52- PDFDocument = require ' pdfkit'
53- blobStream = require ' blob-stream'
51+ // require dependencies
52+ const PDFDocument = require ( ' pdfkit' );
53+ const blobStream = require ( ' blob-stream' );
5454
55- # create a document the same way as above
56- doc = new PDFDocument
55+ // create a document the same way as above
56+ const doc = new PDFDocument ;
5757
58- # pipe the document to a blob
59- stream = doc .pipe (blobStream ())
58+ // pipe the document to a blob
59+ const stream = doc .pipe (blobStream ());
6060
61- # add your content to the document here, as usual
61+ // add your content to the document here, as usual
6262
63- # get a blob when you're done
64- doc .end ()
65- stream .on ' finish' , ->
66- # get a blob you can do whatever you like with
67- blob = stream .toBlob (' application/pdf' )
63+ // get a blob when you' re done
64+ doc.end();
65+ stream.on( ' finish' , function() {
66+ // get a blob you can do whatever you like with
67+ const blob = stream.toBlob(' application/ pdf' );
6868
69- # or get a blob URL for display in the browser
70- url = stream .toBlobURL (' application/pdf' )
71- iframe .src = url
69+ // or get a blob URL for display in the browser
70+ const url = stream.toBlobURL(' application/ pdf' );
71+ iframe.src = url;
72+ });
7273
7374You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
7475
@@ -87,8 +88,7 @@ quite simple!
8788
8889To add some content every time a page is created, either by calling `addPage()` or automatically, you can use the `pageAdded` event.
8990
90- doc .on ' pageAdded' , ->
91- doc .text " Page Title"
91+ doc.on(' pageAdded' , () => doc.text("Page Title"));
9292
9393You can also set some options for the page, such as its size and orientation.
9494
@@ -110,18 +110,20 @@ on all sides.
110110
111111For example:
112112
113- # Add a 50 point margin on all sides
114- doc .addPage
115- margin : 50
113+ // Add a 50 point margin on all sides
114+ doc.addPage({
115+ margin: 50});
116116
117117
118- # Add different margins on each side
119- doc .addPage
120- margins :
121- top : 50
122- bottom : 50
123- left : 72
118+ // Add different margins on each side
119+ doc.addPage({
120+ margins: {
121+ top: 50,
122+ bottom: 50,
123+ left: 72,
124124 right: 72
125+ }
126+ });
125127
126128## Switching to previous pages
127129
@@ -142,28 +144,31 @@ never need to call it. Finally, there is a `bufferedPageRange` method, which re
142144of pages that are currently buffered. Here is a small example that shows how you might add page
143145numbers to a document.
144146
145- # create a document, and enable bufferPages mode
146- doc = new PDFDocument
147- bufferPages : true
147+ // create a document , and enable bufferPages mode
148+ let i;
149+ let end;
150+ const doc = new PDFDocument ({
151+ bufferPages : true });
148152
149- # add some content...
150- doc .addPage ()
151- # ...
152- doc .addPage ()
153+ // add some content...
154+ doc .addPage ();
155+ // ...
156+ doc .addPage ();
153157
154- # see the range of buffered pages
155- range = doc .bufferedPageRange () # => { start: 0, count: 2 }
158+ // see the range of buffered pages
159+ const range = doc .bufferedPageRange (); // => { start : 0 , count : 2 }
156160
157- for i in [range .start ... range .start + range .count ]
158- doc .switchToPage (i)
159- doc .text " Page #{ i + 1 } of #{ range .count } "
161+ for (i = range .start , end = range .start + range .count , range .start <= end; i < end; i++ ;) {
162+ doc .switchToPage (i);
163+ doc .text (` Page ${i + 1 } of ${range .count } ` );
164+ }
160165
161- # manually flush pages that have been buffered
162- doc .flushPages ()
166+ // manually flush pages that have been buffered
167+ doc .flushPages ();
163168
164- # or, if you are at the end of the document anyway,
165- # doc.end() will call it for you automatically.
166- doc .end ()
169+ // or , if you are at the end of the document anyway,
170+ // doc .end () will call it for you automatically.
171+ doc .end ();
167172
168173## Setting document metadata
169174
0 commit comments