Quellcode durchsuchen

Completed document conversion to pdf or(and) image.

tags/v1.0.0
Kalimuthu Selvaraj vor 5 Jahren
Ursprung
Commit
a8fc28af73
8 geänderte Dateien mit 193 neuen und 0 gelöschten Zeilen
  1. 1
    0
      .gitignore
  2. 153
    0
      convert.js
  3. BIN
      file_example_ODP_1MB.odp
  4. BIN
      metro_style_powerpoint_Dark.pdf
  5. BIN
      metro_style_powerpoint_Dark.pptx
  6. 23
    0
      package.json
  7. BIN
      random_color.key
  8. 16
    0
      test/test.js

+ 1
- 0
.gitignore Datei anzeigen

@@ -0,0 +1 @@
node_modules

+ 153
- 0
convert.js Datei anzeigen

@@ -0,0 +1,153 @@
const fs = require("fs");
const path = require("path");
const async = require("async");
const { spawn } = require("child_process");

function libreOffice(libreofficeBin, callback) {
if (libreofficeBin) {
return callback(null, libreofficeBin);
} else {
let paths = [];
switch (process.platform) {
case "darwin":
paths = ["/Applications/LibreOffice.app/Contents/MacOS/soffice"];
break;
case "linux":
paths = ["/usr/bin/libreoffice", "/usr/bin/soffice"];
break;
case "win32":
paths = [
path.join(
process.env["PROGRAMFILES(X86)"],
"LIBREO~1/program/soffice.exe"
),
path.join(
process.env["PROGRAMFILES(X86)"],
"LibreOffice/program/soffice.exe"
),
path.join(
process.env.PROGRAMFILES,
"LibreOffice/program/soffice.exe"
),
];
break;
default:
return callback(
new Error(`Operating system not yet supported: ${process.platform}`)
);
}

return async.filter(
paths,
(filePath, callback) =>
fs.access(filePath, (err) => callback(null, !err)),
(err, res) => {
if (res.length === 0) {
return callback(new Error("Could not find soffice binary"));
}
return callback(
null,
process.platform === "win32" ? `${res[0]}` : res[0]
);
}
);
}
}

function parseCommand(librePath, cmd, convert) {
let _args = [];

if (process.platform === "win32" && convert === "pdf") {
_args.push("/c");
_args.push(librePath);
}

_args = _args.concat(cmd.split(" "));

return { _args };
}

function run(librePath, cmd, convert) {
return new Promise((resolve, reject) => {
const { _args } = parseCommand(librePath, cmd, convert);
let _cmd = null;

if (convert === "img") {
_cmd = "convert";
} else if (process.platform === "win32" && convert === "pdf") {
_cmd = process.env.ComSpec;
} else {
_cmd = librePath;
}
console.log(librePath);
console.log(_cmd);
console.log(_args);
const proc = spawn(_cmd, _args);

proc
.on("error", function (err) {
reject(err);
})
.on("close", (code) => {
const status = code === 0 ? "Success" : "Error";
resolve(status);
})
.stdout.on("data", (data) => {
// console.log(++i, data);
});
});
}

exports.convert = (
{ libreofficeBin, sourceFile, outputDir, img, imgExt, reSize, density },
callback
) => {
libreOffice(libreofficeBin, (err, res) => {
if (err) {
return err;
} else {
const baseFileName = path.basename(sourceFile);
const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");

const outputPath = `${outputDir}${outputFile}`;
const outputImg = outputFile.replace(/\.pdf$/, `-%d.${imgExt}`);

const ext = path.extname(sourceFile.toLowerCase());
const extensions = [".pdf", ".pptx", ".ppt", ".odp", ".key"];

const pdf = `--invisible --convert-to pdf --outdir ${outputDir} ${sourceFile}`;
const image = `-verbose -resize ${reSize || 1200} -density ${
density || 120
} ${outputPath} ${outputDir}${outputImg}`;

const pdf2Img = `-verbose -resize ${reSize || 1200} -density ${
density || 120
} ${sourceFile} ${outputDir}${outputImg}`;

if (ext === ".pdf")
return run(res, pdf2Img, "img").then((res) => callback(null, res));

fs.access(sourceFile, fs.constants.F_OK, (err) => {
if (err) {
return callback(new Error("Source File does not exist."));
} else {
if (extensions.includes(ext)) {
run(res, pdf, "pdf").then((pdfRes) => {
if (pdf) {
if (!img) {
return callback(null, pdfRes);
} else {
run(res, image, "img").then((imageRes) => {
return callback(null, imageRes);
});
}
}
});
} else {
return callback(new Error("Extension does not support."));
}
}
});
}
});
};

BIN
file_example_ODP_1MB.odp Datei anzeigen


BIN
metro_style_powerpoint_Dark.pdf Datei anzeigen


BIN
metro_style_powerpoint_Dark.pptx Datei anzeigen


+ 23
- 0
package.json Datei anzeigen

@@ -0,0 +1,23 @@
{
"name": "slide-pdf-image",
"version": "1.0.0",
"description": "The package export ppt, pptx, odp and key to pdf and/or img[png, jpg, jpeg].",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"ppt to pdf and/or image",
"pptx to pdf and/or image",
"keynote to pdf and/or image",
"odp to pdf and/or image",
"ppt convert to pdf",
"imagemagick",
"ghostscript"
],
"author": "Kalimuthu Selvaraj",
"license": "ISC",
"dependencies": {
"async": "^3.2.0"
}
}

BIN
random_color.key Datei anzeigen


+ 16
- 0
test/test.js Datei anzeigen

@@ -0,0 +1,16 @@
const document = require("../convert");

const options = {
libreofficeBin: "C:\\Program Files\\LibreOffice\\program\\soffice.exe",
sourceFile: "C:\\convert-pdf-img\\metro_style_powerpoint_Dark.pptx",
outputDir: "C:\\convert-pdf-img\\",
img: true,
imgExt: "jpg",
reSize: 800,
density: 120,
};

document.convert(options, (err, res) => {
if (err) console.log(err.message);
console.log(res);
});

Laden…
Abbrechen
Speichern