Quellcode durchsuchen

Optimized the code.

tags/v1.0.0
Kalimuthu Selvaraj vor 5 Jahren
Ursprung
Commit
e9a76736d5
4 geänderte Dateien mit 53 neuen und 27 gelöschten Zeilen
  1. 52
    26
      convert.js
  2. 0
    0
      metro_powerpoint.pptx
  3. BIN
      metro_style_powerpoint_Dark.pdf
  4. 1
    1
      sample.js

+ 52
- 26
convert.js Datei anzeigen

_args.push(librePath); _args.push(librePath);
} }


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


return { _args }; return { _args };
} }
} else { } else {
_cmd = librePath; _cmd = librePath;
} }
console.log(librePath);
console.log(_cmd);
console.log(_args);

const proc = spawn(_cmd, _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);
});
proc.stdout.on("data", (data) => {
// console.log("stdout", data.toString());
});

proc.stderr.on("error", function (err) {
reject(err);
});

proc.on("close", (code) => {
const status = code === 0 ? "Success" : "Error";
resolve(status);
});
}); });
} }


const baseFileName = path.basename(sourceFile); const baseFileName = path.basename(sourceFile);
const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf"); const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");


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


const ext = path.extname(sourceFile.toLowerCase()); const ext = path.extname(sourceFile.toLowerCase());
const extensions = [".pdf", ".pptx", ".ppt", ".odp", ".key"]; 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}`;
const pdf = [
"--headless",
"--convert-to",
"pdf",
"--outdir",
outputDir,
sourceFile,
];

const image = [
"-verbose",
"-resize",
reSize || 1200,
"-density",
density || 120,
`${outputDir}${outputFile}`,
`${outputDir}${outputImg}`,
];

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


if (ext === ".pdf") if (ext === ".pdf")
return run(res, pdf2Img, "img").then((res) => callback(null, res)); return run(res, pdf2Img, "img").then((res) => callback(null, res));
} else { } else {
if (extensions.includes(ext)) { if (extensions.includes(ext)) {
run(res, pdf, "pdf").then((pdfRes) => { run(res, pdf, "pdf").then((pdfRes) => {
if (pdf) {
if (pdfRes !== "Error") {
if (!img) { if (!img) {
return callback(null, pdfRes); return callback(null, pdfRes);
} else { } else {
run(res, image, "img").then((imageRes) => { run(res, image, "img").then((imageRes) => {
return callback(null, imageRes);
if (imageRes !== "Error") {
return callback(null, imageRes);
} else {
return callback(
new Error("Error on image conversion process.")
);
}
}); });
} }
} else {
return callback(new Error("Error on pdf conversion process."));
} }
}); });
} else { } else {

metro_style_powerpoint_Dark.pptx → metro_powerpoint.pptx Datei anzeigen


BIN
metro_style_powerpoint_Dark.pdf Datei anzeigen


test/test.js → sample.js Datei anzeigen



const options = { const options = {
libreofficeBin: "C:\\Program Files\\LibreOffice\\program\\soffice.exe", libreofficeBin: "C:\\Program Files\\LibreOffice\\program\\soffice.exe",
sourceFile: "C:\\convert-pdf-img\\metro_style_powerpoint_Dark.pptx",
sourceFile: "C:\\convert-pdf-img\\metro_powerpoint.pptx",
outputDir: "C:\\convert-pdf-img\\files\\", outputDir: "C:\\convert-pdf-img\\files\\",
img: true, img: true,
imgExt: "jpg", imgExt: "jpg",

Laden…
Abbrechen
Speichern