|
51 | 51 | - [Copy Pages](#copy-pages)
|
52 | 52 | - [Embed PNG and JPEG Images](#embed-png-and-jpeg-images)
|
53 | 53 | - [Embed PDF Pages](#embed-pdf-pages)
|
54 |
| - - [Add Attachments](#add-attachments) |
55 | 54 | - [Embed Font and Measure Text](#embed-font-and-measure-text)
|
| 55 | + - [Add Attachments](#add-attachments) |
56 | 56 | - [Set Document Metadata](#set-document-metadata)
|
57 | 57 | - [Read Document Metadata](#read-document-metadata)
|
58 | 58 | - [Draw SVG Paths](#draw-svg-paths)
|
@@ -360,41 +360,6 @@ const pdfBytes = await pdfDoc.save()
|
360 | 360 | // • Rendered in an <iframe>
|
361 | 361 | ```
|
362 | 362 |
|
363 |
| -### Add Attachments |
364 |
| - |
365 |
| -_This example produces [this PDF](assets/pdfs/examples/file_with_attachment.pdf)_ (when [this PDF](assets/pdfs/normal.pdf) is used for the `fileAttachment` variable). |
366 |
| - |
367 |
| -<!-- prettier-ignore --> |
368 |
| -```js |
369 |
| -import { PDFDocument, rgb } from 'pdf-lib' |
370 |
| - |
371 |
| -// This should be a Uint8Array or ArrayBuffer |
372 |
| -// This data can be obtained in a number of different ways |
373 |
| -// If your running in a Node environment, you could use fs.readFile() |
374 |
| -// In the browser, you could make a fetch() call and use res.arrayBuffer() |
375 |
| -const attachmentBytes = ... |
376 |
| - |
377 |
| -// Create a new PDFDocument |
378 |
| -const pdfDoc = await PDFDocument.create() |
379 |
| - |
380 |
| -// Add the attachment |
381 |
| -await pdfDoc.attach(attachmentBytes, "cool_stuff.pdf", { |
382 |
| - mimeType: "application/pdf", |
383 |
| - description: "Full of cool stuff", |
384 |
| - creationDate: new Date("2019/12/24"), |
385 |
| - modificationDate: new Date("2020/01/01"), |
386 |
| - checkSum: "D7C816C0E7D35860A9505b2d64C9E3E3" |
387 |
| -}) |
388 |
| - |
389 |
| -// Serialize the PDFDocument to bytes (a Uint8Array) |
390 |
| -const pdfBytes = await pdfDoc.save() |
391 |
| - |
392 |
| -// For example, `pdfBytes` can be: |
393 |
| -// • Written to a file in Node |
394 |
| -// • Downloaded from the browser |
395 |
| -// • Rendered in an <iframe> |
396 |
| -``` |
397 |
| - |
398 | 363 | ### Embed Font and Measure Text
|
399 | 364 |
|
400 | 365 | `pdf-lib` relies on a sister module to support embedding custom fonts: [`@pdf-lib/fontkit`](https://www.npmjs.com/package/@pdf-lib/fontkit). You must add the `@pdf-lib/fontkit` module to your project and register it using `pdfDoc.registerFontkit(...)` before embedding custom fonts.
|
@@ -462,6 +427,55 @@ const pdfBytes = await pdfDoc.save()
|
462 | 427 | // • Rendered in an <iframe>
|
463 | 428 | ```
|
464 | 429 |
|
| 430 | +### Add Attachments |
| 431 | + |
| 432 | +_This example produces [this PDF](assets/pdfs/examples/add_attachments.pdf)_ (when [this image](assets/images/cat_riding_unicorn.jpg) is used for the `jpgAttachmentBytes` variable and [this PDF](assets/pdfs/us_constitution.pdf) is used for the `pdfAttachmentBytes` variable). |
| 433 | + |
| 434 | +<!-- [Try the JSFiddle demo](https://jsfiddle.net/Hopding/Lyb16ocj/13/) --> |
| 435 | + |
| 436 | +<!-- prettier-ignore --> |
| 437 | +```js |
| 438 | +import { PDFDocument } from 'pdf-lib' |
| 439 | + |
| 440 | +// These should be Uint8Arrays or ArrayBuffers |
| 441 | +// This data can be obtained in a number of different ways |
| 442 | +// If your running in a Node environment, you could use fs.readFile() |
| 443 | +// In the browser, you could make a fetch() call and use res.arrayBuffer() |
| 444 | +const jpgAttachmentBytes = ... |
| 445 | +const pdfAttachmentBytes = ... |
| 446 | + |
| 447 | +// Create a new PDFDocument |
| 448 | +const pdfDoc = await PDFDocument.create() |
| 449 | + |
| 450 | +// Add the JPG attachment |
| 451 | +await pdfDoc.attach(jpgAttachmentBytes, 'cat_riding_unicorn.jpg', { |
| 452 | + mimeType: 'image/jpeg', |
| 453 | + description: 'Cool cat riding a unicorn! 🦄🐈🕶️', |
| 454 | + creationDate: new Date('2019/12/01'), |
| 455 | + modificationDate: new Date('2020/04/19'), |
| 456 | +}) |
| 457 | + |
| 458 | +// Add the PDF attachment |
| 459 | +await pdfDoc.attach(pdfAttachmentBytes, 'us_constitution.pdf', { |
| 460 | + mimeType: 'application/pdf', |
| 461 | + description: 'Constitution of the United States 🇺🇸🦅', |
| 462 | + creationDate: new Date('1787/09/17'), |
| 463 | + modificationDate: new Date('1992/05/07'), |
| 464 | +}) |
| 465 | + |
| 466 | +// Add a page with some text |
| 467 | +const page = pdfDoc.addPage(); |
| 468 | +page.drawText('This PDF has two attachments', { x: 135, y: 415 }) |
| 469 | + |
| 470 | +// Serialize the PDFDocument to bytes (a Uint8Array) |
| 471 | +const pdfBytes = await pdfDoc.save() |
| 472 | + |
| 473 | +// For example, `pdfBytes` can be: |
| 474 | +// • Written to a file in Node |
| 475 | +// • Downloaded from the browser |
| 476 | +// • Rendered in an <iframe> |
| 477 | +``` |
| 478 | + |
465 | 479 | ### Set Document Metadata
|
466 | 480 |
|
467 | 481 | _This example produces [this PDF](assets/pdfs/examples/set_document_metadata.pdf)_.
|
|
0 commit comments