From 887de03cae4d6034bda7e444921841796033ec61 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Fri, 27 May 2022 18:04:16 -0400 Subject: [PATCH] Add getAttachments to PDFDocument This adds the getAttachments method to get all the attachments from a pdf document. This is basically implementing the suggestion in https://github.com/Hopding/pdf-lib/issues/534#issuecomment-662758789 There's some interesting behavior here, as the attachments don't show up until after save is called. That's tested in the test and described in the README, just so it's clear that's the behavior. I don't know how to do all that great jsfiddle setup, so for the README Ijust copied the basic example from that issue for now. --- README.md | 19 +++++ src/api/PDFDocument.ts | 45 +++++++++++ tests/api/PDFDocument.spec.ts | 142 ++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) diff --git a/README.md b/README.md index d7f9eb8ac..924347fea 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Install with: `npm install @cantoo/pdf-lib` - [Embed PDF Pages](#embed-pdf-pages) - [Embed Font and Measure Text](#embed-font-and-measure-text) - [Add Attachments](#add-attachments) + - [Extract Attachments](#extract-attachments) - [Set Document Metadata](#set-document-metadata) - [Read Document Metadata](#read-document-metadata) - [Set Viewer Preferences](#set-viewer-preferences) @@ -117,6 +118,7 @@ Install with: `npm install @cantoo/pdf-lib` - Set viewer preferences - Read viewer preferences - Add attachments +- Extract attachments ## Motivation @@ -765,6 +767,23 @@ const pdfBytes = await pdfDoc.save() // β€’ Rendered in an