Переглянути джерело

Modified conversion logic totally.

tags/v1.0.0
Kalimuthu Selvaraj 5 роки тому
джерело
коміт
07368f8832
7 змінених файлів з 137 додано та 165 видалено
  1. 3
    1
      .gitignore
  2. 3
    0
      .travis.yml
  3. 83
    141
      convert.js
  4. 11
    3
      package.json
  5. BIN
      source/metro_powerpoint.pptx
  6. BIN
      source/random_color.key
  7. 37
    20
      test/test.js

+ 3
- 1
.gitignore Переглянути файл

@@ -1,3 +1,5 @@
node_modules
source
files
files
coverage
.nyc_output

+ 3
- 0
.travis.yml Переглянути файл

@@ -8,3 +8,6 @@ install:

script:
- npm test

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"

+ 83
- 141
convert.js Переглянути файл

@@ -3,59 +3,6 @@ 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 to convert process: ${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 = [];

@@ -69,17 +16,27 @@ function parseCommand(librePath, cmd, convert) {
return { _args };
}

function run(librePath, cmd, convert) {
function fileExist(file) {
return new Promise((resolve, reject) => {
fs.access(file, fs.constants.F_OK, (err) => {
if (err && err.code === "ENOENT") {
reject(new Error(`${file} does not exist`));
} else {
resolve(true);
}
});
});
}

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

if (convert === "img") {
_cmd = "convert";
} else if (process.platform === "win32" && convert === "pdf") {
_cmd = process.env.ComSpec;
} else {
_cmd = librePath;
}

const proc = spawn(_cmd, _args);
@@ -99,94 +56,79 @@ function run(librePath, cmd, convert) {
});
}

function pathExist(outputPath, callback) {
fs.access(outputPath, fs.constants.F_OK, (err) => {
if (err) {
return callback(new Error("Source file does not exist."));
}
});
}
exports.convert = function ({
libreofficeBin,
sourceFile,
outputDir,
img,
imgExt,
reSize,
density,
}) {
const baseFileName = path.basename(sourceFile);
const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");

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

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

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}`,
];

return fileExist(libreofficeBin).then((binExist) => {
if (binExist) {
return fileExist(sourceFile).then((srcExist) => {
if (srcExist) {
if (ext === ".pdf")
return run(libreofficeBin, pdf2Img, "img").then((res) => res);

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 outputImg = outputFile.replace(/\.pdf$/, `-%d.${imgExt || "png"}`);

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

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")
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 (pdfRes !== "Error") {
if (!img) {
return callback(null, pdfRes);
} else {
run(res, image, "img")
.then((imageRes) => {
if (imageRes !== "Error") {
return callback(null, imageRes);
} else {
return callback(
new Error("Error on image conversion process.")
);
}
})
.catch((e) => callback(e));
}
return run(libreofficeBin, pdf, "pdf").then((pdfRes) => {
if (pdfRes !== "Error") {
if (!img) {
return pdfRes;
} else {
return callback(
new Error("Error on pdf conversion process.")
);
return run(libreofficeBin, image, "img").then((imageRes) => {
if (imageRes !== "Error") {
return imageRes;
} else {
throw new Error("Error on image conversion process.");
}
});
}
})
.catch((e) => callback(e));
} else {
throw new Error("Error on pdf conversion process.");
}
});
} else {
return callback(new Error("Invalid extension."));
throw new Error("Invalid extension.");
}
}
});

+ 11
- 3
package.json Переглянути файл

@@ -4,7 +4,9 @@
"description": "The package export ppt, pptx, odp and key files to pdf and/or img(png, jpg, jpeg and etc...).",
"main": "index.js",
"scripts": {
"test": "mocha --reporter spec"
"test": "node_modules/.bin/mocha --reporter spec",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*",
"coverage": "nyc npm run test"
},
"keywords": [
"ppt to pdf and/or image",
@@ -24,10 +26,16 @@
},
"license": "ISC",
"dependencies": {
"async": "^3.2.0"
"async": "^3.2.0",
"chai-as-promised": "^7.1.1",
"nyc": "^15.1.0"
},
"devDependencies": {
"chai": "^4.2.0",
"babel-plugin-istanbul": "^6.0.0",
"chai": "^4.0.0-canary.2",
"coveralls": "^3.1.0",
"cross-env": "^7.0.2",
"istanbul": "^0.4.5",
"mocha": "^7.2.0"
},
"engines": {

BIN
source/metro_powerpoint.pptx Переглянути файл


BIN
source/random_color.key Переглянути файл


+ 37
- 20
test/test.js Переглянути файл

@@ -1,35 +1,52 @@
"use strict";
var expect = require("chai").expect;
var doc = require("../convert");
var chai = require("chai").use(require("chai-as-promised"));
var document = require("../convert");
var expect = chai.expect;
describe("Convert files to pdf or/and image", function () {
let options = {
sourceFile: "C:\\document-convert\\metro_powerpoint.pptx",
outputDir: "C:\\document-convert\\files\\",
const options = {
libreofficeBin: "C:\\Program Files\\LibreOffice\\program\\sooffice.exe",
sourceFile: "C:\\node\\document-convert\\source\\Metro_Style.pptx",
outputDir: "C:\\node\\document-convert\\files\\",
img: true,
imgExt: "jpg",
reSize: 800,
density: 120,
};
it("should return source file not exist", function () {
options.sourceFile = "C:\\document-convert\\source\\metro_powerpoint.pptex";
var result = doc.convert(options, function (err) {
expect(err.message).to.equal("Source file does not exist.");

it("Should return libre office bin does not exist", function (done) {
document.convert(options).catch((e) => {
expect(e.message).to.equal(`${options.libreofficeBin} does not exist`);
done();
});
});

it("should return invalid extesion", function () {
options.sourceFile = "C:\\document-convert\\source\\sample.txt";
var result = doc.convert(options, function (err) {
expect(err.message).to.equal("Invalid extension.");
options.libreofficeBin =
"C:\\Program Files\\LibreOffice\\program\\soffice.exe";
options.sourceFile = "C:\\node\\document-convert\\source\\sample.txt";
document.convert(options).catch((e) => {
expect(e.message).to.equal("Invalid extension.");
});
});

it("should convert pdf to image", function () {
options.sourceFile = "C:\\node\\document-convert\\source\\Metro_Style.pdf";
options.outputDir = "C:\\node\\document-convert\\files\\";
options.reSize = 800;
document.convert(options).then((res) => {
expect(res).to.equal("Success");
});
});

it("should convert pdf only", function (done) {
options.sourceFile = "C:\\node\\document-convert\\source\\Metro_Style.pptx";
options.img = false;
document.convert(options).then((res) => {
expect(res).to.equal("Success");
done();
});
});

it("should return success", function () {
options.sourceFile = "C:\\document-convert\\source\\metro_powerpoint.pptx";
options.outputDir = "C:\\document-convert\\files\\";
it("should convert without resize, density and imgExt", function () {
options.img = true;
var result = doc.convert(options, function (err, res) {
document.convert(options).then((res) => {
expect(res).to.equal("Success");
});
});

Завантаження…
Відмінити
Зберегти