chromiumly 2.10.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -29
- package/dist/libre-office/index.d.ts +1 -2
- package/dist/libre-office/index.js +3 -3
- package/dist/libre-office/index.js.map +1 -1
- package/dist/libre-office/interfaces/libre-office.types.d.ts +18 -0
- package/dist/libre-office/libre-office.d.ts +27 -0
- package/dist/libre-office/libre-office.js +41 -0
- package/dist/libre-office/libre-office.js.map +1 -0
- package/dist/libre-office/utils/libre-office.utils.d.ts +10 -2
- package/dist/libre-office/utils/libre-office.utils.js +47 -26
- package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
- package/dist/main.d.ts +2 -1
- package/dist/main.js +4 -2
- package/dist/main.js.map +1 -1
- package/dist/pdf-engines/index.d.ts +1 -1
- package/dist/pdf-engines/index.js +3 -3
- package/dist/pdf-engines/index.js.map +1 -1
- package/dist/pdf-engines/interfaces/pdf-engines.types.d.ts +8 -0
- package/dist/pdf-engines/interfaces/pdf-engines.types.js +3 -0
- package/dist/pdf-engines/interfaces/pdf-engines.types.js.map +1 -0
- package/dist/pdf-engines/{pdf.engine.d.ts → pdf-engines.d.ts} +9 -15
- package/dist/pdf-engines/{pdf.engine.js → pdf-engines.js} +22 -28
- package/dist/pdf-engines/pdf-engines.js.map +1 -0
- package/dist/pdf-engines/utils/{engine.utils.d.ts → pdf-engines.utils.d.ts} +10 -1
- package/dist/pdf-engines/utils/{engine.utils.js → pdf-engines.utils.js} +24 -4
- package/dist/pdf-engines/utils/pdf-engines.utils.js.map +1 -0
- package/package.json +3 -4
- package/src/chromium/interfaces/common.types.ts +1 -1
- package/src/libre-office/index.ts +1 -2
- package/src/libre-office/interfaces/libre-office.types.ts +150 -0
- package/src/libre-office/libre-office.ts +56 -0
- package/src/libre-office/tests/libre-office.test.ts +56 -0
- package/src/libre-office/utils/libre-office.utils.ts +61 -32
- package/src/libre-office/utils/tests/libre-office.utils.test.ts +9 -10
- package/src/main.ts +2 -1
- package/src/pdf-engines/index.ts +1 -1
- package/src/pdf-engines/interfaces/pdf-engines.types.ts +10 -0
- package/src/pdf-engines/{pdf.engine.ts → pdf-engines.ts} +27 -39
- package/src/pdf-engines/tests/pdf.engine.test.ts +20 -19
- package/src/pdf-engines/utils/{engine.utils.ts → pdf-engines.utils.ts} +29 -1
- package/src/pdf-engines/utils/tests/{engine.utils.test.ts → pdf-engines.utils.test.ts} +6 -7
- package/dist/pdf-engines/pdf.engine.js.map +0 -1
- package/dist/pdf-engines/utils/engine.utils.js.map +0 -1
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PDFEngines = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
7
7
|
const form_data_1 = tslib_1.__importDefault(require("form-data"));
|
|
8
8
|
const main_config_1 = require("../main.config");
|
|
9
9
|
const common_1 = require("../common");
|
|
10
|
-
const
|
|
11
|
-
const engine_utils_1 = require("./utils/engine.utils");
|
|
10
|
+
const pdf_engines_utils_1 = require("./utils/pdf-engines.utils");
|
|
12
11
|
/**
|
|
13
|
-
* Class
|
|
12
|
+
* Class uses PDF engines for various operations such as merging and conversion.
|
|
14
13
|
*/
|
|
15
|
-
class
|
|
14
|
+
class PDFEngines {
|
|
16
15
|
/**
|
|
17
16
|
* Merges multiple PDF files into a single PDF document.
|
|
18
17
|
*
|
|
@@ -21,9 +20,14 @@ class PDFEngine {
|
|
|
21
20
|
* @returns {Promise<Buffer>} A Promise resolving to the merged PDF content as a Buffer.
|
|
22
21
|
*/
|
|
23
22
|
static merge(_a) {
|
|
24
|
-
return tslib_1.__awaiter(this, arguments, void 0, function* ({ files }) {
|
|
23
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, pdfa, pdfUA, metadata }) {
|
|
25
24
|
const data = new form_data_1.default();
|
|
26
|
-
yield
|
|
25
|
+
yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
|
|
26
|
+
yield pdf_engines_utils_1.PDFEnginesUtils.customize(data, {
|
|
27
|
+
pdfa,
|
|
28
|
+
pdfUA,
|
|
29
|
+
metadata
|
|
30
|
+
});
|
|
27
31
|
const endpoint = `${main_config_1.Chromiumly.GOTENBERG_ENDPOINT}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.merge}`;
|
|
28
32
|
return common_1.GotenbergUtils.fetch(endpoint, data);
|
|
29
33
|
});
|
|
@@ -33,28 +37,18 @@ class PDFEngine {
|
|
|
33
37
|
*
|
|
34
38
|
* @param {Object} options - Options for the conversion operation.
|
|
35
39
|
* @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the files to be converted to PDF.
|
|
36
|
-
* @param {
|
|
37
|
-
* @param {PdfFormat} [options.pdfFormat] - PDF format options.
|
|
40
|
+
* @param {pdfa} [options.pdfa] - PDF format options.
|
|
38
41
|
* @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output.
|
|
39
|
-
* @param {boolean} [options.merge] - Indicates whether to merge the resulting PDFs.
|
|
40
42
|
* @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a Buffer.
|
|
41
43
|
*/
|
|
42
44
|
static convert(_a) {
|
|
43
|
-
return tslib_1.__awaiter(this, arguments, void 0, function* ({ files,
|
|
45
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, pdfa, pdfUA }) {
|
|
44
46
|
const data = new form_data_1.default();
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
if (merge) {
|
|
52
|
-
data.append('merge', String(merge));
|
|
53
|
-
}
|
|
54
|
-
if (properties) {
|
|
55
|
-
libre_office_1.LibreOfficeUtils.addPageProperties(data, properties);
|
|
56
|
-
}
|
|
57
|
-
yield libre_office_1.LibreOfficeUtils.addFiles(files, data);
|
|
47
|
+
yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
|
|
48
|
+
yield pdf_engines_utils_1.PDFEnginesUtils.customize(data, {
|
|
49
|
+
pdfa,
|
|
50
|
+
pdfUA
|
|
51
|
+
});
|
|
58
52
|
const endpoint = `${main_config_1.Chromiumly.GOTENBERG_ENDPOINT}/${main_config_1.Chromiumly.LIBRE_OFFICE_PATH}/${main_config_1.Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
|
|
59
53
|
return common_1.GotenbergUtils.fetch(endpoint, data);
|
|
60
54
|
});
|
|
@@ -68,7 +62,7 @@ class PDFEngine {
|
|
|
68
62
|
static readMetadata(files) {
|
|
69
63
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
70
64
|
const data = new form_data_1.default();
|
|
71
|
-
yield
|
|
65
|
+
yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
|
|
72
66
|
const endpoint = `${main_config_1.Chromiumly.GOTENBERG_ENDPOINT}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.readMetadata}`;
|
|
73
67
|
return common_1.GotenbergUtils.fetch(endpoint, data);
|
|
74
68
|
});
|
|
@@ -84,7 +78,7 @@ class PDFEngine {
|
|
|
84
78
|
return tslib_1.__awaiter(this, arguments, void 0, function* ({ files, metadata }) {
|
|
85
79
|
const data = new form_data_1.default();
|
|
86
80
|
data.append('metadata', JSON.stringify(metadata));
|
|
87
|
-
yield
|
|
81
|
+
yield pdf_engines_utils_1.PDFEnginesUtils.addFiles(files, data);
|
|
88
82
|
const endpoint = `${main_config_1.Chromiumly.GOTENBERG_ENDPOINT}/${main_config_1.Chromiumly.PDF_ENGINES_PATH}/${main_config_1.Chromiumly.PDF_ENGINE_ROUTES.writeMetadata}`;
|
|
89
83
|
return common_1.GotenbergUtils.fetch(endpoint, data);
|
|
90
84
|
});
|
|
@@ -104,5 +98,5 @@ class PDFEngine {
|
|
|
104
98
|
});
|
|
105
99
|
}
|
|
106
100
|
}
|
|
107
|
-
exports.
|
|
108
|
-
//# sourceMappingURL=pdf.
|
|
101
|
+
exports.PDFEngines = PDFEngines;
|
|
102
|
+
//# sourceMappingURL=pdf-engines.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pdf-engines.js","sourceRoot":"","sources":["../../src/pdf-engines/pdf-engines.ts"],"names":[],"mappings":";;;;AAAA,2BAA8B;AAC9B,wDAAwB;AAExB,kEAAiC;AAEjC,gDAA4C;AAC5C,sCAKmB;AACnB,iEAA4D;AAE5D;;GAEG;AACH,MAAa,UAAU;IACnB;;;;;;OAMG;IACI,MAAM,CAAO,KAAK;qEAAC,EACtB,KAAK,EACL,IAAI,EACJ,KAAK,EACL,QAAQ,EAMX;YACG,MAAM,IAAI,GAAG,IAAI,mBAAQ,EAAE,CAAC;YAC5B,MAAM,mCAAe,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;YAC5C,MAAM,mCAAe,CAAC,SAAS,CAAC,IAAI,EAAE;gBAClC,IAAI;gBACJ,KAAK;gBACL,QAAQ;aACX,CAAC,CAAC;YACH,MAAM,QAAQ,GAAG,GAAG,wBAAU,CAAC,kBAAkB,IAAI,wBAAU,CAAC,gBAAgB,IAAI,wBAAU,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC;YACzH,OAAO,uBAAc,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAChD,CAAC;KAAA;IAED;;;;;;;;OAQG;IACI,MAAM,CAAO,OAAO;qEAAC,EACxB,KAAK,EACL,IAAI,EACJ,KAAK,EAKR;YACG,MAAM,IAAI,GAAG,IAAI,mBAAQ,EAAE,CAAC;YAE5B,MAAM,mCAAe,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,C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;;;;OAMG;IACI,MAAM,CAAO,QAAQ,CACxB,QAAgB,EAChB,MAAc;;YAEd,MAAM,aAAa,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,eAAe,CAAC,CAAC;YACnE,MAAM,aAAQ,CAAC,KAAK,CAAC,cAAI,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YACvE,MAAM,aAAQ,CAAC,SAAS,CAAC,cAAI,CAAC,OAAO,CAAC,aAAa,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;QAC5E,CAAC;KAAA;CACJ;AAtHD,gCAsHC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import FormData from 'form-data';
|
|
2
2
|
import { PathLikeOrReadStream } from '../../common';
|
|
3
|
+
import { ConversionOptions, MergeOptions } from '../interfaces/pdf-engines.types';
|
|
3
4
|
/**
|
|
4
5
|
* Utility class for handling common tasks related to PDF engine operations.
|
|
5
6
|
*/
|
|
6
|
-
export declare class
|
|
7
|
+
export declare class PDFEnginesUtils {
|
|
7
8
|
/**
|
|
8
9
|
* Adds PDF files to the FormData object.
|
|
9
10
|
*
|
|
@@ -12,4 +13,12 @@ export declare class PDFEngineUtils {
|
|
|
12
13
|
* @throws {Error} Throws an error if the file extension is not supported.
|
|
13
14
|
*/
|
|
14
15
|
static addFiles(files: PathLikeOrReadStream[], data: FormData): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Customizes the FormData object based on the provided conversion options.
|
|
18
|
+
*
|
|
19
|
+
* @param {FormData} data - The FormData object to be customized.
|
|
20
|
+
* @param {ConversionOptions | MergeOptions} options - The options to apply to the FormData.
|
|
21
|
+
* @returns {Promise<void>} A Promise that resolves once the customization is complete.
|
|
22
|
+
*/
|
|
23
|
+
static customize(data: FormData, options: ConversionOptions | MergeOptions): Promise<void>;
|
|
15
24
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PDFEnginesUtils = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const path_1 = tslib_1.__importDefault(require("path"));
|
|
7
7
|
/**
|
|
8
8
|
* Utility class for handling common tasks related to PDF engine operations.
|
|
9
9
|
*/
|
|
10
|
-
class
|
|
10
|
+
class PDFEnginesUtils {
|
|
11
11
|
/**
|
|
12
12
|
* Adds PDF files to the FormData object.
|
|
13
13
|
*
|
|
@@ -39,6 +39,26 @@ class PDFEngineUtils {
|
|
|
39
39
|
}
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Customizes the FormData object based on the provided conversion options.
|
|
44
|
+
*
|
|
45
|
+
* @param {FormData} data - The FormData object to be customized.
|
|
46
|
+
* @param {ConversionOptions | MergeOptions} options - The options to apply to the FormData.
|
|
47
|
+
* @returns {Promise<void>} A Promise that resolves once the customization is complete.
|
|
48
|
+
*/
|
|
49
|
+
static customize(data, options) {
|
|
50
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
if (options.pdfa) {
|
|
52
|
+
data.append('pdfa', options.pdfa);
|
|
53
|
+
}
|
|
54
|
+
if (options.pdfUA) {
|
|
55
|
+
data.append('pdfUA', String(options.pdfUA));
|
|
56
|
+
}
|
|
57
|
+
if ('metadata' in options && options.metadata) {
|
|
58
|
+
data.append('metadata', JSON.stringify(options.metadata));
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
42
62
|
}
|
|
43
|
-
exports.
|
|
44
|
-
//# sourceMappingURL=
|
|
63
|
+
exports.PDFEnginesUtils = PDFEnginesUtils;
|
|
64
|
+
//# sourceMappingURL=pdf-engines.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pdf-engines.utils.js","sourceR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IAAI,EAAE,CAAC;gBACf,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;YACtC,CAAC;YAED,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;gBAChB,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;YAChD,CAAC;YAED,IAAI,UAAU,IAAI,OAAO,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAC5C,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;YAC9D,CAAC;QACL,CAAC;KAAA;CACJ;AAtDD,0CAsDC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chromiumly",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.",
|
|
5
5
|
"main": "dist/main.js",
|
|
6
6
|
"types": "dist/main.d.ts",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@types/dotenv": "8.2.0",
|
|
51
51
|
"@types/form-data": "2.5.0",
|
|
52
52
|
"@types/jest": "29.5.12",
|
|
53
|
-
"@types/node": "20.12.
|
|
53
|
+
"@types/node": "20.12.5",
|
|
54
54
|
"@types/node-fetch": "2.6.11",
|
|
55
55
|
"@typescript-eslint/eslint-plugin": "7.5.0",
|
|
56
56
|
"@typescript-eslint/parser": "7.5.0",
|
|
@@ -67,12 +67,11 @@
|
|
|
67
67
|
"ts-jest": "29.1.2",
|
|
68
68
|
"ts-node": "10.9.2",
|
|
69
69
|
"tslib": "2.6.2",
|
|
70
|
-
"typescript": "5.4.
|
|
70
|
+
"typescript": "5.4.4"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
73
|
"config": "3.3.11",
|
|
74
74
|
"dotenv": "16.4.5",
|
|
75
|
-
"file-type": "16.5.4",
|
|
76
75
|
"form-data": "4.0.0",
|
|
77
76
|
"node-fetch": "2.7.0"
|
|
78
77
|
},
|
|
@@ -6,7 +6,7 @@ export type ChromiumOptions = {
|
|
|
6
6
|
header?: PathLikeOrReadStream;
|
|
7
7
|
footer?: PathLikeOrReadStream;
|
|
8
8
|
emulatedMediaType?: EmulatedMediaType;
|
|
9
|
-
waitDelay?: string; // Duration (e.g, '5s') to wait when loading an HTML document before
|
|
9
|
+
waitDelay?: string; // Duration (e.g, '5s') to wait when loading an HTML document before conversion.
|
|
10
10
|
waitForExpression?: string; // JavaScript's expression to wait before converting an HTML document into PDF until it returns true.
|
|
11
11
|
extraHttpHeaders?: Record<string, string>;
|
|
12
12
|
failOnHttpStatusCodes?: number[]; // Return a 409 Conflict response if the HTTP status code is in the list (default [499,599])
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { LibreOfficeUtils } from './utils/libre-office.utils';
|
|
1
|
+
export { LibreOffice } from './libre-office';
|
|
@@ -1,5 +1,155 @@
|
|
|
1
|
+
import { ReadStream } from 'fs';
|
|
2
|
+
import { Metadata, PdfFormat } from '../../common';
|
|
3
|
+
|
|
4
|
+
type FileExtension =
|
|
5
|
+
| '123'
|
|
6
|
+
| '602'
|
|
7
|
+
| 'abw'
|
|
8
|
+
| 'bib'
|
|
9
|
+
| 'bmp'
|
|
10
|
+
| 'cdr'
|
|
11
|
+
| 'cgm'
|
|
12
|
+
| 'cmx'
|
|
13
|
+
| 'csv'
|
|
14
|
+
| 'cwk'
|
|
15
|
+
| 'dbf'
|
|
16
|
+
| 'dif'
|
|
17
|
+
| 'doc'
|
|
18
|
+
| 'docm'
|
|
19
|
+
| 'docx'
|
|
20
|
+
| 'dot'
|
|
21
|
+
| 'dotm'
|
|
22
|
+
| 'dotx'
|
|
23
|
+
| 'dxf'
|
|
24
|
+
| 'emf'
|
|
25
|
+
| 'eps'
|
|
26
|
+
| 'epub'
|
|
27
|
+
| 'fodg'
|
|
28
|
+
| 'fodp'
|
|
29
|
+
| 'fods'
|
|
30
|
+
| 'fodt'
|
|
31
|
+
| 'fopd'
|
|
32
|
+
| 'gif'
|
|
33
|
+
| 'htm'
|
|
34
|
+
| 'html'
|
|
35
|
+
| 'hwp'
|
|
36
|
+
| 'jpeg'
|
|
37
|
+
| 'jpg'
|
|
38
|
+
| 'key'
|
|
39
|
+
| 'ltx'
|
|
40
|
+
| 'lwp'
|
|
41
|
+
| 'mcw'
|
|
42
|
+
| 'met'
|
|
43
|
+
| 'mml'
|
|
44
|
+
| 'mw'
|
|
45
|
+
| 'numbers'
|
|
46
|
+
| 'odd'
|
|
47
|
+
| 'odg'
|
|
48
|
+
| 'odm'
|
|
49
|
+
| 'odp'
|
|
50
|
+
| 'ods'
|
|
51
|
+
| 'odt'
|
|
52
|
+
| 'otg'
|
|
53
|
+
| 'oth'
|
|
54
|
+
| 'otp'
|
|
55
|
+
| 'ots'
|
|
56
|
+
| 'ott'
|
|
57
|
+
| 'pages'
|
|
58
|
+
| 'pbm'
|
|
59
|
+
| 'pcd'
|
|
60
|
+
| 'pct'
|
|
61
|
+
| 'pcx'
|
|
62
|
+
| 'pdb'
|
|
63
|
+
| 'pdf'
|
|
64
|
+
| 'pgm'
|
|
65
|
+
| 'png'
|
|
66
|
+
| 'pot'
|
|
67
|
+
| 'potm'
|
|
68
|
+
| 'potx'
|
|
69
|
+
| 'ppm'
|
|
70
|
+
| 'pps'
|
|
71
|
+
| 'ppt'
|
|
72
|
+
| 'pptm'
|
|
73
|
+
| 'pptx'
|
|
74
|
+
| 'psd'
|
|
75
|
+
| 'psw'
|
|
76
|
+
| 'pub'
|
|
77
|
+
| 'pwp'
|
|
78
|
+
| 'pxl'
|
|
79
|
+
| 'ras'
|
|
80
|
+
| 'rtf'
|
|
81
|
+
| 'sda'
|
|
82
|
+
| 'sdc'
|
|
83
|
+
| 'sdd'
|
|
84
|
+
| 'sdp'
|
|
85
|
+
| 'sdw'
|
|
86
|
+
| 'sgl'
|
|
87
|
+
| 'slk'
|
|
88
|
+
| 'smf'
|
|
89
|
+
| 'stc'
|
|
90
|
+
| 'std'
|
|
91
|
+
| 'sti'
|
|
92
|
+
| 'stw'
|
|
93
|
+
| 'svg'
|
|
94
|
+
| 'svm'
|
|
95
|
+
| 'swf'
|
|
96
|
+
| 'sxc'
|
|
97
|
+
| 'sxd'
|
|
98
|
+
| 'sxg'
|
|
99
|
+
| 'sxi'
|
|
100
|
+
| 'sxm'
|
|
101
|
+
| 'sxw'
|
|
102
|
+
| 'tga'
|
|
103
|
+
| 'tif'
|
|
104
|
+
| 'tiff'
|
|
105
|
+
| 'txt'
|
|
106
|
+
| 'uof'
|
|
107
|
+
| 'uop'
|
|
108
|
+
| 'uos'
|
|
109
|
+
| 'uot'
|
|
110
|
+
| 'vdx'
|
|
111
|
+
| 'vor'
|
|
112
|
+
| 'vsd'
|
|
113
|
+
| 'vsdm'
|
|
114
|
+
| 'vsdx'
|
|
115
|
+
| 'wb2'
|
|
116
|
+
| 'wk1'
|
|
117
|
+
| 'wks'
|
|
118
|
+
| 'wmf'
|
|
119
|
+
| 'wpd'
|
|
120
|
+
| 'wpg'
|
|
121
|
+
| 'wps'
|
|
122
|
+
| 'xbm'
|
|
123
|
+
| 'xhtml'
|
|
124
|
+
| 'xls'
|
|
125
|
+
| 'xlsb'
|
|
126
|
+
| 'xlsm'
|
|
127
|
+
| 'xlsx'
|
|
128
|
+
| 'xlt'
|
|
129
|
+
| 'xltm'
|
|
130
|
+
| 'xltx'
|
|
131
|
+
| 'xlw'
|
|
132
|
+
| 'xml'
|
|
133
|
+
| 'xpm'
|
|
134
|
+
| 'zabw';
|
|
135
|
+
|
|
136
|
+
type FileInfo = {
|
|
137
|
+
data: Buffer | ReadStream;
|
|
138
|
+
ext: FileExtension;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export type PathLikeOrReadStream = string | FileInfo;
|
|
142
|
+
|
|
1
143
|
export type PageProperties = {
|
|
2
144
|
landscape?: boolean; // Set the paper orientation to landscape (default false)
|
|
3
145
|
nativePageRanges?: { from: number; to: number }; // Page ranges to print
|
|
4
146
|
exportFormFields?: boolean; // Set whether to export the form fields or to use the inputted/selected content of the fields (default true)
|
|
5
147
|
};
|
|
148
|
+
|
|
149
|
+
export type ConversionOptions = {
|
|
150
|
+
properties?: PageProperties;
|
|
151
|
+
merge?: boolean;
|
|
152
|
+
pdfa?: PdfFormat;
|
|
153
|
+
pdfUA?: boolean;
|
|
154
|
+
metadata?: Metadata;
|
|
155
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import FormData from 'form-data';
|
|
2
|
+
|
|
3
|
+
import { Chromiumly } from '../main.config';
|
|
4
|
+
import { GotenbergUtils, PdfFormat, Metadata } from '../common';
|
|
5
|
+
import {
|
|
6
|
+
PageProperties,
|
|
7
|
+
PathLikeOrReadStream
|
|
8
|
+
} from './interfaces/libre-office.types';
|
|
9
|
+
import { LibreOfficeUtils } from './utils/libre-office.utils';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Class representing a LibreOffice for various operations such as merging and conversion.
|
|
13
|
+
*/
|
|
14
|
+
export class LibreOffice {
|
|
15
|
+
/**
|
|
16
|
+
* Converts various document formats to PDF.
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} options - Options for the conversion operation.
|
|
19
|
+
* @param {PathLikeOrReadStream[]} options.files - An array of PathLikes or ReadStreams to the files to be converted to PDF.
|
|
20
|
+
* @param {PageProperties} [options.properties] - Page properties for the conversion.
|
|
21
|
+
* @param {pdfa} [options.pdfFormat] - PDF format options.
|
|
22
|
+
* @param {boolean} [options.pdfUA] - Indicates whether to generate PDF/UA compliant output.
|
|
23
|
+
* @param {boolean} [options.merge] - Indicates whether to merge the resulting PDFs.
|
|
24
|
+
* @returns {Promise<Buffer>} A Promise resolving to the converted PDF content as a Buffer.
|
|
25
|
+
*/
|
|
26
|
+
public static async convert({
|
|
27
|
+
files,
|
|
28
|
+
properties,
|
|
29
|
+
pdfa,
|
|
30
|
+
pdfUA,
|
|
31
|
+
merge,
|
|
32
|
+
metadata
|
|
33
|
+
}: {
|
|
34
|
+
files: PathLikeOrReadStream[];
|
|
35
|
+
properties?: PageProperties;
|
|
36
|
+
pdfa?: PdfFormat;
|
|
37
|
+
pdfUA?: boolean;
|
|
38
|
+
merge?: boolean;
|
|
39
|
+
metadata?: Metadata;
|
|
40
|
+
}): Promise<Buffer> {
|
|
41
|
+
const data = new FormData();
|
|
42
|
+
|
|
43
|
+
await LibreOfficeUtils.addFiles(files, data);
|
|
44
|
+
await LibreOfficeUtils.customize(data, {
|
|
45
|
+
properties,
|
|
46
|
+
merge,
|
|
47
|
+
pdfa,
|
|
48
|
+
pdfUA,
|
|
49
|
+
metadata
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
|
|
53
|
+
|
|
54
|
+
return GotenbergUtils.fetch(endpoint, data);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { LibreOffice } from '../libre-office';
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3
|
+
import { createReadStream, promises } from 'fs';
|
|
4
|
+
import FormData from 'form-data';
|
|
5
|
+
import fetch from 'node-fetch';
|
|
6
|
+
import { PdfFormat } from '../../common';
|
|
7
|
+
|
|
8
|
+
const { Response } = jest.requireActual('node-fetch');
|
|
9
|
+
jest.mock('node-fetch', () => jest.fn());
|
|
10
|
+
|
|
11
|
+
describe('LibreOffice', () => {
|
|
12
|
+
const mockPromisesAccess = jest.spyOn(promises, 'access');
|
|
13
|
+
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
|
|
14
|
+
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
(createReadStream as jest.Mock) = jest
|
|
18
|
+
.fn()
|
|
19
|
+
.mockImplementation((file) => file);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
jest.resetAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('convert', () => {
|
|
27
|
+
describe('when no properties are passed', () => {
|
|
28
|
+
it('should return a buffer', async () => {
|
|
29
|
+
mockPromisesAccess.mockResolvedValue();
|
|
30
|
+
mockFetch.mockResolvedValue(new Response('content'));
|
|
31
|
+
const buffer = await LibreOffice.convert({
|
|
32
|
+
files: ['path/to/file.docx', 'path/to/file.bib']
|
|
33
|
+
});
|
|
34
|
+
expect(buffer).toEqual(Buffer.from('content'));
|
|
35
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('when properties are passed', () => {
|
|
40
|
+
it('should return a buffer', async () => {
|
|
41
|
+
mockPromisesAccess.mockResolvedValue();
|
|
42
|
+
mockFetch.mockResolvedValue(new Response('content'));
|
|
43
|
+
const buffer = await LibreOffice.convert({
|
|
44
|
+
files: ['path/to/file.docx', 'path/to/file.bib'],
|
|
45
|
+
properties: { landscape: true },
|
|
46
|
+
pdfa: PdfFormat.A_2b,
|
|
47
|
+
pdfUA: true,
|
|
48
|
+
metadata: { author: 'John Doe' },
|
|
49
|
+
merge: true
|
|
50
|
+
});
|
|
51
|
+
expect(buffer).toEqual(Buffer.from('content'));
|
|
52
|
+
expect(mockFormDataAppend).toHaveBeenCalledTimes(7);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -1,18 +1,33 @@
|
|
|
1
|
-
import { constants, createReadStream, promises
|
|
1
|
+
import { constants, createReadStream, promises } from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
|
|
4
|
-
import { fromStream, fromBuffer } from 'file-type';
|
|
5
|
-
|
|
6
4
|
import FormData from 'form-data';
|
|
7
5
|
|
|
8
|
-
import { GotenbergUtils
|
|
6
|
+
import { GotenbergUtils } from '../../common';
|
|
9
7
|
import { LIBRE_OFFICE_EXTENSIONS } from './constants';
|
|
10
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
ConversionOptions,
|
|
10
|
+
PageProperties,
|
|
11
|
+
PathLikeOrReadStream
|
|
12
|
+
} from '../interfaces/libre-office.types';
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Utility class for handling common tasks related to LibreOffice conversions.
|
|
14
16
|
*/
|
|
15
17
|
export class LibreOfficeUtils {
|
|
18
|
+
private static async getFileInfo(file: PathLikeOrReadStream) {
|
|
19
|
+
if (typeof file === 'string') {
|
|
20
|
+
await promises.access(file, constants.R_OK);
|
|
21
|
+
const filename = path.basename(path.parse(file).base);
|
|
22
|
+
return {
|
|
23
|
+
data: createReadStream(file),
|
|
24
|
+
ext: path.extname(filename).slice(1)
|
|
25
|
+
};
|
|
26
|
+
} else {
|
|
27
|
+
return { data: file.data, ext: file.ext };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
16
31
|
/**
|
|
17
32
|
* Adds files to the FormData object for LibreOffice conversion.
|
|
18
33
|
*
|
|
@@ -24,33 +39,15 @@ export class LibreOfficeUtils {
|
|
|
24
39
|
files: PathLikeOrReadStream[],
|
|
25
40
|
data: FormData
|
|
26
41
|
) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
await promises.access(value, constants.R_OK);
|
|
37
|
-
const filename = path.basename(value.toString());
|
|
38
|
-
fileInfo = { ext: path.extname(filename).slice(1) };
|
|
39
|
-
file = createReadStream(value);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!fileInfo) {
|
|
43
|
-
throw new Error('File type could not be determined');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const extension = fileInfo.ext;
|
|
47
|
-
|
|
48
|
-
if (LIBRE_OFFICE_EXTENSIONS.includes(extension)) {
|
|
49
|
-
data.append('files', file, `${key}.${extension}`);
|
|
50
|
-
} else {
|
|
51
|
-
throw new Error(`${extension} is not supported`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
42
|
+
await Promise.all(
|
|
43
|
+
files.map(async (file, key) => {
|
|
44
|
+
const fileInfo = await this.getFileInfo(file);
|
|
45
|
+
if (!LIBRE_OFFICE_EXTENSIONS.includes(fileInfo.ext)) {
|
|
46
|
+
throw new Error(`${fileInfo.ext} is not supported`);
|
|
47
|
+
}
|
|
48
|
+
data.append('files', fileInfo.data, `${key}.${fileInfo.ext}`);
|
|
49
|
+
})
|
|
50
|
+
);
|
|
54
51
|
}
|
|
55
52
|
|
|
56
53
|
/**
|
|
@@ -89,4 +86,36 @@ export class LibreOfficeUtils {
|
|
|
89
86
|
);
|
|
90
87
|
}
|
|
91
88
|
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Customizes the FormData object based on the provided conversion options.
|
|
92
|
+
*
|
|
93
|
+
* @param {FormData} data - The FormData object to be customized.
|
|
94
|
+
* @param {ConversionOptions} options - The conversion options to apply to the FormData.
|
|
95
|
+
* @returns {Promise<void>} A Promise that resolves once the customization is complete.
|
|
96
|
+
*/
|
|
97
|
+
public static async customize(
|
|
98
|
+
data: FormData,
|
|
99
|
+
options: ConversionOptions
|
|
100
|
+
): Promise<void> {
|
|
101
|
+
if (options.pdfa) {
|
|
102
|
+
data.append('pdfa', options.pdfa);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (options.pdfUA) {
|
|
106
|
+
data.append('pdfUA', String(options.pdfUA));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (options.merge) {
|
|
110
|
+
data.append('merge', String(options.merge));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (options.metadata) {
|
|
114
|
+
data.append('metadata', JSON.stringify(options.metadata));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (options.properties) {
|
|
118
|
+
LibreOfficeUtils.addPageProperties(data, options.properties);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
92
121
|
}
|
|
@@ -3,12 +3,10 @@ import { promises, createReadStream } from 'fs';
|
|
|
3
3
|
import { LibreOfficeUtils } from '../libre-office.utils';
|
|
4
4
|
|
|
5
5
|
import FormData from 'form-data';
|
|
6
|
-
import FileType from 'file-type';
|
|
7
6
|
|
|
8
7
|
describe('LibreOfficeUtils', () => {
|
|
9
8
|
const mockPromisesAccess = jest.spyOn(promises, 'access');
|
|
10
9
|
const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
|
|
11
|
-
const mockFromBuffer = jest.spyOn(FileType, 'fromBuffer');
|
|
12
10
|
|
|
13
11
|
const data = new FormData();
|
|
14
12
|
|
|
@@ -37,12 +35,11 @@ describe('LibreOfficeUtils', () => {
|
|
|
37
35
|
describe('when files parameter contains a buffer', () => {
|
|
38
36
|
it('should append each file to data', async () => {
|
|
39
37
|
mockPromisesAccess.mockResolvedValueOnce();
|
|
40
|
-
mockFromBuffer.mockResolvedValueOnce({
|
|
41
|
-
ext: 'docx',
|
|
42
|
-
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
43
|
-
});
|
|
44
38
|
await LibreOfficeUtils.addFiles(
|
|
45
|
-
[
|
|
39
|
+
[
|
|
40
|
+
{ data: Buffer.from('data'), ext: 'csv' },
|
|
41
|
+
'path/to/file.bib'
|
|
42
|
+
],
|
|
46
43
|
data
|
|
47
44
|
);
|
|
48
45
|
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
|
|
@@ -53,14 +50,16 @@ describe('LibreOfficeUtils', () => {
|
|
|
53
50
|
describe('when one of the files has undetermined format', () => {
|
|
54
51
|
it('should throw an error', async () => {
|
|
55
52
|
mockPromisesAccess.mockResolvedValueOnce();
|
|
56
|
-
mockFromBuffer.mockResolvedValueOnce(undefined);
|
|
57
53
|
|
|
58
54
|
await expect(
|
|
59
55
|
LibreOfficeUtils.addFiles(
|
|
60
|
-
[
|
|
56
|
+
[
|
|
57
|
+
{ data: Buffer.from('data'), ext: 'docx' },
|
|
58
|
+
'path/to/file.none'
|
|
59
|
+
],
|
|
61
60
|
data
|
|
62
61
|
)
|
|
63
|
-
).rejects.toThrow('
|
|
62
|
+
).rejects.toThrow('none is not supported');
|
|
64
63
|
});
|
|
65
64
|
});
|
|
66
65
|
|
package/src/main.ts
CHANGED