Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 3.2KB

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