• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adnsistemas / pdf-lib / #15

20 Feb 2026 04:09PM UTC coverage: 73.994% (+0.2%) from 73.811%
#15

push

David N. Abdala
Added delete attachmets features to Readme

2559 of 3972 branches covered (64.43%)

Branch coverage included in aggregate %.

7183 of 9194 relevant lines covered (78.13%)

156851.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.06
/src/api/PDFEmbeddedFile.ts
1
import Embeddable from './Embeddable';
2
import PDFDocument from './PDFDocument';
3
import FileEmbedder from '../core/embedders/FileEmbedder';
4
import { PDFName, PDFArray, PDFDict, PDFHexString, PDFRef } from '../core';
32✔
5

6
/**
7
 * Represents a file that has been embedded in a [[PDFDocument]].
8
 */
9
export default class PDFEmbeddedFile implements Embeddable {
32✔
10
  /**
11
   * > **NOTE:** You probably don't want to call this method directly. Instead,
12
   * > consider using the [[PDFDocument.attach]] method, which will create
13
   * instances of [[PDFEmbeddedFile]] for you.
14
   *
15
   * Create an instance of [[PDFEmbeddedFile]] from an existing ref and embedder
16
   *
17
   * @param ref The unique reference for this file.
18
   * @param doc The document to which the file will belong.
19
   * @param embedder The embedder that will be used to embed the file.
20
   */
21
  static of = (ref: PDFRef, doc: PDFDocument, embedder: FileEmbedder) =>
32✔
22
    new PDFEmbeddedFile(ref, doc, embedder);
10✔
23

24
  /** The unique reference assigned to this embedded file within the document. */
25
  readonly ref: PDFRef;
26

27
  /** The document to which this embedded file belongs. */
28
  readonly doc: PDFDocument;
29

30
  private alreadyEmbedded = false;
10✔
31
  private readonly embedder: FileEmbedder;
32

33
  private constructor(ref: PDFRef, doc: PDFDocument, embedder: FileEmbedder) {
34
    this.ref = ref;
10✔
35
    this.doc = doc;
10✔
36
    this.embedder = embedder;
10✔
37
  }
38

39
  /**
40
   * > **NOTE:** You probably don't need to call this method directly. The
41
   * > [[PDFDocument.save]] and [[PDFDocument.saveAsBase64]] methods will
42
   * > automatically ensure all embeddable files get embedded.
43
   *
44
   * Embed this embeddable file in its document.
45
   *
46
   * @returns Resolves when the embedding is complete.
47
   */
48
  async embed(): Promise<void> {
49
    if (!this.alreadyEmbedded) {
8✔
50
      const ref = await this.embedder.embedIntoContext(
8✔
51
        this.doc.context,
52
        this.ref,
53
      );
54

55
      if (!this.doc.catalog.has(PDFName.of('Names'))) {
8✔
56
        this.doc.catalog.set(PDFName.of('Names'), this.doc.context.obj({}));
3✔
57
      }
58
      const Names = this.doc.catalog.lookup(PDFName.of('Names'), PDFDict);
8✔
59

60
      if (!Names.has(PDFName.of('EmbeddedFiles'))) {
8✔
61
        Names.set(PDFName.of('EmbeddedFiles'), this.doc.context.obj({}));
3✔
62
      }
63
      const EmbeddedFiles = Names.lookup(PDFName.of('EmbeddedFiles'), PDFDict);
8✔
64

65
      if (!EmbeddedFiles.has(PDFName.of('Names'))) {
8✔
66
        EmbeddedFiles.set(PDFName.of('Names'), this.doc.context.obj([]));
3✔
67
      }
68
      const EFNames = EmbeddedFiles.lookup(PDFName.of('Names'), PDFArray);
8✔
69

70
      EFNames.push(PDFHexString.fromText(this.embedder.fileName));
8✔
71
      EFNames.push(ref);
8✔
72

73
      /**
74
       * The AF-Tag is needed to achieve PDF-A3 compliance for embedded files
75
       *
76
       * The following document outlines the uses cases of the associated files (AF) tag.
77
       * See:
78
       * https://www.pdfa.org/wp-content/uploads/2018/10/PDF20_AN002-AF.pdf
79
       */
80

81
      if (!this.doc.catalog.has(PDFName.of('AF'))) {
8✔
82
        this.doc.catalog.set(PDFName.of('AF'), this.doc.context.obj([]));
4✔
83
      }
84
      const AF = this.doc.catalog.lookup(PDFName.of('AF'), PDFArray);
8✔
85
      AF.push(ref);
8✔
86

87
      this.alreadyEmbedded = true;
8✔
88
    }
89
  }
90

91
  /**
92
   * Get the embedder used to embed the file.
93
   * @returns the embedder.
94
   */
95
  getEmbedder() {
96
    return this.embedder;
2✔
97
  }
98

99
  /**
100
   * Returns whether or not this file has already been embedded.
101
   * @returns true if the file has already been embedded, false otherwise.
102
   */
103
  getAlreadyEmbedded() {
104
    return this.alreadyEmbedded;
5✔
105
  }
106

107
  getRef() {
108
    return this.ref;
×
109
  }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc