You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const fs = require("fs");
  2. const path = require("path");
  3. const {spawn} = require("child_process");
  4. function parseCommand(librePath, cmd, convert) {
  5. let _args = [];
  6. if (process.platform === "win32" && convert === "pdf") {
  7. _args.push("/c");
  8. _args.push(librePath);
  9. }
  10. _args = _args.concat(cmd);
  11. return {_args};
  12. }
  13. function fileExist(file) {
  14. return new Promise((resolve, reject) => {
  15. fs.access(file, fs.constants.F_OK, (err) => {
  16. if (err && err.code === "ENOENT") {
  17. resolve(false);
  18. } else {
  19. resolve(true);
  20. }
  21. });
  22. });
  23. }
  24. const getFileThatExist = async (...files) => {
  25. for (const file of files) {
  26. if (file && await fileExist(file) === true) {
  27. return file;
  28. }
  29. }
  30. return false;
  31. }
  32. const filesExist = (...files) => {
  33. return Promise.resolve(!!getFileThatExist(...files));
  34. }
  35. function run(libreOfficeBin, cmd, convert) {
  36. return new Promise((resolve, reject) => {
  37. const {_args} = parseCommand(libreOfficeBin, cmd, convert);
  38. let _cmd = libreOfficeBin;
  39. // if (convert === "img") {
  40. // _cmd = "convert";
  41. // } else if (process.platform === "win32" && convert === "pdf") {
  42. // _cmd = process.env.ComSpec;
  43. // }
  44. const proc = spawn(_cmd, _args);
  45. // proc.stdout.on("data", (data) => {
  46. // // console.log("stdout", data.toString());
  47. // });
  48. proc.stderr.on("error", function (err) {
  49. reject(err);
  50. });
  51. proc.on("close", (code) => {
  52. const status = code === 0 ? "Success" : "Error";
  53. resolve(status);
  54. });
  55. });
  56. }
  57. exports.convert = async ({
  58. libreofficeBin,
  59. libreofficeBins,
  60. convertBin,
  61. sourceFile,
  62. outputDir,
  63. img,
  64. imgExt,
  65. reSize,
  66. density,
  67. disableExtensionCheck
  68. }) => {
  69. const baseFileName = path.basename(sourceFile);
  70. const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");
  71. const outputImg = outputFile.replace(/\.pdf$/, `-%d.${imgExt || "png"}`);
  72. const ext = path.extname(sourceFile.toLowerCase());
  73. const extensions = [".pdf", ".pptx", ".ppt", ".docx", ".doc", ".xlsx", ".xls", ".odp", ".key"];
  74. const pdf = [
  75. "--headless",
  76. "--convert-to",
  77. "pdf",
  78. "--outdir",
  79. outputDir,
  80. sourceFile,
  81. ];
  82. const image = [
  83. "-verbose",
  84. "-resize",
  85. reSize || 1200,
  86. "-density",
  87. density || 120,
  88. `${outputDir}${outputFile}`,
  89. `${outputDir}${outputImg}`,
  90. ];
  91. const pdf2Img = [
  92. "-verbose",
  93. "-resize",
  94. reSize || 1200,
  95. "-density",
  96. density || 120,
  97. sourceFile,
  98. `${outputDir}${outputImg}`,
  99. ];
  100. const libreOfficeBins = [
  101. ...(libreofficeBin ? [libreofficeBin] : []),
  102. ...(Array.isArray(libreofficeBins) ? libreofficeBins : [])
  103. ];
  104. if (!libreofficeBin) {
  105. libreOfficeBins.push(path.resolve("/usr/bin/libreoffice"));
  106. libreOfficeBins.push(path.resolve("/usr/bin/soffice"));
  107. libreOfficeBins.push(path.resolve("/Applications/LibreOffice.app/Contents/MacOS/soffice"));
  108. libreOfficeBins.push(path.resolve("C:\\\\Program Files\\\\LibreOffice\\\\program\\\\soffice.exe"));
  109. libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LibreOffice\\\\program\\\\soffice.exe"));
  110. libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LIBREO~1\\\\program\\\\soffice.exe"));
  111. }
  112. return getFileThatExist(...libreOfficeBins).then((libreofficeBin) => {
  113. if (libreofficeBin) {
  114. //Re arrange the array for more efficient future runs
  115. if (libreOfficeBins[0] !== libreofficeBin) {
  116. libreOfficeBins.splice(libreOfficeBins.indexOf(libreofficeBin), 1);
  117. libreOfficeBins.unshift(libreofficeBin);
  118. }
  119. return filesExist(sourceFile).then((srcExist) => {
  120. if (srcExist) {
  121. if (ext === ".pdf")
  122. return run(libreofficeBin, pdf2Img, "img").then((res) => res);
  123. if (disableExtensionCheck || extensions.includes(ext)) {
  124. return run(libreofficeBin, pdf, "pdf").then((pdfRes) => {
  125. if (pdfRes !== "Error") {
  126. if (!img) {
  127. return pdfRes;
  128. } else {
  129. return run(convertBin, image, "img").then((imageRes) => {
  130. if (imageRes !== "Error") {
  131. return imageRes;
  132. } else {
  133. throw new Error("Error on image conversion process.");
  134. }
  135. });
  136. }
  137. } else {
  138. throw new Error("Error on pdf conversion process.");
  139. }
  140. });
  141. } else {
  142. throw new Error("Invalid extension.");
  143. }
  144. }
  145. });
  146. }
  147. });
  148. };