npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pdfclean

v0.0.1

Published

Command-line tool to remove watermarks from PDF files using Node.js

Downloads

19

Readme

PDFClean - PDF 水印移除工具

一个基于 Node.js 的命令行工具,用于移除 PDF 文件中的浅色水印。

功能特点

  • ✅ 自动检测并移除浅色水印
  • ✅ 多线程并行处理,充分利用 CPU 核心
  • ✅ 2倍分辨率处理,确保小字体水印精确识别
  • ✅ 友好的命令行界面,带进度条显示
  • ✅ 支持多页 PDF 批量处理
  • ✅ 容错机制,单页失败不影响整体输出

安装依赖

前置要求

  • Node.js >= 18.12.0
  • 对于 macOS: 需要 Xcode Command Line Tools
  • 对于 Linux: 需要 Cairo 库和相关开发包

安装步骤

# 克隆或进入项目目录
cd pdf_img

# 安装依赖
npm install

# 将 CLI 工具链接到全局
npm link

依赖说明

项目使用以下核心依赖:

  • @napi-rs/canvas (^0.1.97): PDF 页面渲染和像素级操作
  • jspdf (^3.0.4): PDF 生成和合并
  • pdfjs-dist (^4.0.379): PDF 解析和内容提取
  • yargs (^17.7.2): 命令行参数解析
  • cli-progress (^3.12.0): 进度条显示

使用方法

基础用法

# 最简单的用法,自动生成输出文件名
pdfclean input.pdf

# 指定输出文件
pdfclean input.pdf -o output.pdf

# 指定 Worker 线程数和渲染缩放
pdfclean input.pdf -w 4 -s 3

# 禁用进度条
pdfclean input.pdf --no-progress

命令行参数

Usage: pdfclean <input> [options]

参数:
  input                输入 PDF 文件路径                        [必需]

选项:
  -o, --output         输出 PDF 文件路径         [默认: input_clean.pdf]
  -w, --workers        最大 Worker 线程数        [默认: CPU 核心数]
  -S, --scale          渲染缩放因子 (越高质量越好) [默认: 2]
  --no-progress        禁用进度条                [默认: false]
  -h, --help           显示帮助信息
  -v, --version        显示版本号

示例:
  pdfclean input.pdf                        移除 input.pdf 的水印
  pdfclean input.pdf -o output.pdf          指定输出文件
  pdfclean input.pdf -w 4 -s 3              使用 4 个 Worker,3 倍缩放

程序化使用

const { removePdfWatermark } = require('./src/pdfWatermarkRemover');

// 基本用法
await removePdfWatermark('input.pdf', 'output.pdf');

// 带选项
await removePdfWatermark('input.pdf', 'output.pdf', {
  maxWorkers: 4,        // Worker 线程数
  scale: 2,             // 渲染缩放
  onProgress: (current, total) => {
    console.log(`处理进度: ${current}/${total}`);
  }
});

工作原理

水印检测算法

系统使用双重判定机制检测浅色水印:

  1. RGB 范围检测: 像素的 R、G、B 值都在 160-255 范围内
  2. 饱和度检测: max(R,G,B) - min(R,G,B) ≤ 10 (接近灰色)

满足以上两个条件的像素会被识别为水印并替换为纯白色 (255, 255, 255)。

处理流程

输入PDF → PDF解析 → 多线程并行处理 → 像素级水印检测 →
生成PNG图片 → PDF重建 → 合并所有页面 → 输出PDF

性能特点

  • 并行处理: 使用 Worker 线程池,充分利用多核 CPU
  • 2倍分辨率: 提高水印检测准确率
  • 内存优化: 流式处理,减少内存占用

项目结构

pdf_img/
├── bin/
│   └── cli.js              # CLI 入口
├── src/
│   ├── index.js            # 主入口
│   ├── pdfWatermarkRemover.js  # 主控制器
│   ├── watermarkProcessor.js   # 水印处理核心
│   ├── worker.js           # Worker 线程脚本
│   └── workerPool.js       # Worker 线程池管理
├── package.json
└── README.md

常见问题

Canvas 安装失败

如果 canvas 依赖安装失败:

macOS:

xcode-select --install
npm install canvas

Ubuntu/Debian:

sudo apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
npm install canvas

Windows: 建议使用预编译版本或 WSL2

内存不足

处理大型 PDF 时可能遇到内存问题,可以尝试:

  1. 减少 Worker 数量: pdfclean input.pdf -w 2
  2. 降低渲染缩放: pdfclean input.pdf -s 1.5

部分页面处理失败

工具具有容错机制,即使部分页面失败也会输出可处理的页面。检查控制台输出查看失败原因。

技术栈

  • Node.js 18.12.0+
  • Canvas - PDF 渲染和像素操作
  • jsPDF - PDF 生成
  • PDF.js - PDF 解析
  • Worker Threads - 多线程处理

License

MIT

作者

开发者: 根据技术方案实现

更新日志

v1.0.0 (2024)

  • 初始版本发布
  • 实现基础水印移除功能
  • 支持多线程并行处理
  • 命令行工具完整实现