UNPKG

chromiumly

Version:

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

150 lines 9.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PDFEngines = void 0; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const path_1 = tslib_1.__importDefault(require("path")); const main_config_1 = require("../main.config"); const common_1 = require("../common"); const pdf_engines_utils_1 = require("./utils/pdf-engines.utils"); /** * Class uses PDF engines for various operations such as merging and conversion. */ class PDFEngines { /** * Merges multiple PDF files into a single PDF document. * * @param {Object} options - Options for the merge operation. * @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the PDF files to be merged. * @param {PdfFormat} [options.pdfa] - PDF format options. * @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output. * @param {Metadata} [options.metadata] - Metadata to be written. * @param {DownloadFrom} [options.downloadFrom] - Download a file from a URL. It must return a Content-Disposition header with a filename parameter. * @param {boolean} [options.flatten] - Flatten the PDF document. * * @returns {Promise<Buffer>} A Promise resolving to the merged PDF content as a buffer */ static merge(_a) { return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, pdfa, pdfUA, metadata, downloadFrom, flatten }) { const data = new FormData(); yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data); yield pdf_engines_utils_1.PDFEnginesUtils.customize(data, { pdfa, pdfUA, metadata, downloadFrom, flatten }); const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.merge}`; return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders()); }); } /** * Converts various document formats to PDF. * * @param {Object} options - Options for the conversion operation. * @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the files to be converted to PDF. * @param {PdfFormat} [options.pdfa] - PDF format options. * @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output. * @param {DownloadFrom} [options.downloadFrom] - Download a file from a URL. It must return a Content-Disposition header with a filename parameter. * @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a buffer */ static convert(_a) { return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, pdfa, pdfUA, downloadFrom }) { const data = new FormData(); yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data); yield pdf_engines_utils_1.PDFEnginesUtils.customize(data, { pdfa, pdfUA, downloadFrom }); const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.convert}`; return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders()); }); } /** * Splits a PDF file into multiple PDF files. * * @param {Object} options - Options for the split operation. * @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the PDF files to be split. * @param {Split} options.options - Split configuration specifying mode ('pages' or 'intervals'), span, and unify options. * @returns {Promise<Buffer>} A Promise resolving to the split PDF content as a buffer */ static split(_a) { return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, options }) { const data = new FormData(); yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data); data.append('splitMode', options.mode); data.append('splitSpan', options.span); if (options.flatten) { data.append('flatten', String(options.flatten)); } if (options.unify) { common_1.GotenbergUtils.assert(options.mode === 'pages', 'split unify is only supported for pages mode'); data.append('splitUnify', String(options.unify)); } const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.split}`; return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders()); }); } /** * Flattens a PDF file. * * @param {PathLikeOrReadStream[]} files - An array of PathLikes or ReadStreams to the PDF files to be flattened. * @returns {Promise<Buffer>} A Promise resolving to the flattened PDF content as a buffer */ static flatten(files) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const data = new FormData(); yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data); const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.flatten}`; return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders()); }); } /** * Reads metadata from the provided files. * * @param {PathLikeOrReadStream[]} files An array of PathLikes or ReadStreams to the PDF files. * @returns {Promise<Buffer>} A Promise resolving to the metadata buffer. */ static readMetadata(files) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const data = new FormData(); yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data); const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.readMetadata}`; return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders()); }); } /** * Writes metadata to the provided PDF files. * * @param {PathLikeOrReadStream[]} files - An array of PathLikes or ReadStreams to the PDF files. * @param {Metadata} metadata - Metadata to be written. * @returns {Promise<Buffer>} A Promise that resolves to the PDF file containing metadata as a buffer. */ static writeMetadata(_a) { return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, metadata }) { const data = new FormData(); data.append('metadata', JSON.stringify(metadata)); yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data); const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.writeMetadata}`; return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders()); }); } /** * Generates a PDF file from a buffer and saves it to the "__generated__" directory. * * @param {string} filename - The filename for the generated PDF. * @param {Buffer} buffer - The PDF content as a buffer * @returns {Promise<void>} A Promise that resolves once the file is generated and saved. */ static generate(filename, buffer) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const __generated__ = path_1.default.resolve(process.cwd(), '__generated__'); yield fs_1.promises.mkdir(path_1.default.resolve(__generated__), { recursive: true }); yield fs_1.promises.writeFile(path_1.default.resolve(__generated__, filename), buffer); }); } } exports.PDFEngines = PDFEngines; //# sourceMappingURL=pdf-engines.js.map