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

四、项目结构

五、使用方法

六、效果

总结

踩坑路漫漫长@~@

相关推荐
Dxy123931021617 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷17 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
椒哥19 小时前
一天被问几十次"允许",我统计了 517 次,然后写了个插件
前端·javascript·github
kyriewen20 小时前
我测了 6 种防请求竞态的写法:只有 1 种真能打
前端·javascript·程序员
OpsEye21 小时前
企业大模型压测,需要重点关注哪些指标?
javascript·ai编程
Hone21 小时前
我把「自动发文章到掘金」做成了一个 Agent 技能:juejin-skills
node.js·ai编程·前端工程化
像我这样帅的人丶你还21 小时前
我html生成的doc文档为何在手机文档显示不了图片?
前端·javascript
栉甜1 天前
Vue2学习内容(上)
前端·javascript·vue.js·学习
shmily麻瓜小菜鸡1 天前
TypeScript 知识点补充(第三部分:工程化与高级类型)
前端·javascript·typescript
Dumbness1 天前
二十行手写的 SKILL.md 头部解析,遇到 YAML 的折叠写法、单引号和嵌套键就读错:和 js-yaml 逐条对照
node.js·ai编程