ソースを参照

Merge pull request #3 from vasilevich/master

New features
master
kalimuthu-selvaraj 4年前
コミット
9ede23401c
コミッターのメールアドレスに関連付けられたアカウントが存在しません
2個のファイルの変更57行の追加17行の削除
  1. 7
    0
      README.md
  2. 50
    17
      index.js

+ 7
- 0
README.md ファイルの表示

@@ -9,6 +9,13 @@ Converts `.ppt` `.pptx` `.odp` and `.key` file to `pdf or/and image(png, jpg, jp
Please install [libreoffice](https://www.libreoffice.org/), [imagemagick](https://www.imagemagick.org/script/index.php) and might required [ghostscript](https://www.ghostscript.com/) for Mac os
**(Note: Please do restart your machine after installed all required software.)**

###IMPORTANT
Make sure you install libreoffice to the USER you launch nodejs with and that this user HAS a home directory!
try to convert a file from inside that USER to properly test!
in case you install directly from libreoffice website,
MAKE SURE you use the ./install script that comes inside the tar.gz inside the USER!


## LibreOffice test

libreOfficeInstallationPath --headless --convert-to pdf --outdir outputDir sourceFile(test.pdf),

+ 50
- 17
index.js ファイルの表示

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

function parseCommand(librePath, cmd, convert) {
let _args = [];
@@ -12,14 +12,14 @@ function parseCommand(librePath, cmd, convert) {

_args = _args.concat(cmd);

return { _args };
return {_args};
}

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`));
resolve(false);
} else {
resolve(true);
}
@@ -27,9 +27,22 @@ function fileExist(file) {
});
}

const getFileThatExist = async (...files) => {
for (const file of files) {
if (file && await fileExist(file) === true) {
return file;
}
}
return false;
}

const filesExist = (...files) => {
return Promise.resolve(!!getFileThatExist(...files));
}

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

if (convert === "img") {
@@ -55,15 +68,17 @@ function run(libreOfficeBin, cmd, convert) {
});
}

exports.convert = function ({
libreofficeBin,
sourceFile,
outputDir,
img,
imgExt,
reSize,
density,
}) {
exports.convert = async ({
libreofficeBin,
libreofficeBins,
sourceFile,
outputDir,
img,
imgExt,
reSize,
density,
disableExtensionCheck
}) => {
const baseFileName = path.basename(sourceFile);
const outputFile = baseFileName.replace(/\.[^.]+$/, ".pdf");

@@ -100,15 +115,33 @@ exports.convert = function ({
sourceFile,
`${outputDir}${outputImg}`,
];
const libreOfficeBins = [
...(libreofficeBin ? [libreofficeBin] : []),
...(Array.isArray(libreofficeBins) ? libreofficeBins : [])

return fileExist(libreofficeBin).then((binExist) => {
if (binExist) {
return fileExist(sourceFile).then((srcExist) => {
];
if (!libreofficeBin) {
libreOfficeBins.push(path.resolve("/usr/bin/libreoffice"));
libreOfficeBins.push(path.resolve("/usr/bin/soffice"));
libreOfficeBins.push(path.resolve("/Applications/LibreOffice.app/Contents/MacOS/soffice"));
libreOfficeBins.push(path.resolve("C:\\\\Program Files\\\\LibreOffice\\\\program\\\\soffice.exe"));
libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LibreOffice\\\\program\\\\soffice.exe"));
libreOfficeBins.push(path.resolve("C:\\\\Program Files (x86)\\\\LIBREO~1\\\\program\\\\soffice.exe"));
}

return getFileThatExist(...libreOfficeBins).then((libreofficeBin) => {
if (libreofficeBin) {
//Re arrange the array for more efficient future runs
if (libreOfficeBins[0] !== libreofficeBin) {
libreOfficeBins.splice(libreOfficeBins.indexOf(libreofficeBin), 1);
libreOfficeBins.unshift(libreofficeBin);
}
return filesExist(sourceFile).then((srcExist) => {
if (srcExist) {
if (ext === ".pdf")
return run(libreofficeBin, pdf2Img, "img").then((res) => res);

if (extensions.includes(ext)) {
if (disableExtensionCheck || extensions.includes(ext)) {
return run(libreofficeBin, pdf, "pdf").then((pdfRes) => {
if (pdfRes !== "Error") {
if (!img) {

読み込み中…
キャンセル
保存