Package Exports
- @pdfsmaller/pdf-encrypt-lite
- @pdfsmaller/pdf-encrypt-lite/dist/index.js
- @pdfsmaller/pdf-encrypt-lite/dist/index.mjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@pdfsmaller/pdf-encrypt-lite) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
pdf-encrypt-lite π
Ultra-lightweight PDF encryption library (only 7KB!) with real RC4 128-bit encryption
Built by PDFSmaller.com - Try our free online PDF tools with this encryption built-in!
π Why pdf-encrypt-lite?
When building PDFSmaller.com, we needed real PDF encryption that worked within Cloudflare Workers' 1MB limit. Every existing solution was 2-20MB+ in size. We were told it was "impossible" to implement proper PDF encryption in such a small package.
We proved them wrong.
This library is the exact encryption engine that powers PDFSmaller.com's Protect PDF tool - battle-tested on thousands of PDFs daily.
The Problem We Solved:
- β node-forge: 1.7MB minified
- β crypto-js: 234KB (still too large with pdf-lib)
- β Native crypto: Not available in many edge environments
- β pdf-encrypt-lite: Only 7KB! π
β¨ Features
- π Real PDF encryption - RC4 128-bit encryption that actually works
- π¦ Tiny size - Only ~7KB total (MD5 + RC4 implementations)
- β‘ Edge-ready - Works in Cloudflare Workers, Vercel Edge, Deno Deploy
- π Browser compatible - No Node.js dependencies
- π± Password protection - PDFs prompt for password in any reader
- π‘οΈ PDF Standard compliant - Implements Algorithm 2 & 3 from PDF spec
- π Zero dependencies - Just needs pdf-lib as peer dependency
π₯ Installation
npm install @pdfsmaller/pdf-encrypt-lite pdf-lib
π» Usage
import { encryptPDF } from '@pdfsmaller/pdf-encrypt-lite';
import { PDFDocument } from 'pdf-lib';
// Basic usage
const encryptedPdfBytes = await encryptPDF(existingPdfBytes, 'user-password');
// With separate owner password
const encryptedPdfBytes = await encryptPDF(
existingPdfBytes,
'user-password',
'owner-password'
);
// Full example
async function protectPDF() {
// Load your PDF
const existingPdfBytes = await fetch('document.pdf').then(res => res.arrayBuffer());
// Encrypt it with pdf-encrypt-lite
const encryptedBytes = await encryptPDF(
new Uint8Array(existingPdfBytes),
'secret123',
'owner456'
);
// Save the encrypted PDF
const blob = new Blob([encryptedBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'protected.pdf';
a.click();
}
π₯ Use Cases
Perfect for:
- Edge Functions (Cloudflare Workers, Vercel Edge, Netlify Edge)
- Browser applications (Like PDFSmaller.com)
- Serverless functions with size limits
- Client-side PDF protection without server uploads
- Lightweight Node.js applications
π― Real-World Example
See it in action at PDFSmaller.com/protect-pdf - our free online tool uses this exact library to encrypt PDFs directly in your browser. No uploads, no server processing, just pure client-side encryption!
ποΈ How It Works
We built custom implementations of:
- MD5 hashing - For password processing per PDF spec
- RC4 encryption - For content encryption
- PDF object traversal - Encrypts all strings and streams
- Standard Security Handler - Implements PDF encryption spec
Total size: ~7KB π€―
π Comparison
Library | Size | Real Encryption | Edge Compatible |
---|---|---|---|
pdf-encrypt-lite | 7KB β | β | β |
node-forge | 1,700KB | β | β |
crypto-js | 234KB | β | β οΈ |
pdf-lib alone | 0KB | β | β |
π€ Contributing
We welcome contributions! This library powers PDFSmaller.com, so we maintain high standards for security and compatibility.
π License
MIT License - Use it freely in your projects!
π Credits
Built with β€οΈ by PDFSmaller.com - Your free PDF toolkit
If this library helps you, check out our other free PDF tools:
- Compress PDF - Reduce PDF size by up to 90%
- Merge PDF - Combine multiple PDFs
- Split PDF - Extract pages from PDFs
- Protect PDF - Uses this library!
- 20+ more tools - All free, all private
π Quick Start for Cloudflare Workers
export default {
async fetch(request, env) {
const formData = await request.formData();
const file = formData.get('pdf');
const password = formData.get('password');
const pdfBytes = new Uint8Array(await file.arrayBuffer());
const encrypted = await encryptPDF(pdfBytes, password);
return new Response(encrypted, {
headers: { 'Content-Type': 'application/pdf' }
});
}
}
π§ Support
- π Report issues
- π‘ Request features
- π Visit PDFSmaller.com
- π§ Contact us
β Star this repo if it helps you!
Built because we needed it. Shared because you might too.
PDFSmaller.com - Free PDF Tools That Actually Workβ’