Skip to content

Commit 83775c8

Browse files
committed
Add comments to examples in README
1 parent 8f2a6c7 commit 83775c8

File tree

1 file changed

+87
-29
lines changed

1 file changed

+87
-29
lines changed

README.md

+87-29
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
- [Features](#features)
4141
- [Motivation](#motivation)
42-
- [Examples](#examples)
42+
- [Usage Examples](#usage-examples)
4343
- [Create Document](#create-document)
4444
- [Modify Document](#modify-document)
4545
- [Copy Pages](#copy-pages)
@@ -60,9 +60,8 @@
6060
- Draw Text
6161
- Draw Images
6262
- Draw Vector Graphics
63-
- Embed Fonts (supports UTF-8 and UTF-16 character sets)
6463
- Measure width and height of text
65-
- Embed Images
64+
- Embed Fonts (supports UTF-8 and UTF-16 character sets)
6665

6766
## Motivation
6867

@@ -75,7 +74,7 @@ Two of `pdf-lib`'s distinguishing features are:
7574

7675
There are [other](#prior-art) good open source JavaScript PDF libraries available. However, most of them can only _create_ documents, they cannot _modify_ existing ones. And many of them only work in particular environments.
7776

78-
## Examples
77+
## Usage Examples
7978

8079
### Create Document
8180

@@ -85,13 +84,19 @@ _This example produces [this PDF](assets/pdfs/examples/document_creation.pdf)._
8584
```js
8685
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib'
8786

87+
// Create a new PDFDocument
8888
const pdfDoc = await PDFDocument.create()
8989

90+
// Embed the Times Roman font
9091
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
9192

93+
// Add a blank page to the document
9294
const page = pdfDoc.addPage()
93-
const { height } = page.getSize()
9495

96+
// Get the width and height of the page
97+
const { width, height } = page.getSize()
98+
99+
// Draw a string of text toward the top of the page
95100
const fontSize = 30
96101
page.drawText('Creating PDFs in JavaScript is awesome!', {
97102
x: 50,
@@ -101,7 +106,13 @@ page.drawText('Creating PDFs in JavaScript is awesome!', {
101106
color: rgb(0, 0.53, 0.71),
102107
})
103108

109+
// Serialize the PDFDocument to bytes (a Uint8Array)
104110
const pdfBytes = await pdfDoc.save()
111+
112+
// For example, `pdfBytes` can be:
113+
// • Written to a file in Node
114+
// • Downloaded from the browser
115+
// • Rendered in an <iframe>
105116
```
106117

107118
### Modify Document
@@ -112,20 +123,26 @@ _This example produces [this PDF](assets/pdfs/examples/document_modification.pdf
112123
```js
113124
import { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib';
114125

115-
// This should be a Uint8Array or ArrayBuffer.
116-
// This data can be obtained in a number of different ways.
117-
// If your running in a Node environment, you could use fs.readFile().
118-
// In the browser, you could make a fetch() call and use res.arrayBuffer().
126+
// This should be a Uint8Array or ArrayBuffer
127+
// This data can be obtained in a number of different ways
128+
// If your running in a Node environment, you could use fs.readFile()
129+
// In the browser, you could make a fetch() call and use res.arrayBuffer()
119130
const existingPdfBytes = ...
120131

132+
// Load a PDFDocument from the existing PDF bytes
121133
const pdfDoc = await PDFDocument.load(existingPdfBytes)
122134

135+
// Embed the Helvetica font
123136
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)
124137

138+
// Get the first page of the document
125139
const pages = pdfDoc.getPages()
126140
const firstPage = pages[0]
127-
const { height } = firstPage.getSize()
128141

142+
// Get the width and height of the first page
143+
const { width, height } = firstPage.getSize()
144+
145+
// Draw a string of text diagonally across the first page
129146
firstPage.drawText('This text was added with JavaScript!', {
130147
x: 5,
131148
y: height / 2 + 300,
@@ -135,7 +152,14 @@ firstPage.drawText('This text was added with JavaScript!', {
135152
rotate: degrees(-45),
136153
})
137154

155+
156+
// Serialize the PDFDocument to bytes (a Uint8Array)
138157
const pdfBytes = await pdfDoc.save()
158+
159+
// For example, `pdfBytes` can be:
160+
// • Written to a file in Node
161+
// • Downloaded from the browser
162+
// • Rendered in an <iframe>
139163
```
140164

141165
### Copy Pages
@@ -146,30 +170,39 @@ _This example produces [this PDF](assets/pdfs/examples/copying_pages.pdf)_ (when
146170
```js
147171
import { PDFDocument } from 'pdf-lib'
148172

173+
// Create a new PDFDocument
149174
const pdfDoc = await PDFDocument.create();
150175

151-
// These should be a Uint8Arrays or ArrayBuffers.
152-
// This data can be obtained in a number of different ways.
153-
// If your running in a Node environment, you could use fs.readFile().
154-
// In the browser, you could make a fetch() call and use res.arrayBuffer().
176+
// These should be Uint8Arrays or ArrayBuffers
177+
// This data can be obtained in a number of different ways
178+
// If your running in a Node environment, you could use fs.readFile()
179+
// In the browser, you could make a fetch() call and use res.arrayBuffer()
155180
const firstDonorPdfBytes = ...
156181
const secondDonorPdfBytes = ...
157182

183+
// Load a PDFDocument from each of the existing PDFs
158184
const firstDonorPdfDoc = await PDFDocument.load(firstDonorPdfBytes)
159185
const secondDonorPdfDoc = await PDFDocument.load(secondDonorPdfBytes)
160186

161-
// We'll copy the 1st page from the first donor document, and the
162-
// 743rd page from the second donor document.
187+
// Copy the 1st page from the first donor document, and
188+
// the 743rd page from the second donor document
163189
const [firstDonorPage] = await pdfDoc.copyPages(firstDonorPdfDoc, [0])
164190
const [secondDonorPage] = await pdfDoc.copyPages(secondDonorPdfDoc, [742])
165191

166192
// Add the first copied page
167193
pdfDoc.addPage(firstDonorPage)
168194

169-
// Insert the second copied page to index 0, so it will be the first page
195+
// Insert the second copied page to index 0, so it will be the
196+
// first page in `pdfDoc`
170197
pdfDoc.insertPage(0, secondDonorPage)
171198

199+
// Serialize the PDFDocument to bytes (a Uint8Array)
172200
const pdfBytes = await pdfDoc.save()
201+
202+
// For example, `pdfBytes` can be:
203+
// • Written to a file in Node
204+
// • Downloaded from the browser
205+
// • Rendered in an <iframe>
173206
```
174207

175208
### Embed PNG and JPEG Images
@@ -180,38 +213,52 @@ _This example produces [this PDF](assets/pdfs/examples/embed_png_and_jpeg_images
180213
```js
181214
import { PDFDocument } from 'pdf-lib'
182215

183-
// These should be a Uint8Arrays or ArrayBuffers.
184-
// This data can be obtained in a number of different ways.
185-
// If your running in a Node environment, you could use fs.readFile().
186-
// In the browser, you could make a fetch() call and use res.arrayBuffer().
216+
// These should be Uint8Arrays or ArrayBuffers
217+
// This data can be obtained in a number of different ways
218+
// If your running in a Node environment, you could use fs.readFile()
219+
// In the browser, you could make a fetch() call and use res.arrayBuffer()
187220
const jpgImageBytes = ...
188221
const pngImageBytes = ...
189222

223+
// Create a new PDFDocument
190224
const pdfDoc = await PDFDocument.create()
191225

226+
// Embed the JPG image bytes and PNG image bytes
192227
const jpgImage = await pdfDoc.embedJpg(jpgImageBytes)
193228
const pngImage = await pdfDoc.embedPng(pngImageBytes)
194229

230+
// Get the width/height of the JPG image scaled down to 25% of its original size
195231
const jpgDims = jpgImage.scale(0.25)
232+
233+
// Get the width/height of the PNG image scaled down to 50% of its original size
196234
const pngDims = pngImage.scale(0.5)
197235

236+
// Add a blank page to the document
198237
const page = pdfDoc.addPage()
199238

239+
// Draw the JPG image in the center of the page
200240
page.drawImage(jpgImage, {
201241
x: page.getWidth() / 2 - jpgDims.width / 2,
202242
y: page.getHeight() / 2 - jpgDims.height / 2,
203243
width: jpgDims.width,
204244
height: jpgDims.height,
205245
})
206246

247+
// Draw the PNG image near the lower right corner of the JPG image
207248
page.drawImage(pngImage, {
208249
x: page.getWidth() / 2 - pngDims.width / 2 + 75,
209250
y: page.getHeight() / 2 - pngDims.height,
210251
width: pngDims.width,
211252
height: pngDims.height,
212253
})
213254

255+
// Serialize the PDFDocument to bytes (a Uint8Array)
214256
const pdfBytes = await pdfDoc.save()
257+
258+
// For example, `pdfBytes` can be:
259+
// • Written to a file in Node
260+
// • Downloaded from the browser
261+
// • Rendered in an <iframe>
215262
```
216263

217264
### Embed Font and Measure Text
@@ -227,26 +274,31 @@ _This example produces [this PDF](assets/pdfs/examples/embed_font_and_measure_te
227274
import { PDFDocument, rgb } from 'pdf-lib'
228275
import fontkit from '@pdf-lib/fontkit'
229276

230-
// This should be a Uint8Array or ArrayBuffer.
231-
// This data can be obtained in a number of different ways.
232-
// If your running in a Node environment, you could use fs.readFile().
233-
// In the browser, you could make a fetch() call and use res.arrayBuffer().
277+
// This should be a Uint8Array or ArrayBuffer
278+
// This data can be obtained in a number of different ways
279+
// If your running in a Node environment, you could use fs.readFile()
280+
// In the browser, you could make a fetch() call and use res.arrayBuffer()
234281
const fontBytes = ...
235282

283+
// Create a new PDFDocument
236284
const pdfDoc = await PDFDocument.create()
237285

286+
// Register the `fontkit` instance
238287
pdfDoc.registerFontkit(fontkit)
239288

289+
// Embed our custom font in the document
240290
const customFont = await pdfDoc.embedFont(fontBytes)
241291

292+
// Add a blank page to the document
242293
const page = pdfDoc.addPage()
243294

295+
// Create a string of text and measure its width and height in our custom font
244296
const text = 'This is text in an embedded font!'
245297
const textSize = 35
246298
const textWidth = customFont.widthOfTextAtSize(text, textSize)
247299
const textHeight = customFont.heightAtSize(textSize)
248300

249-
// Draw text on page
301+
// Draw the string of text on the page
250302
page.drawText(text, {
251303
x: 40,
252304
y: 450,
@@ -255,7 +307,7 @@ page.drawText(text, {
255307
color: rgb(0, 0.53, 0.71),
256308
})
257309

258-
// Draw a box around the text
310+
// Draw a box around the string of text
259311
page.drawRectangle({
260312
x: 40,
261313
y: 450,
@@ -265,7 +317,13 @@ page.drawRectangle({
265317
borderWidth: 1.5,
266318
})
267319

320+
// Serialize the PDFDocument to bytes (a Uint8Array)
268321
const pdfBytes = await pdfDoc.save()
322+
323+
// For example, `pdfBytes` can be:
324+
// • Written to a file in Node
325+
// • Downloaded from the browser
326+
// • Rendered in an <iframe>
269327
```
270328

271329
## Installation
@@ -346,8 +404,8 @@ pdfDoc.registerFontkit(fontkit);
346404

347405
## Prior Art
348406

349-
- [`pdfkit`](https://github.com/devongovett/pdfkit) is a PDF generation library for Node and the Browser. This library was immensely helpful as a reference and existence proof when creating `pdf-lib`. `pdfkit`'s code for [font embedding](https://github.com/Hopding/pdf-lib/blob/AddDocumentation/src/core/pdf-structures/factories/PDFFontFactory.ts#L64-L68), [PNG embedding](https://github.com/Hopding/pdf-lib/blob/AddDocumentation/src/core/pdf-structures/factories/PNGXObjectFactory.ts#L19-L23), and [JPG embedding](https://github.com/Hopding/pdf-lib/blob/AddDocumentation/src/core/pdf-structures/factories/JPEGXObjectFactory.ts#L32-L36) was especially useful.
350-
- [`pdf.js`](https://github.com/mozilla/pdf.js) is a PDF rendering library for the Browser. This library was helpful as a reference when writing `pdf-lib`'s parser. Some of the code for stream decoding was ported directly to TypeScript for use in `pdf-lib`.
407+
- [`pdfkit`](https://github.com/devongovett/pdfkit) is a PDF generation library for Node and the Browser. This library was immensely helpful as a reference and existence proof when creating `pdf-lib`. `pdfkit`'s code for [font embedding](https://github.com/Hopding/pdf-lib/blob/Rewrite/src/core/embedders/CustomFontEmbedder.ts#L17-L21), [PNG embedding](https://github.com/Hopding/pdf-lib/blob/Rewrite/src/core/embedders/PngEmbedder.ts#L7-L11), and [JPG embedding](https://github.com/Hopding/pdf-lib/blob/Rewrite/src/core/embedders/JpegEmbedder.ts#L25-L29) was especially useful.
408+
- [`pdf.js`](https://github.com/mozilla/pdf.js) is a PDF rendering library for the Browser. This library was helpful as a reference when writing `pdf-lib`'s parser. Some of the code for stream decoding was [ported directly to TypeScript](https://github.com/Hopding/pdf-lib/tree/Rewrite/src/core/streams) for use in `pdf-lib`.
351409
- [`jspdf`](https://github.com/MrRio/jsPDF) is a PDF generation library for the browser.
352410
- [`pdfmake`](https://github.com/bpampuch/pdfmake) is a PDF generation library for the browser.
353411
- [`hummus`](https://github.com/galkahana/HummusJS) is a PDF generation and modification library for Node environments. `hummus` is a Node wrapper around a [C++ library](https://github.com/galkahana/PDF-Writer), so it doesn't work in many JavaScript environments - like the Browser or React Native.

0 commit comments

Comments
 (0)