UNPKG

chromiumly

Version:

A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.

99 lines 4.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PDFEnginesUtils = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const consumers_1 = require("node:stream/consumers"); const common_1 = require("../../common"); /** * Utility class for handling common tasks related to PDF engine operations. */ class PDFEnginesUtils { /** * Adds PDF files to the FormData object. * * @param {PathLikeOrReadStream[]} files - An array of PDF files to be added to the FormData. * @param {FormData} data - The FormData object to which PDF files will be added. * @throws {Error} Throws an error if the file extension is not supported. */ static async addFiles(files, data) { const paddingLength = String(files.length).length + 1; await Promise.all(files.map(async (file, index) => { const filename = `file${String(index + 1).padStart(paddingLength, '0')}.pdf`; if (Buffer.isBuffer(file)) { data.append('files', new Blob([file]), filename); } else if (file instanceof fs_1.ReadStream) { const content = await (0, consumers_1.blob)(file); data.append('files', content, filename); } else { await fs_1.promises.access(file, fs_1.constants.R_OK); const _filename = path_1.default.basename(file.toString()); const extension = path_1.default.extname(_filename); if (extension === '.pdf') { const content = await (0, fs_1.openAsBlob)(file); data.append('files', content, _filename); } else { throw new Error(`${extension} is not supported`); } } })); } /** * Adds files to the FormData object with a custom field name. * * @param {PathLikeOrReadStream[]} files - An array of files to be added to the FormData. * @param {FormData} data - The FormData object to which files will be added. * @param {string} fieldName - The field name to use when appending files (e.g., 'files', 'embeds'). * @returns {Promise<void>} A Promise that resolves once the files have been added. */ static async addFilesWithFieldName(files, data, fieldName) { await Promise.all(files.map(async (file, index) => { const filename = path_1.default.basename(typeof file === 'string' ? file : `file${index + 1}`); if (Buffer.isBuffer(file)) { data.append(fieldName, new Blob([file]), filename); } else if (file instanceof fs_1.ReadStream) { const content = await (0, consumers_1.blob)(file); data.append(fieldName, content, filename); } else { await fs_1.promises.access(file, fs_1.constants.R_OK); const _filename = path_1.default.basename(file.toString()); const content = await (0, fs_1.openAsBlob)(file); data.append(fieldName, content, _filename); } })); } /** * Customizes the FormData object based on the provided conversion options. * * @param {FormData} data - The FormData object to be customized. * @param {ConversionOptions | MergeOptions} options - The options to apply to the FormData. * @returns {Promise<void>} A Promise that resolves once the customization is complete. */ static async customize(data, options) { if (options.pdfa) { data.append('pdfa', options.pdfa); } if (options.pdfUA) { data.append('pdfua', String(options.pdfUA)); } if ('metadata' in options && options.metadata) { data.append('metadata', JSON.stringify(options.metadata)); } if (options.downloadFrom) { data.append('downloadFrom', JSON.stringify(common_1.GotenbergUtils.normalizeDownloadFrom(options.downloadFrom))); } if ('flatten' in options && options.flatten) { data.append('flatten', String(options.flatten)); } } } exports.PDFEnginesUtils = PDFEnginesUtils; //# sourceMappingURL=pdf-engines.utils.js.map