Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

convert.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const fs = require("fs");
  2. const path = require("path");
  3. const async = require("async");
  4. const { spawn } = require("child_process");
  5. function parseCommand(librePath, cmd, convert) {
  6. let _args = [];
  7. if (process.platform === "win32" && convert === "pdf") {
  8. _args.push("/c");
  9. _args.push(librePath);
  10. }
  11. _args = _args.concat(cmd);
  12. return { _args };
  13. }
  14. function fileExist(file) {
  15. return new Promise((resolve, reject) => {
  16. fs.access(file, fs.constants.F_OK, (err) => {
  17. if (err && err.code === "ENOENT") {
  18. reject(new Error(`${file} does not exist`));
  19. } else {
  20. resolve(true);
  21. }
  22. });
  23. });
  24. }
  25. function run(libreOfficeBin, cmd, convert) {
  26. return new Promise((resolve, reject) => {
  27. const { _args } = parseCommand(libreOfficeBin, cmd, convert);
  28. let _cmd = libreOfficeBin;
  29. if (convert === "img") {
  30. _cmd = "convert";
  31. } else if (process.platform === "win32" && convert === "pdf") {
  32. _cmd = process.env.ComSpec;
  33. }
  34. const proc = spawn(_cmd, _args);
  35. proc.stdout.on("data", (data) => {
  36. // console.log("stdout", data.toString());
  37. });
  38. proc.stderr.on("error", function (err) {
  39. reject(err);
  40. });
  41. proc.on("close", (code) => {
  42. const status = code === 0 ? "Success" : "Error";
  43. resolve(status);
  44. });
  45. });
  46. }
  47. exports.convert = function ({
  48. libreofficeBin,
  49. sourceFile,
  50. outputDir,
  51. img,
  52. imgExt,
  53. reSize,
  54. density,
  55. }) {
  56. const baseFileName = path.basename(sourceFile);
  57. const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");
  58. const outputImg = outputFile.replace(/\.pdf$/, `-%d.${imgExt || "png"}`);
  59. const ext = path.extname(sourceFile.toLowerCase());
  60. const extensions = [".pdf", ".pptx", ".ppt", ".odp", ".key"];
  61. const pdf = [
  62. "--headless",
  63. "--convert-to",
  64. "pdf",
  65. "--outdir",
  66. outputDir,
  67. sourceFile,
  68. ];
  69. const image = [
  70. "-verbose",
  71. "-resize",
  72. reSize || 1200,
  73. "-density",
  74. density || 120,
  75. `${outputDir}${outputFile}`,
  76. `${outputDir}${outputImg}`,
  77. ];
  78. const pdf2Img = [
  79. "-verbose",
  80. "-resize",
  81. reSize || 1200,
  82. "-density",
  83. density || 120,
  84. sourceFile,
  85. `${outputDir}${outputImg}`,
  86. ];
  87. return fileExist(libreofficeBin).then((binExist) => {
  88. if (binExist) {
  89. return fileExist(sourceFile).then((srcExist) => {
  90. if (srcExist) {
  91. if (ext === ".pdf")
  92. return run(libreofficeBin, pdf2Img, "img").then((res) => res);
  93. if (extensions.includes(ext)) {
  94. return run(libreofficeBin, pdf, "pdf").then((pdfRes) => {
  95. if (pdfRes !== "Error") {
  96. if (!img) {
  97. return pdfRes;
  98. } else {
  99. return run(libreofficeBin, image, "img").then((imageRes) => {
  100. if (imageRes !== "Error") {
  101. return imageRes;
  102. } else {
  103. throw new Error("Error on image conversion process.");
  104. }
  105. });
  106. }
  107. } else {
  108. throw new Error("Error on pdf conversion process.");
  109. }
  110. });
  111. } else {
  112. throw new Error("Invalid extension.");
  113. }
  114. }
  115. });
  116. }
  117. });
  118. };