chromiumly
Version:
A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.
182 lines • 8.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LibreOfficeUtils = void 0;
const tslib_1 = require("tslib");
const fs_1 = require("fs");
const path_1 = tslib_1.__importDefault(require("path"));
const consumers_1 = require("node:stream/consumers");
const common_1 = require("../../common");
const constants_1 = require("./constants");
/**
* Utility class for handling common tasks related to LibreOffice conversions.
*/
class LibreOfficeUtils {
static getFileInfo(file) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (typeof file === 'string') {
yield fs_1.promises.access(file, fs_1.constants.R_OK);
const filename = path_1.default.basename(path_1.default.parse(file).base);
return {
data: file,
ext: path_1.default.extname(filename).slice(1)
};
}
else {
return { data: file.data, ext: file.ext };
}
});
}
/**
* Adds files to the FormData object for LibreOffice conversion.
*
* @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.
* @throws {Error} Throws an error if the file extension is not supported.
*/
static addFiles(files, data) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const paddingLength = String(files.length).length + 1;
yield Promise.all(files.map((file, index) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const fileInfo = yield this.getFileInfo(file);
if (!constants_1.LIBRE_OFFICE_EXTENSIONS.includes(fileInfo.ext)) {
throw new Error(`${fileInfo.ext} is not supported`);
}
const filename = `file${String(index + 1).padStart(paddingLength, '0')}.${fileInfo.ext}`;
if (Buffer.isBuffer(fileInfo.data)) {
data.append('files', new Blob([fileInfo.data]), filename);
}
else if (fileInfo.data instanceof fs_1.ReadStream) {
const content = yield (0, consumers_1.blob)(fileInfo.data);
data.append('files', content, filename);
}
else {
yield fs_1.promises.access(fileInfo.data, fs_1.constants.R_OK);
const content = yield (0, fs_1.openAsBlob)(fileInfo.data);
data.append('files', content, filename);
}
})));
});
}
/**
* 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.landscape) {
data.append('landscape', String(pageProperties.landscape));
}
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}`);
}
if (pageProperties.exportFormFields === false) {
data.append('exportFormFields', String(pageProperties.exportFormFields));
}
if (pageProperties.singlePageSheets) {
data.append('singlePageSheets', String(pageProperties.singlePageSheets));
}
if (pageProperties.allowDuplicateFieldNames) {
data.append('allowDuplicateFieldNames', String(pageProperties.allowDuplicateFieldNames));
}
if (pageProperties.exportBookmarks === false) {
data.append('exportBookmarks', String(pageProperties.exportBookmarks));
}
if (pageProperties.exportBookmarksToPdfDestination) {
data.append('exportBookmarksToPdfDestination', String(pageProperties.exportBookmarksToPdfDestination));
}
if (pageProperties.exportPlaceholders) {
data.append('exportPlaceholders', String(pageProperties.exportPlaceholders));
}
if (pageProperties.exportNotes) {
data.append('exportNotes', String(pageProperties.exportNotes));
}
if (pageProperties.exportNotesPages) {
data.append('exportNotesPages', String(pageProperties.exportNotesPages));
}
if (pageProperties.exportOnlyNotesPages) {
data.append('exportOnlyNotesPages', String(pageProperties.exportOnlyNotesPages));
}
if (pageProperties.exportNotesInMargin) {
data.append('exportNotesInMargin', String(pageProperties.exportNotesInMargin));
}
if (pageProperties.convertOooTargetToPdfTarget) {
data.append('convertOooTargetToPdfTarget', String(pageProperties.convertOooTargetToPdfTarget));
}
if (pageProperties.exportLinksRelativeFsys) {
data.append('exportLinksRelativeFsys', String(pageProperties.exportLinksRelativeFsys));
}
if (pageProperties.exportHiddenSlides) {
data.append('exportHiddenSlides', String(pageProperties.exportHiddenSlides));
}
if (pageProperties.skipEmptyPages) {
data.append('skipEmptyPages', String(pageProperties.skipEmptyPages));
}
if (pageProperties.addOriginalDocumentAsStream) {
data.append('addOriginalDocumentAsStream', String(pageProperties.addOriginalDocumentAsStream));
}
if (pageProperties.password) {
data.append('password', pageProperties.password);
}
}
/**
* 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 customize(data, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (options.pdfa) {
data.append('pdfa', options.pdfa);
}
if (options.pdfUA) {
data.append('pdfua', String(options.pdfUA));
}
if (options.merge) {
data.append('merge', String(options.merge));
}
if (options.metadata) {
data.append('metadata', JSON.stringify(options.metadata));
}
if (options.losslessImageCompression) {
data.append('losslessImageCompression', String(options.losslessImageCompression));
}
if (options.reduceImageResolution) {
data.append('reduceImageResolution', String(options.reduceImageResolution));
}
if (options.quality) {
common_1.GotenbergUtils.assert(options.quality >= 1 && options.quality <= 100, 'Invalid compression quality. Please provide a value between 1 and 100.');
data.append('quality', options.quality);
}
if (options.maxImageResolution) {
common_1.GotenbergUtils.assert(options.reduceImageResolution === true, 'Compression quality is only supported when the reduceImageResolution property is enabled.');
data.append('maxImageResolution', options.maxImageResolution);
}
if (options.downloadFrom) {
data.append('downloadFrom', JSON.stringify(options.downloadFrom));
}
if (options.properties) {
LibreOfficeUtils.addPageProperties(data, options.properties);
}
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.flatten) {
data.append('flatten', String(options.flatten));
}
});
}
}
exports.LibreOfficeUtils = LibreOfficeUtils;
//# sourceMappingURL=libre-office.utils.js.map