nodejs小工具--pdf拆分

提示:pdf拆分

### 文章目录

  • [@TOC(文章目录)](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [前言](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [一、pdf-lib](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [二、pdf拆分功能](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [三、双击运行bat文件](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [四、项目结构](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [五、使用方法](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [六、效果](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)
  • [总结](#文章目录 @TOC 前言 一、pdf-lib 二、pdf拆分功能 三、双击运行bat文件 四、项目结构 五、使用方法 六、效果 总结)

前言

一、pdf-lib

pdf-lib

c 复制代码
npm install pdf-lib --save-dev

二、pdf拆分功能

index.js

c 复制代码
// 启动express服务
const express = require('express');
const app = express();

app.listen(3000,()=>{
    console.log('http://localhost:3000');
});

// 文件读写
const fs = require('fs');
// 路径
const path = require('path');
// pdf-lib
const pdfLib = require('pdf-lib');
// PDFDocument
const pdfDoc = pdfLib.PDFDocument;

// 拆分pdf文件
const splitPdf = async (pdfPath,name) => {
    // 文件bytes
    const bytes = await fs.promises.readFile(pdfPath);
    // pdf数据
    const pdfData = await pdfDoc.load(bytes)
    // pdf页数
    const pages = pdfData.getPages().length;
    for (let i = 0; i < pages; i++) {
        const doc = await pdfDoc.create();
        const [ page ] = await doc.copyPages(pdfData, [i])
        doc.addPage(page);
        const pdfBytes = await doc.save()
        await fs.promises.writeFile(`./output/${name||'pdf'}-${i + 1}.pdf`, pdfBytes);
    }
    console.log(`拆分为${pages}个pdf文件`)
}

// 扫描文件夹下的.pdf文件
const searchPdf =  (dir, callback) => {
    // 读取文件夹下数据
    fs.readdir(dir, (fileErr, files) => {
        if (fileErr)return console.error('Error:', fileErr);
        // 遍历文件夹下的文件
        files.forEach((file) => {
            // file路径
            let fullPath = path.join(dir, file);
            fs.stat(fullPath, async (err, state) => {
                if (err)return console.error('Error:', err);
                // 如果是文件夹,继续扫描读取到的文件夹
                if (state.isDirectory()) return searchPdf(fullPath);
                // 是否为.pdf文件
                if (path.extname(file).toLowerCase() == '.pdf') {
                    console.log('扫描到pdf文件,路径:', fullPath,file);
                    // 拆分pdf文件
                    let name = file.split('.pdf')[0];
                    await splitPdf(fullPath,name);
                    
                }
            });
        });
    });
}

searchPdf('./source');

三、双击运行bat文件

run.bat

c 复制代码
@echo off
cd /d "%~dp0"
node index.js

四、项目结构

五、使用方法

六、效果

总结

踩坑路漫漫长@~@

相关推荐
Amumu1213818 分钟前
Node学习: 网络基础概念 + HTTP模块
网络·node.js
sumitrmardy32 分钟前
写了一个邮箱验证 API 之后,我才知道的几个 SMTP 冷知识
node.js
zttbee2 小时前
npm install卡顿
前端·npm·node.js
西红柿炖牛腩3542 小时前
机房电脑不能联网,怎么离线把课件里的 3D 模型压小?Draco / Meshopt 参数清单
前端·javascript·3d·性能优化·大屏端
爱喝水的小周2 小时前
实验十三 Vue 过渡与动画—电商后台消息中心实现
前端·javascript·vue.js
无责任此方_修行中2 小时前
如何不用括号调用 JS 的函数?
前端·javascript·node.js
无责任此方_修行中2 小时前
Deno 的八年:一场没逃出 Node 引力的运行时起义
前端·javascript·node.js
开开心心就好3 小时前
白天晚上自动调蓝光,护眼工具亮度可调
服务器·开发语言·算法·pdf·ocr·intellij-idea·idea
asdzx673 小时前
C# 将 HTML 转换为 PDF:基于 Chrome 的转换方案
pdf·c#·html
用户298698530143 小时前
在 React 中使用 JavaScript 将 Markdown 转换为 PDF
javascript·react.js·markdown