chromiumly
Version:
A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.
84 lines • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HtmlConverter = void 0;
const common_1 = require("../../common");
const converter_utils_1 = require("../utils/converter.utils");
const converter_1 = require("./converter");
const main_config_1 = require("../../main.config");
/**
* Class representing an HTML converter that extends the base Converter class.
* This class is used to convert HTML content to PDF using Gotenberg service.
*
* @extends Converter
*/
class HtmlConverter extends converter_1.Converter {
/**
* Creates an instance of HtmlConverter.
* Initializes the converter with the HTML conversion route.
*/
constructor() {
super(main_config_1.ChromiumRoute.HTML);
}
/**
* Converts HTML content to PDF.
*
* @param {Object} options - Conversion options.
* @param {PathLikeOrReadStream} options.html - PathLike or ReadStream of the HTML content to be converted.
* @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
* @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
* @param {PageProperties} [options.properties] - Page properties for the conversion.
* @param {PdfFormat} [options.pdfFormat] - PDF format options.
* @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output.
* @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the conversion.
* @param {string} [options.waitDelay] - Delay before the conversion process starts.
* @param {string} [options.waitForExpression] - JavaScript expression to wait for before completing the conversion.
* @param {string} [options.userAgent] - User agent string to use during the conversion.
* @param {Record<string, string>} [options.extraHttpHeaders] - Additional HTTP headers for the conversion.
* @param {number[]} [options.failOnHttpStatusCodes] - Whether to fail on HTTP status code.
* @param {boolean} [options.failOnConsoleExceptions] - Whether to fail on console exceptions during conversion.
* @param {number[]} [options.failOnResourceHttpStatusCodes] - Whether to fail on resource HTTP status code.
* @param {boolean} [options.failOnResourceLoadingFailed] - Whether to fail on resource loading failed.
* @param {boolean} [options.skipNetworkIdleEvent] - Whether to skip network idle event.
* @param {boolean} [options.generateDocumentOutline] - Whether to generate document outline.
* @param {Metadata} options.metadata - Metadata to be written.
* @param {Cookie[]} options.cookies - Cookies 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 {Split} [options.split] - Split the PDF into multiple files.
* @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a buffer
*/
async convert({ html, assets, header, footer, properties, pdfFormat, pdfUA, emulatedMediaType, waitDelay, waitForExpression, userAgent, extraHttpHeaders, failOnHttpStatusCodes, failOnConsoleExceptions, failOnResourceHttpStatusCodes, failOnResourceLoadingFailed, skipNetworkIdleEvent, generateDocumentOutline, metadata, cookies, downloadFrom, split, userPassword, ownerPassword, embeds }) {
const data = new FormData();
await common_1.GotenbergUtils.addFile(data, html, 'index.html');
if (assets?.length) {
await Promise.all(assets.map(({ file, name }) => common_1.GotenbergUtils.addFile(data, file, name)));
}
await converter_utils_1.ConverterUtils.customize(data, {
header,
footer,
properties,
pdfFormat,
pdfUA,
emulatedMediaType,
waitDelay,
waitForExpression,
userAgent,
extraHttpHeaders,
failOnHttpStatusCodes,
failOnConsoleExceptions,
failOnResourceHttpStatusCodes,
failOnResourceLoadingFailed,
skipNetworkIdleEvent,
generateDocumentOutline,
metadata,
cookies,
downloadFrom,
split,
userPassword,
ownerPassword,
embeds
});
return common_1.GotenbergUtils.fetch(this.endpoint, data, main_config_1.Chromiumly.getGotenbergApiBasicAuthUsername(), main_config_1.Chromiumly.getGotenbergApiBasicAuthPassword(), main_config_1.Chromiumly.getCustomHttpHeaders());
}
}
exports.HtmlConverter = HtmlConverter;
//# sourceMappingURL=html.converter.js.map