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

@moneyinto/node-sign-pdf

v1.0.1

Published

Node.js PDF签名库,用于调用Java JAR文件进行PDF数字签名

Readme

node-sign-pdf

Node.js PDF签名库,用于调用Java JAR文件进行PDF数字签名。

项目介绍

大文件签名@signpdf库会内存溢出问题,本项目采用Java实现PDF签名功能。

特性

  • 支持PDF文件数字签名
  • 支持自定义签名原因和位置
  • 简单易用的API
  • 基于Java实现,确保签名兼容性

安装步骤

  1. 克隆或下载项目到本地

  2. 安装依赖(如果需要)

    npm install
  3. 或通过npm安装(如果已发布到npm)

    npm install node-sign-pdf

使用示例

基本用法

// CommonJS 示例
const { signPdf } = require('node-sign-pdf');
const path = require('path');

async function main() {
  try {
    const inputPath = path.join(__dirname, 'test-unsigned.pdf');
    const outputPath = path.join(__dirname, 'test-signed.pdf');
    const keystorePath = path.join(__dirname, 'keystore.p12');
    
    console.log('开始签名PDF...');
    const result = await signPdf(inputPath, outputPath, keystorePath, 'your-password', 'your-user-password', 'your-owner-password');
    console.log('PDF签名成功:', result);
  } catch (error) {
    console.error('PDF签名失败:', error);
  }
}

main();

// ES Module 示例
import signPdf from 'node-sign-pdf';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function main() {
  try {
    const inputPath = path.join(__dirname, 'test-unsigned.pdf');
    const outputPath = path.join(__dirname, 'test-signed.pdf');
    const keystorePath = path.join(__dirname, 'keystore.p12');
    
    console.log('开始签名PDF...');
    const result = await signPdf(inputPath, outputPath, keystorePath, 'your-password', 'your-user-password', 'your-owner-password');
    console.log('PDF签名成功:', result);
  } catch (error) {
    console.error('PDF签名失败:', error);
  }
}

main();

测试示例

项目包含一个测试脚本,可用于验证签名功能:

npm test

API文档

signPdf(inputPath, outputPath, keystorePath, keystorePassword, pdfUserPassword, pdfOwnerPassword)

对PDF文件进行数字签名。

参数

  • inputPath (string): 输入PDF文件路径
  • outputPath (string): 输出PDF文件路径
  • keystorePath (string): 密钥库文件路径
  • keystorePassword (string): 密钥库密码
  • pdfUserPassword (string): PDF用户密码
  • pdfOwnerPassword (string): PDF所有者密码

返回值

  • Promise<string>: 返回输出文件路径

项目结构

node-sign-pdf/
├── index.js          # 主入口文件
├── package.json      # 项目配置文件
├── fat-pdf-signer.jar  # PDF签名Java JAR文件
├── example/    # 测试用未签名PDF文件和测试脚本
└── README.md         # 项目文档

依赖项

  • 核心依赖:

    • child_process (Node.js内置模块): 用于执行Java命令
    • path (Node.js内置模块): 用于处理文件路径
  • 开发依赖:

    • pdfkit (可选): 用于生成测试PDF文件

生成测试证书

如果需要测试签名功能,可以使用以下命令生成自签名证书:

使用OpenSSL

  1. 生成私钥:

    openssl genrsa -out private.key 2048
  2. 生成证书签名请求:

    openssl req -new -key private.key -out certificate.csr
  3. 生成自签名证书:

    openssl x509 -req -days 365 -in certificate.csr -signkey private.key -out certificate.crt
  4. 导出为PKCS#12格式:

    openssl pkcs12 -export -out keystore.p12 -inkey private.key -in certificate.crt

使用Java Keytool

keytool -genkeypair -alias myalias -keyalg RSA -keysize 2048 -keystore keystore.p12 -storetype PKCS12 -validity 365 -storepass your-password

注意事项

  1. 确保Java环境已正确安装并配置在系统PATH中
  2. 密钥库文件应妥善保管,避免泄露
  3. 自签名证书仅适用于测试环境,生产环境应使用受信任CA颁发的证书
  4. 签名操作会修改PDF文件,建议在签名前备份原始文件

故障排除

常见问题

  1. Java命令未找到

    • 确保Java已安装并添加到系统PATH
    • 检查java -version命令是否能正常执行
  2. 密钥库文件不存在

    • 确保提供了正确的密钥库文件路径
    • 检查密钥库文件是否存在且可读
  3. 密码错误

    • 确保提供了正确的密钥库密码
  4. 别名不存在

    • 确保提供了正确的密钥别名
    • 使用keytool -list -v -keystore keystore.p12 -storetype PKCS12 -storepass your-password查看可用别名