|  |  |  |  |  |  | 
													
												
													
														|  |  | const fs = require("fs"); |  |  | const fs = require("fs"); | 
													
												
													
														|  |  | const path = require("path"); |  |  | const path = require("path"); | 
													
												
													
														|  |  | const { spawn } = require("child_process"); |  |  |  | 
													
												
													
														|  |  |  |  |  | const {spawn} = require("child_process"); | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | function parseCommand(librePath, cmd, convert) { |  |  | function parseCommand(librePath, cmd, convert) { | 
													
												
													
														|  |  | let _args = []; |  |  | let _args = []; | 
													
												
											
												
													
														|  |  |  |  |  |  | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | _args = _args.concat(cmd); |  |  | _args = _args.concat(cmd); | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | return { _args }; |  |  |  | 
													
												
													
														|  |  |  |  |  | return {_args}; | 
													
												
													
														|  |  | } |  |  | } | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | function fileExist(file) { |  |  | function fileExist(file) { | 
													
												
													
														|  |  | return new Promise((resolve, reject) => { |  |  | return new Promise((resolve, reject) => { | 
													
												
													
														|  |  | fs.access(file, fs.constants.F_OK, (err) => { |  |  | fs.access(file, fs.constants.F_OK, (err) => { | 
													
												
													
														|  |  | if (err && err.code === "ENOENT") { |  |  | if (err && err.code === "ENOENT") { | 
													
												
													
														|  |  | reject(new Error(`${file} does not exist`)); |  |  |  | 
													
												
													
														|  |  |  |  |  | resolve(false); | 
													
												
													
														|  |  | } else { |  |  | } else { | 
													
												
													
														|  |  | resolve(true); |  |  | resolve(true); | 
													
												
													
														|  |  | } |  |  | } | 
													
												
											
												
													
														|  |  |  |  |  |  | 
													
												
													
														|  |  | }); |  |  | }); | 
													
												
													
														|  |  | } |  |  | } | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  |  |  |  | const getFileThatExist = async (...files) => { | 
													
												
													
														|  |  |  |  |  | for (const file of files) { | 
													
												
													
														|  |  |  |  |  | if (file && await fileExist(file) === true) { | 
													
												
													
														|  |  |  |  |  | return file; | 
													
												
													
														|  |  |  |  |  | } | 
													
												
													
														|  |  |  |  |  | } | 
													
												
													
														|  |  |  |  |  | return false; | 
													
												
													
														|  |  |  |  |  | } | 
													
												
													
														|  |  |  |  |  | 
 | 
													
												
													
														|  |  |  |  |  | const filesExist = (...files) => { | 
													
												
													
														|  |  |  |  |  | return Promise.resolve(!!getFileThatExist(...files)); | 
													
												
													
														|  |  |  |  |  | } | 
													
												
													
														|  |  |  |  |  | 
 | 
													
												
													
														|  |  | function run(libreOfficeBin, cmd, convert) { |  |  | function run(libreOfficeBin, cmd, convert) { | 
													
												
													
														|  |  | return new Promise((resolve, reject) => { |  |  | return new Promise((resolve, reject) => { | 
													
												
													
														|  |  | const { _args } = parseCommand(libreOfficeBin, cmd, convert); |  |  |  | 
													
												
													
														|  |  |  |  |  | const {_args} = parseCommand(libreOfficeBin, cmd, convert); | 
													
												
													
														|  |  | let _cmd = libreOfficeBin; |  |  | let _cmd = libreOfficeBin; | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | if (convert === "img") { |  |  | if (convert === "img") { | 
													
												
											
												
													
														|  |  |  |  |  |  | 
													
												
													
														|  |  | }); |  |  | }); | 
													
												
													
														|  |  | } |  |  | } | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | exports.convert = function ({ |  |  |  | 
													
												
													
														|  |  | libreofficeBin, |  |  |  | 
													
												
													
														|  |  | sourceFile, |  |  |  | 
													
												
													
														|  |  | outputDir, |  |  |  | 
													
												
													
														|  |  | img, |  |  |  | 
													
												
													
														|  |  | imgExt, |  |  |  | 
													
												
													
														|  |  | reSize, |  |  |  | 
													
												
													
														|  |  | density, |  |  |  | 
													
												
													
														|  |  | }) { |  |  |  | 
													
												
													
														|  |  |  |  |  | exports.convert = async ({ | 
													
												
													
														|  |  |  |  |  | libreofficeBin, | 
													
												
													
														|  |  |  |  |  | libreofficeBins, | 
													
												
													
														|  |  |  |  |  | sourceFile, | 
													
												
													
														|  |  |  |  |  | outputDir, | 
													
												
													
														|  |  |  |  |  | img, | 
													
												
													
														|  |  |  |  |  | imgExt, | 
													
												
													
														|  |  |  |  |  | reSize, | 
													
												
													
														|  |  |  |  |  | density, | 
													
												
													
														|  |  |  |  |  | disableExtensionCheck | 
													
												
													
														|  |  |  |  |  | }) => { | 
													
												
													
														|  |  | const baseFileName = path.basename(sourceFile); |  |  | const baseFileName = path.basename(sourceFile); | 
													
												
													
														|  |  | const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf"); |  |  | const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf"); | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
											
												
													
														|  |  |  |  |  |  | 
													
												
													
														|  |  | sourceFile, |  |  | sourceFile, | 
													
												
													
														|  |  | `${outputDir}${outputImg}`, |  |  | `${outputDir}${outputImg}`, | 
													
												
													
														|  |  | ]; |  |  | ]; | 
													
												
													
														|  |  |  |  |  | const libreOfficeBins = [ | 
													
												
													
														|  |  |  |  |  | ...(libreofficeBin ? [libreofficeBin] : []), | 
													
												
													
														|  |  |  |  |  | ...(Array.isArray(libreofficeBins) ? libreofficeBins : []) | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | return fileExist(libreofficeBin).then((binExist) => { |  |  |  | 
													
												
													
														|  |  | if (binExist) { |  |  |  | 
													
												
													
														|  |  | return fileExist(sourceFile).then((srcExist) => { |  |  |  | 
													
												
													
														|  |  |  |  |  | ]; | 
													
												
													
														|  |  |  |  |  | if (!libreofficeBin) { | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.push(path.resolve("/usr/bin/libreoffice")); | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.push(path.resolve("/usr/bin/soffice")); | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.push(path.resolve("/Applications/LibreOffice.app/Contents/MacOS/soffice")); | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.push(path.resolve("C:\\\\Program Files\\\\LibreOffice\\\\program\\\\soffice.exe")); | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LibreOffice\\\\program\\\\soffice.exe")); | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LIBREO~1\\\\program\\\\soffice.exe")); | 
													
												
													
														|  |  |  |  |  | } | 
													
												
													
														|  |  |  |  |  | 
 | 
													
												
													
														|  |  |  |  |  | return getFileThatExist(...libreOfficeBins).then((libreofficeBin) => { | 
													
												
													
														|  |  |  |  |  | if (libreofficeBin) { | 
													
												
													
														|  |  |  |  |  | //Re arrange the array for more efficient future runs | 
													
												
													
														|  |  |  |  |  | if (libreOfficeBins[0] !== libreofficeBin) { | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.splice(libreOfficeBins.indexOf(libreofficeBin), 1); | 
													
												
													
														|  |  |  |  |  | libreOfficeBins.unshift(libreofficeBin); | 
													
												
													
														|  |  |  |  |  | } | 
													
												
													
														|  |  |  |  |  | return filesExist(sourceFile).then((srcExist) => { | 
													
												
													
														|  |  | if (srcExist) { |  |  | if (srcExist) { | 
													
												
													
														|  |  | if (ext === ".pdf") |  |  | if (ext === ".pdf") | 
													
												
													
														|  |  | return run(libreofficeBin, pdf2Img, "img").then((res) => res); |  |  | return run(libreofficeBin, pdf2Img, "img").then((res) => res); | 
													
												
													
														|  |  | 
 |  |  | 
 | 
													
												
													
														|  |  | if (extensions.includes(ext)) { |  |  |  | 
													
												
													
														|  |  |  |  |  | if (disableExtensionCheck || extensions.includes(ext)) { | 
													
												
													
														|  |  | return run(libreofficeBin, pdf, "pdf").then((pdfRes) => { |  |  | return run(libreofficeBin, pdf, "pdf").then((pdfRes) => { | 
													
												
													
														|  |  | if (pdfRes !== "Error") { |  |  | if (pdfRes !== "Error") { | 
													
												
													
														|  |  | if (!img) { |  |  | if (!img) { |