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

四、项目结构

五、使用方法

六、效果

总结

踩坑路漫漫长@~@

相关推荐
Never_Satisfied15 分钟前
在c#中,抛出异常,并指定其message的值
java·javascript·c#
Highcharts.js19 分钟前
什么是散点图?一文学会Highcharts散点图的核心特性与3D扩展应用
javascript·3d·开发文档·散点图·highcharts·图表类型
phltxy1 小时前
Vue Router:从入门到实战
前端·javascript·vue.js
Zhencode1 小时前
Vue3核心运行时之runtime-core
前端·javascript·vue.js
随逸1772 小时前
《吃透防抖与节流:从原理到实战,彻底解决高频事件性能问题》
javascript
千寻girling2 小时前
面试官 : “ 请说一下 JS 的常见的数组 和 字符串方法有哪些 ? ”
前端·javascript·面试
不会敲代码12 小时前
面试必考:如何优雅地将列表转换为树形结构?
javascript·算法·面试
用户5757303346242 小时前
深入理解 Promise:从单线程到异步流程控制的终极指南
javascript
我是伪码农2 小时前
Vue 大事件管理系统
前端·javascript·vue.js
无巧不成书02182 小时前
KMP适配鸿蒙开发实战|从0到1搭建可运行工程
javascript·华为·harmonyos·kmp