UNPKG

pdf-lib

Version:

Create and modify PDF files with JavaScript

49 lines (39 loc) 1.57 kB
import PDFObject from 'src/core/objects/PDFObject'; import CharCodes from 'src/core/syntax/CharCodes'; import { copyStringIntoBuffer, padStart } from 'src/utils'; class PDFString extends PDFObject { // The PDF spec allows newlines and parens to appear directly within a literal // string. These character _may_ be escaped. But they do not _have_ to be. So // for simplicity, we will not bother escaping them. static of = (value: string) => new PDFString(value); static fromDate = (date: Date) => { const year = padStart(String(date.getUTCFullYear()), 4, '0'); const month = padStart(String(date.getUTCMonth() + 1), 2, '0'); const day = padStart(String(date.getUTCDate()), 2, '0'); const hours = padStart(String(date.getUTCHours()), 2, '0'); const mins = padStart(String(date.getUTCMinutes()), 2, '0'); const secs = padStart(String(date.getUTCSeconds()), 2, '0'); return new PDFString(`D:${year}${month}${day}${hours}${mins}${secs}Z`); }; private readonly value: string; private constructor(value: string) { super(); this.value = value; } clone(): PDFString { return PDFString.of(this.value); } toString(): string { return `(${this.value})`; } sizeInBytes(): number { return this.value.length + 2; } copyBytesInto(buffer: Uint8Array, offset: number): number { buffer[offset++] = CharCodes.LeftParen; offset += copyStringIntoBuffer(this.value, buffer, offset); buffer[offset++] = CharCodes.RightParen; return this.value.length + 2; } } export default PDFString;