MCPcopy Create free account
hub / github.com/LibPDF-js/core / createEmbeddedFileStream

Function createEmbeddedFileStream

src/attachments/file-spec.ts:245–279  ·  view source on GitHub ↗
(
  data: Uint8Array,
  filename: string,
  options: AddAttachmentOptions = {},
)

Source from the content-addressed store, hash-verified

243 * @returns A PdfStream for the embedded file
244 */
245export function createEmbeddedFileStream(
246 data: Uint8Array,
247 filename: string,
248 options: AddAttachmentOptions = {},
249): PdfStream {
250 const now = new Date();
251 const mimeType = options.mimeType ?? getMimeType(filename);
252
253 // Build params dictionary
254 const paramsEntries: Array<[string, PdfObject]> = [["Size", PdfNumber.of(data.length)]];
255
256 const createdAt = options.createdAt ?? now;
257 paramsEntries.push(["CreationDate", PdfString.fromString(formatPdfDate(createdAt))]);
258
259 const modifiedAt = options.modifiedAt ?? now;
260 paramsEntries.push(["ModDate", PdfString.fromString(formatPdfDate(modifiedAt))]);
261
262 const params = new PdfDict(paramsEntries);
263
264 // Build stream dictionary entries
265 const streamEntries: Array<[string, PdfObject]> = [
266 ["Type", PdfName.of("EmbeddedFile")],
267 ["Params", params],
268 ];
269
270 // Add MIME type if known
271
272 if (mimeType) {
273 // Encode MIME type as PDF name (replace / with #2F)
274 const encodedMime = mimeType.replaceAll("/", "#2F").replaceAll(" ", "#20");
275 streamEntries.push(["Subtype", PdfName.of(encodedMime)]);
276 }
277
278 return new PdfStream(streamEntries, data);
279}
280
281/**
282 * Create a FileSpec dictionary for an embedded file.

Callers 2

file-spec.test.tsFile · 0.90
addMethod · 0.90

Calls 5

formatPdfDateFunction · 0.90
getMimeTypeFunction · 0.85
pushMethod · 0.80
ofMethod · 0.45
fromStringMethod · 0.45

Tested by

no test coverage detected