()
| 11 | import { formatBytes, loadFixture, saveOutput } from "../utils"; |
| 12 | |
| 13 | async function main() { |
| 14 | console.log("Loading encrypted PDF...\n"); |
| 15 | |
| 16 | // Load an encrypted PDF fixture |
| 17 | // PasswordSample-128bit.pdf uses: owner="owner", user="user" |
| 18 | const bytes = await loadFixture("encryption", "PasswordSample-128bit.pdf"); |
| 19 | |
| 20 | // Try loading without a password (will have limited access) |
| 21 | console.log("=== Attempting without password ==="); |
| 22 | const pdfNoPassword = await PDF.load(bytes); |
| 23 | console.log(`Encrypted: ${pdfNoPassword.isEncrypted}`); |
| 24 | console.log(`Authenticated: ${pdfNoPassword.isAuthenticated}`); |
| 25 | |
| 26 | // Load with the owner password for full access |
| 27 | console.log("\n=== Loading with owner password ==="); |
| 28 | const pdf = await PDF.load(bytes, { |
| 29 | credentials: "owner", |
| 30 | }); |
| 31 | |
| 32 | console.log(`Encrypted: ${pdf.isEncrypted}`); |
| 33 | console.log(`Authenticated: ${pdf.isAuthenticated}`); |
| 34 | console.log(`Has owner access: ${pdf.hasOwnerAccess()}`); |
| 35 | console.log(`Page Count: ${pdf.getPageCount()}`); |
| 36 | |
| 37 | // Show the page content is now accessible |
| 38 | const page = pdf.getPage(0); |
| 39 | if (page) { |
| 40 | console.log(`\nPage 1 Size: ${page.width} x ${page.height} points`); |
| 41 | } |
| 42 | |
| 43 | // Remove protection and save as unencrypted PDF |
| 44 | console.log("\nRemoving protection and saving..."); |
| 45 | pdf.removeProtection(); |
| 46 | const decryptedBytes = await pdf.save(); |
| 47 | |
| 48 | const outputPath = await saveOutput("01-basic/decrypted-document.pdf", decryptedBytes); |
| 49 | |
| 50 | console.log(`\n=== Saved Successfully ===`); |
| 51 | console.log(`Original size: ${formatBytes(bytes.length)}`); |
| 52 | console.log(`Decrypted size: ${formatBytes(decryptedBytes.length)}`); |
| 53 | console.log(`Output: ${outputPath}`); |
| 54 | |
| 55 | // Verify the saved PDF is not encrypted |
| 56 | const verifyPdf = await PDF.load(decryptedBytes); |
| 57 | console.log(`\nVerification - Is encrypted: ${verifyPdf.isEncrypted}`); |
| 58 | } |
| 59 | |
| 60 | main().catch(console.error); |
no test coverage detected