UNPKG

chromiumly

Version:

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

178 lines 8.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConverterUtils = 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 conversion. */ class ConverterUtils { /** * Adds page properties to the FormData object based on the provided PageProperties. * * @param {FormData} data - The FormData object to which page properties will be added. * @param {PageProperties} pageProperties - The page properties to be added to the FormData. */ static addPageProperties(data, pageProperties) { if (pageProperties.singlePage) { data.append('singlePage', String(pageProperties.singlePage)); } if (pageProperties.size) { common_1.GotenbergUtils.assert(pageProperties.size.width >= 1.0 && pageProperties.size.height >= 1.5, 'size is smaller than the minimum printing requirements (i.e. 1.0 x 1.5 in)'); data.append('paperWidth', pageProperties.size.width); data.append('paperHeight', pageProperties.size.height); } if (pageProperties.margins) { common_1.GotenbergUtils.assert(pageProperties.margins.top >= 0 && pageProperties.margins.bottom >= 0 && pageProperties.margins.left >= 0 && pageProperties.margins.left >= 0, 'negative margins are not allowed'); data.append('marginTop', pageProperties.margins.top); data.append('marginBottom', pageProperties.margins.bottom); data.append('marginLeft', pageProperties.margins.left); data.append('marginRight', pageProperties.margins.right); } if (pageProperties.preferCssPageSize) { data.append('preferCssPageSize', String(pageProperties.preferCssPageSize)); } if (pageProperties.printBackground) { data.append('printBackground', String(pageProperties.printBackground)); } if (pageProperties.omitBackground) { data.append('omitBackground', String(pageProperties.omitBackground)); } if (pageProperties.landscape) { data.append('landscape', String(pageProperties.landscape)); } if (pageProperties.scale) { common_1.GotenbergUtils.assert(pageProperties.scale >= 0.1 && pageProperties.scale <= 2.0, 'scale is outside of [0.1 - 2] range'); data.append('scale', pageProperties.scale); } if (pageProperties.nativePageRanges) { common_1.GotenbergUtils.assert(pageProperties.nativePageRanges.from > 0 && pageProperties.nativePageRanges.to > 0 && pageProperties.nativePageRanges.to >= pageProperties.nativePageRanges.from, 'page ranges syntax error'); data.append('nativePageRanges', `${pageProperties.nativePageRanges.from}-${pageProperties.nativePageRanges.to}`); } } /** * 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} options - The conversion 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.pdfFormat) { data.append('pdfa', options.pdfFormat); } if (options.pdfUA) { data.append('pdfua', String(options.pdfUA)); } if (options.header) { const { header } = options; await common_1.GotenbergUtils.addFile(data, header, 'header.html'); } if (options.footer) { const { footer } = options; await common_1.GotenbergUtils.addFile(data, footer, 'footer.html'); } if (options.emulatedMediaType) { data.append('emulatedMediaType', options.emulatedMediaType); } if (options.properties) { ConverterUtils.addPageProperties(data, options.properties); } if (options.waitDelay) { data.append('waitDelay', options.waitDelay); } if (options.waitForExpression) { data.append('waitForExpression', options.waitForExpression); } if (options.userAgent) { data.append('userAgent', options.userAgent); } if (options.extraHttpHeaders) { data.append('extraHttpHeaders', JSON.stringify(options.extraHttpHeaders)); } if (options.failOnHttpStatusCodes) { data.append('failOnHttpStatusCodes', JSON.stringify(options.failOnHttpStatusCodes)); } if (options.failOnResourceHttpStatusCodes) { data.append('failOnResourceHttpStatusCodes', JSON.stringify(options.failOnResourceHttpStatusCodes)); } if (options.failOnResourceLoadingFailed) { data.append('failOnResourceLoadingFailed', String(options.failOnResourceLoadingFailed)); } if (options.failOnConsoleExceptions) { data.append('failOnConsoleExceptions', String(options.failOnConsoleExceptions)); } if (options.skipNetworkIdleEvent === false) { data.append('skipNetworkIdleEvent', String(options.skipNetworkIdleEvent)); } if (options.metadata) { data.append('metadata', JSON.stringify(options.metadata)); } if (options.cookies) { data.append('cookies', JSON.stringify(options.cookies)); } if (options.downloadFrom) { data.append('downloadFrom', JSON.stringify(options.downloadFrom)); } if (options.generateDocumentOutline) { data.append('generateDocumentOutline', String(options.generateDocumentOutline)); } if (options.split) { data.append('splitMode', options.split.mode); data.append('splitSpan', options.split.span); if (options.split.unify) { common_1.GotenbergUtils.assert(options.split.mode === 'pages', 'split unify is only supported for pages mode'); data.append('splitUnify', String(options.split.unify)); } } if (options.userPassword) { data.append('userPassword', options.userPassword); } if (options.ownerPassword) { data.append('ownerPassword', options.ownerPassword); } if (options.embeds && options.embeds.length > 0) { await ConverterUtils.addFilesWithFieldName(options.embeds, data, 'embeds'); } } } exports.ConverterUtils = ConverterUtils; //# sourceMappingURL=converter.utils.js.map