MCPcopy Index your code
hub / github.com/foliojs/pdfkit / convert

Method convert

lib/object.js:78–162  ·  view source on GitHub ↗
(object, encryptFn = null)

Source from the content-addressed store, hash-verified

76
77class PDFObject {
78 static convert(object, encryptFn = null) {
79 // String literals are converted to the PDF name type
80 if (typeof object === 'string') {
81 return `/${object}`;
82
83 // String objects are converted to PDF strings (UTF-16)
84 } else if (object instanceof String) {
85 let string = object;
86 // Detect if this is a unicode string
87 let isUnicode = false;
88 for (let i = 0, end = string.length; i < end; i++) {
89 if (string.charCodeAt(i) > 0x7f) {
90 isUnicode = true;
91 break;
92 }
93 }
94
95 // If so, encode it as big endian UTF-16
96 let stringBuffer;
97 if (isUnicode) {
98 stringBuffer = swapBytes(Buffer.from(`\ufeff${string}`, 'utf16le'));
99 } else {
100 stringBuffer = Buffer.from(string.valueOf(), 'ascii');
101 }
102
103 // Encrypt the string when necessary
104 if (encryptFn) {
105 string = encryptFn(stringBuffer).toString('binary');
106 } else {
107 string = stringBuffer.toString('binary');
108 }
109
110 // Escape characters as required by the spec
111 string = string.replace(escapableRe, (c) => escapable[c]);
112
113 return `(${string})`;
114
115 // Buffers are converted to PDF hex strings
116 } else if (Buffer.isBuffer(object)) {
117 return `<${object.toString('hex')}>`;
118 } else if (
119 object instanceof PDFAbstractReference ||
120 object instanceof PDFTree ||
121 object instanceof SpotColor
122 ) {
123 return object.toString();
124 } else if (object instanceof Date) {
125 let string =
126 `D:${pad(object.getUTCFullYear(), 4)}` +
127 pad(object.getUTCMonth() + 1, 2) +
128 pad(object.getUTCDate(), 2) +
129 pad(object.getUTCHours(), 2) +
130 pad(object.getUTCMinutes(), 2) +
131 pad(object.getUTCSeconds(), 2) +
132 'Z';
133
134 // Encrypt the string when necessary
135 if (encryptFn) {

Callers 5

toStringMethod · 0.80
_finalizeMethod · 0.80
finalizeMethod · 0.80
markContentFunction · 0.80
object.spec.jsFile · 0.80

Calls 5

swapBytesFunction · 0.85
padFunction · 0.85
pushMethod · 0.80
numberMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected