You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { getInputDir } = require('../utils/index');
const { base, ppt } = require('../config/index');
//工作目录文件夹路径
const inputDir = getInputDir();
if (!inputDir) {
console.log('未执行ppt转图片功能工作目录不存在');
return;
}
//创建pdf输出文件夹
const pdfDir = `${inputDir}/pdf`;
if (!fs.existsSync(pdfDir)) {
fs.mkdirSync(pdfDir);
}
//创建pdf转图片的输出文件夹
const imgDir = `${inputDir}/img`;
if (!fs.existsSync(imgDir)) {
fs.mkdirSync(imgDir);
}
// 使用 LibreOffice 将 PPT 转换为 PDF
const convertPptToPdf = (inputDir) => {
const pdfCmd = `soffice --headless --invisible --convert-to pdf --outdir "${pdfDir}" "${inputDir}" 2>NUL`;
console.log(pdfCmd);
execSync(pdfCmd);
};
// 使用 ImageMagick 将 PDF 转换为图片
const convertPdfToImg = (inputDir, file) => {
const imgItemDir = path.join(imgDir, `${file.split('.')[0]}`);
if (!fs.existsSync(imgItemDir)) {
fs.mkdirSync(imgItemDir);
}
const imageOutputPath = path.join(imgItemDir, 'slide_%d.png');
const imgCmd = `magick -density 72 "${inputDir}" "${imageOutputPath}"`;
console.log(imgCmd);
execSync(imgCmd);
};
// 读取输入目录下所有ppt文件 => 转换为pdf
let execNum = 0;
fs.readdirSync(inputDir).forEach((file) => {
const ext = file.split('.').pop().toLowerCase();
if (ppt.pptFormat.has(ext)) {
execNum++;
if (base.rangeNum && execNum > base.rangeNum) {
return;
}
const inputPath = path.join(inputDir, file);
convertPptToPdf(inputPath);
}
});
// 读取输入目录下所有pdf文件下所有pdf文件 => 转换为image
fs.readdirSync(pdfDir).forEach((file) => {
const ext = file.split('.').pop().toLowerCase();
if (ppt.pdfFormat.has(ext)) {
const inputPath = path.join(pdfDir, file);
convertPdfToImg(inputPath, file);
}
});