From f3ebfa3a4e10d24875803bfc5e59cb3bd61c6e3c Mon Sep 17 00:00:00 2001 From: LCJ-MinYa <1049468118@qq.com> Date: Mon, 30 Sep 2024 17:58:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ppt=E8=BD=AC=E5=9B=BE=E7=89=87=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/index.js | 6 +++- package.json | 3 +- src/ppt_to_img.js | 71 ++++++++++++++++++++++++++++++++++++++++ src/product_cover_img.js | 6 ++-- 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/ppt_to_img.js diff --git a/config/index.js b/config/index.js index f574ed6..61522ec 100644 --- a/config/index.js +++ b/config/index.js @@ -16,7 +16,7 @@ const config = { // 裁剪商品封面图 cover: { // 支持处理的常见图片格式 - imgFormat: new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'svg']), + format: new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'svg']), // 封面图裁剪比例 scaleArr: [ { @@ -31,6 +31,10 @@ const config = { }, ], }, + ppt: { + // 支持处理的常见图片格式 + format: new Set(['ppt', 'pptx']), + }, }; module.exports = config; diff --git a/package.json b/package.json index a4be03d..5446457 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "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": "", "license": "ISC", diff --git a/src/ppt_to_img.js b/src/ppt_to_img.js new file mode 100644 index 0000000..87219d2 --- /dev/null +++ b/src/ppt_to_img.js @@ -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); + }); + } +}); diff --git a/src/product_cover_img.js b/src/product_cover_img.js index b3d3691..5787a39 100644 --- a/src/product_cover_img.js +++ b/src/product_cover_img.js @@ -4,11 +4,11 @@ const path = require('path'); const { getInputDir } = require('../utils/index'); const { cover } = require('../config/index'); -//图片文件夹路径 +//工作目录文件夹路径 let inputDir = getInputDir(); if (!inputDir) { - console.log('未执行裁剪主图,工作目录不存在'); + console.log('未执行裁剪主图功能,工作目录不存在'); return; } @@ -47,7 +47,7 @@ async function resizeImage(imageDir, outputDir, scale) { //读取输入目录下所有文件 fs.readdirSync(inputDir).forEach(async (file) => { const ext = file.split('.').pop().toLowerCase(); - if (cover.imgFormat.has(ext)) { + if (cover.format.has(ext)) { const inputPath = path.join(inputDir, file); cover.scaleArr.forEach((item) => { const outputPath = path.join(item.dir, file);