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

adnsistemas / pdf-lib / #18

24 Mar 2026 08:15PM UTC coverage: 74.286% (+0.3%) from 74.001%
#18

push

David N. Abdala
Documentation change

2569 of 3981 branches covered (64.53%)

Branch coverage included in aggregate %.

7372 of 9401 relevant lines covered (78.42%)

297170.51 hits per line

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

95.92
/src/core/objects/PDFHexString.ts
1
import PDFObject from './PDFObject';
54✔
2
import CharCodes from '../syntax/CharCodes';
54✔
3
import {
54✔
4
  copyStringIntoBuffer,
5
  toHexStringOfMinLength,
6
  utf16Decode,
7
  utf16Encode,
8
  pdfDocEncodingDecode,
9
  parseDate,
10
  hasUtf16BOM,
11
  byteArrayToHexString,
12
} from '../../utils';
13
import { InvalidPDFDateStringError } from '../errors';
54✔
14
import { PDFClasses } from '../../api/objects';
54✔
15

16
class PDFHexString extends PDFObject {
17
  static className = () => PDFClasses.PDFHexString;
109,128✔
18
  myClass(): PDFClasses {
19
    return PDFClasses.PDFHexString;
109,202✔
20
  }
21
  static of = (value: string) => new PDFHexString(value);
11,142✔
22

23
  static fromText = (value: string) => {
54✔
24
    const encoded = utf16Encode(value);
436✔
25

26
    let hex = '';
436✔
27
    for (let idx = 0, len = encoded.length; idx < len; idx++) {
436✔
28
      hex += toHexStringOfMinLength(encoded[idx], 4);
14,063✔
29
    }
30

31
    return new PDFHexString(hex);
436✔
32
  };
33

34
  static fromBytes = (bytes: Uint8Array) =>
54✔
35
    PDFHexString.of(byteArrayToHexString(bytes));
×
36

37
  private readonly value: string;
38

39
  constructor(value: string) {
40
    super();
11,578✔
41
    this.value = value;
11,578✔
42
  }
43

44
  asBytes(): Uint8Array {
45
    // Append a zero if the number of digits is odd. See PDF spec 7.3.4.3
46
    const hex = this.value + (this.value.length % 2 === 1 ? '0' : '');
215✔
47
    const hexLength = hex.length;
215✔
48

49
    const bytes = new Uint8Array(hex.length / 2);
215✔
50

51
    let hexOffset = 0;
215✔
52
    let bytesOffset = 0;
215✔
53

54
    // Interpret each pair of hex digits as a single byte
55
    while (hexOffset < hexLength) {
215✔
56
      const byte = parseInt(hex.substring(hexOffset, hexOffset + 2), 16);
6,634✔
57
      bytes[bytesOffset] = byte;
6,634✔
58

59
      hexOffset += 2;
6,634✔
60
      bytesOffset += 1;
6,634✔
61
    }
62

63
    return bytes;
215✔
64
  }
65

66
  decodeText(): string {
67
    const bytes = this.asBytes();
208✔
68
    if (hasUtf16BOM(bytes)) return utf16Decode(bytes);
208✔
69
    return pdfDocEncodingDecode(bytes);
2✔
70
  }
71

72
  decodeDate(): Date {
73
    const text = this.decodeText();
13✔
74
    const date = parseDate(text);
13✔
75
    if (!date) throw new InvalidPDFDateStringError(text);
13!
76
    return date;
13✔
77
  }
78

79
  asString(): string {
80
    return this.value;
3✔
81
  }
82

83
  clone(): PDFHexString {
84
    return PDFHexString.of(this.value);
2✔
85
  }
86

87
  toString(): string {
88
    return `<${this.value}>`;
37✔
89
  }
90

91
  sizeInBytes(): number {
92
    return this.value.length + 2;
300✔
93
  }
94

95
  copyBytesInto(buffer: Uint8Array, offset: number): number {
96
    buffer[offset++] = CharCodes.LessThan;
265✔
97
    offset += copyStringIntoBuffer(this.value, buffer, offset);
265✔
98
    buffer[offset++] = CharCodes.GreaterThan;
265✔
99
    return this.value.length + 2;
265✔
100
  }
101
}
102

103
export default PDFHexString;
54✔
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