Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. sourceFile,
  61. outputDir,
  62. img,
  63. imgExt,
  64. reSize,
  65. density,
  66. disableExtensionCheck
  67. }) => {
  68. const baseFileName = path.basename(sourceFile);
  69. const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");
  70. const outputImg = outputFile.replace(/\.pdf$/, `-%d.${imgExt || "png"}`);
  71. const ext = path.extname(sourceFile.toLowerCase());
  72. const extensions = [".pdf", ".pptx", ".ppt", ".odp", ".key"];
  73. const pdf = [
  74. "--headless",
  75. "--convert-to",
  76. "pdf",
  77. "--outdir",
  78. outputDir,
  79. sourceFile,
  80. ];
  81. const image = [
  82. "-verbose",
  83. "-resize",
  84. reSize || 1200,
  85. "-density",
  86. density || 120,
  87. `${outputDir}${outputFile}`,
  88. `${outputDir}${outputImg}`,
  89. ];
  90. const pdf2Img = [
  91. "-verbose",
  92. "-resize",
  93. reSize || 1200,
  94. "-density",
  95. density || 120,
  96. sourceFile,
  97. `${outputDir}${outputImg}`,
  98. ];
  99. const libreOfficeBins = [
  100. ...(libreofficeBin ? [libreofficeBin] : []),
  101. ...(Array.isArray(libreofficeBins) ? libreofficeBins : [])
  102. ];
  103. if (!libreofficeBin) {
  104. libreOfficeBins.push(path.resolve("/usr/bin/libreoffice"));
  105. libreOfficeBins.push(path.resolve("/usr/bin/soffice"));
  106. libreOfficeBins.push(path.resolve("/Applications/LibreOffice.app/Contents/MacOS/soffice"));
  107. libreOfficeBins.push(path.resolve("C:\\\\Program Files\\\\LibreOffice\\\\program\\\\soffice.exe"));
  108. libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LibreOffice\\\\program\\\\soffice.exe"));
  109. libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LIBREO~1\\\\program\\\\soffice.exe"));
  110. }
  111. return getFileThatExist(...libreOfficeBins).then((libreofficeBin) => {
  112. if (libreofficeBin) {
  113. //Re arrange the array for more efficient future runs
  114. if (libreOfficeBins[0] !== libreofficeBin) {
  115. libreOfficeBins.splice(libreOfficeBins.indexOf(libreofficeBin), 1);
  116. libreOfficeBins.unshift(libreofficeBin);
  117. }
  118. return filesExist(sourceFile).then((srcExist) => {
  119. if (srcExist) {
  120. if (ext === ".pdf")
  121. return run(libreofficeBin, pdf2Img, "img").then((res) => res);
  122. if (disableExtensionCheck || extensions.includes(ext)) {
  123. return run(libreofficeBin, pdf, "pdf").then((pdfRes) => {
  124. if (pdfRes !== "Error") {
  125. if (!img) {
  126. return pdfRes;
  127. } else {
  128. return run(libreofficeBin, image, "img").then((imageRes) => {
  129. if (imageRes !== "Error") {
  130. return imageRes;
  131. } else {
  132. throw new Error("Error on image conversion process.");
  133. }
  134. });
  135. }
  136. } else {
  137. throw new Error("Error on pdf conversion process.");
  138. }
  139. });
  140. } else {
  141. throw new Error("Invalid extension.");
  142. }
  143. }
  144. });
  145. }
  146. });
  147. };