您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. const fs = require("fs");
  2. const path = require("path");
  3. const async = require("async");
  4. const { spawn } = require("child_process");
  5. function libreOffice(libreofficeBin, callback) {
  6. if (libreofficeBin) {
  7. return callback(null, libreofficeBin);
  8. } else {
  9. let paths = [];
  10. switch (process.platform) {
  11. case "darwin":
  12. paths = ["/Applications/LibreOffice.app/Contents/MacOS/soffice"];
  13. break;
  14. case "linux":
  15. paths = ["/usr/bin/libreoffice", "/usr/bin/soffice"];
  16. break;
  17. case "win32":
  18. paths = [
  19. path.join(
  20. process.env["PROGRAMFILES(X86)"],
  21. "LIBREO~1/program/soffice.exe"
  22. ),
  23. path.join(
  24. process.env["PROGRAMFILES(X86)"],
  25. "LibreOffice/program/soffice.exe"
  26. ),
  27. path.join(
  28. process.env.PROGRAMFILES,
  29. "LibreOffice/program/soffice.exe"
  30. ),
  31. ];
  32. break;
  33. default:
  34. return callback(
  35. new Error(`Operating system not yet supported: ${process.platform}`)
  36. );
  37. }
  38. return async.filter(
  39. paths,
  40. (filePath, callback) =>
  41. fs.access(filePath, (err) => callback(null, !err)),
  42. (err, res) => {
  43. if (res.length === 0) {
  44. return callback(new Error("Could not find soffice binary"));
  45. }
  46. return callback(
  47. null,
  48. process.platform === "win32" ? `${res[0]}` : res[0]
  49. );
  50. }
  51. );
  52. }
  53. }
  54. function parseCommand(librePath, cmd, convert) {
  55. let _args = [];
  56. if (process.platform === "win32" && convert === "pdf") {
  57. _args.push("/c");
  58. _args.push(librePath);
  59. }
  60. _args = _args.concat(cmd);
  61. return { _args };
  62. }
  63. function run(librePath, cmd, convert) {
  64. return new Promise((resolve, reject) => {
  65. const { _args } = parseCommand(librePath, cmd, convert);
  66. let _cmd = null;
  67. if (convert === "img") {
  68. _cmd = "convert";
  69. } else if (process.platform === "win32" && convert === "pdf") {
  70. _cmd = process.env.ComSpec;
  71. } else {
  72. _cmd = librePath;
  73. }
  74. const proc = spawn(_cmd, _args);
  75. proc.stdout.on("data", (data) => {
  76. // console.log("stdout", data.toString());
  77. });
  78. proc.stderr.on("error", function (err) {
  79. reject(err);
  80. });
  81. proc.on("close", (code) => {
  82. const status = code === 0 ? "Success" : "Error";
  83. resolve(status);
  84. });
  85. });
  86. }
  87. exports.convert = (
  88. { libreofficeBin, sourceFile, outputDir, img, imgExt, reSize, density },
  89. callback
  90. ) => {
  91. libreOffice(libreofficeBin, (err, res) => {
  92. if (err) {
  93. return err;
  94. } else {
  95. const baseFileName = path.basename(sourceFile);
  96. const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");
  97. const outputImg = outputFile.replace(/\.pdf$/, `-%d.${imgExt}`);
  98. const ext = path.extname(sourceFile.toLowerCase());
  99. const extensions = [".pdf", ".pptx", ".ppt", ".odp", ".key"];
  100. const pdf = [
  101. "--headless",
  102. "--convert-to",
  103. "pdf",
  104. "--outdir",
  105. outputDir,
  106. sourceFile,
  107. ];
  108. const image = [
  109. "-verbose",
  110. "-resize",
  111. reSize || 1200,
  112. "-density",
  113. density || 120,
  114. `${outputDir}${outputFile}`,
  115. `${outputDir}${outputImg}`,
  116. ];
  117. const pdf2Img = [
  118. "-verbose",
  119. "-resize",
  120. reSize || 1200,
  121. "-density",
  122. density || 120,
  123. sourceFile,
  124. `${outputDir}${outputImg}`,
  125. ];
  126. if (ext === ".pdf")
  127. return run(res, pdf2Img, "img").then((res) => callback(null, res));
  128. fs.access(sourceFile, fs.constants.F_OK, (err) => {
  129. if (err) {
  130. return callback(new Error("Source File does not exist."));
  131. } else {
  132. if (extensions.includes(ext)) {
  133. run(res, pdf, "pdf").then((pdfRes) => {
  134. if (pdfRes !== "Error") {
  135. if (!img) {
  136. return callback(null, pdfRes);
  137. } else {
  138. run(res, image, "img").then((imageRes) => {
  139. if (imageRes !== "Error") {
  140. return callback(null, imageRes);
  141. } else {
  142. return callback(
  143. new Error("Error on image conversion process.")
  144. );
  145. }
  146. });
  147. }
  148. } else {
  149. return callback(new Error("Error on pdf conversion process."));
  150. }
  151. });
  152. } else {
  153. return callback(new Error("Extension does not support."));
  154. }
  155. }
  156. });
  157. }
  158. });
  159. };