feat: ppt转图片功能

main
LCJ-MinYa 1 year ago
parent b5e7b83f22
commit f3ebfa3a4e

@ -16,7 +16,7 @@ const config = {
// 裁剪商品封面图 // 裁剪商品封面图
cover: { cover: {
// 支持处理的常见图片格式 // 支持处理的常见图片格式
imgFormat: new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'svg']), format: new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'svg']),
// 封面图裁剪比例 // 封面图裁剪比例
scaleArr: [ scaleArr: [
{ {
@ -31,6 +31,10 @@ const config = {
}, },
], ],
}, },
ppt: {
// 支持处理的常见图片格式
format: new Set(['ppt', 'pptx']),
},
}; };
module.exports = config; module.exports = config;

@ -5,7 +5,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"cover": "node ./src/product_cover_img.js" "cover": "node ./src/product_cover_img.js",
"ppt": "node ./src/ppt_to_img.js"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

@ -0,0 +1,71 @@
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const { getInputDir } = require('../utils/index');
const { ppt } = require('../config/index');
//工作目录文件夹路径
const inputDir = getInputDir(); // 输入PPT文件路径
if (!inputDir) {
console.log('未执行ppt转图片功能工作目录不存在');
return;
}
//创建pdf输出文件夹
const pdfDir = `${inputDir}/pdf`;
console.log(pdfDir);
if (!fs.existsSync(pdfDir)) {
fs.mkdirSync(pdfDir);
}
//创建pdf转图片的输出文件夹
const imgDir = `${inputDir}/img`;
if (!fs.existsSync(imgDir)) {
fs.mkdirSync(imgDir);
}
const convertPptToImages = (inputDir, outputDir) => {
// 使用 LibreOffice 将 PPT 转换为 PDF
// const pdfFilePath = path.join(outputDir, '9.pdf');
// console.log(pdfFilePath);
const convertCommand = `soffice --headless --invisible --convert-to pdf --outdir "${outputDir}" "${inputDir}"`;
return new Promise((resolve, reject) => {
exec(convertCommand, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
// // 使用 ImageMagick 将 PDF 转换为图片
// const imageOutputPath = path.join(outputDir, 'slide_%d.png');
// const convertImageCommand = `convert -density 300 "${pdfFilePath}" "${imageOutputPath}"`;
// exec(convertImageCommand, (error) => {
// if (error) {
// console.error(`转换为图片失败: ${error}`);
// return;
// }
// console.log('转换成功!输出目录:', outputDir);
// });
});
});
};
//读取输入目录下所有文件
fs.readdirSync(inputDir).forEach(async (file) => {
const ext = file.split('.').pop().toLowerCase();
if (ppt.format.has(ext)) {
const inputPath = path.join(inputDir, file);
const outputPath = path.join(pdfDir, file);
convertPptToImages(inputPath, outputPath)
.then(() => {
console.log(`${file}的图片已生成`);
})
.catch((err) => {
console.error(`${file}的图片生成失败`, err);
});
}
});

@ -4,11 +4,11 @@ const path = require('path');
const { getInputDir } = require('../utils/index'); const { getInputDir } = require('../utils/index');
const { cover } = require('../config/index'); const { cover } = require('../config/index');
//图片文件夹路径 //工作目录文件夹路径
let inputDir = getInputDir(); let inputDir = getInputDir();
if (!inputDir) { if (!inputDir) {
console.log('未执行裁剪主图,工作目录不存在'); console.log('未执行裁剪主图功能,工作目录不存在');
return; return;
} }
@ -47,7 +47,7 @@ async function resizeImage(imageDir, outputDir, scale) {
//读取输入目录下所有文件 //读取输入目录下所有文件
fs.readdirSync(inputDir).forEach(async (file) => { fs.readdirSync(inputDir).forEach(async (file) => {
const ext = file.split('.').pop().toLowerCase(); const ext = file.split('.').pop().toLowerCase();
if (cover.imgFormat.has(ext)) { if (cover.format.has(ext)) {
const inputPath = path.join(inputDir, file); const inputPath = path.join(inputDir, file);
cover.scaleArr.forEach((item) => { cover.scaleArr.forEach((item) => {
const outputPath = path.join(item.dir, file); const outputPath = path.join(item.dir, file);

Loading…
Cancel
Save