chromiumly
Version:
A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.
171 lines • 10.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PDFEngines = void 0;
const fs_1 = require("fs");
const path_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 async merge({ files, pdfa, pdfUA, metadata, downloadFrom, flatten }) {
const data = new FormData();
await pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
await 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 async convert({ files, pdfa, pdfUA, downloadFrom }) {
const data = new FormData();
await pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
await 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 async split({ files, options }) {
const data = new FormData();
await 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 async flatten(files) {
const data = new FormData();
await 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 async readMetadata(files) {
const data = new FormData();
await 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 async writeMetadata({ files, metadata }) {
const data = new FormData();
data.append('metadata', JSON.stringify(metadata));
await 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());
}
/**
* Encrypts a PDF file.
*
* @param {Object} options - Options for the encrypt operation.
* @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the PDF files to be encrypted.
* @param {EncryptOptions} options.options - Encryption configuration specifying userPassword (required) and ownerPassword (optional).
* @returns {Promise<Buffer>} A Promise resolving to the encrypted PDF content as a buffer
*/
static async encrypt({ files, options }) {
const data = new FormData();
await pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
data.append('userPassword', options.userPassword);
if (options.ownerPassword) {
data.append('ownerPassword', options.ownerPassword);
}
const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.encrypt}`;
return common_1.GotenbergUtils.fetch(endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders());
}
/**
* Embeds files into PDF files.
*
* @param {Object} options - Options for the embed operation.
* @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the PDF files to embed files into.
* @param {PathLikeOrReadStream[]} options.embeds - An array of PathLikes or ReadStreams to the files to embed in the PDF.
* @returns {Promise<Buffer>} A Promise resolving to the PDF content with embedded files as a buffer
*/
static async embed({ files, embeds }) {
const data = new FormData();
await pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
await pdf_engines_utils_1.PDFEnginesUtils.addFilesWithFieldName(embeds, data, 'embeds');
const endpoint = `${main_config_1.Chromiumly.getGotenbergEndpoint()}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.embed}`;
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 async generate(filename, buffer) {
const __generated__ = path_1.default.resolve(process.cwd(), '__generated__');
await fs_1.promises.mkdir(path_1.default.resolve(__generated__), { recursive: true });
await fs_1.promises.writeFile(path_1.default.resolve(__generated__, filename), buffer);
}
}
exports.PDFEngines = PDFEngines;
//# sourceMappingURL=pdf-engines.js.map