(
data: Uint8Array,
filename: string,
options: AddAttachmentOptions = {},
)
| 243 | * @returns A PdfStream for the embedded file |
| 244 | */ |
| 245 | export 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. |
no test coverage detected