MCPcopy Create free account
hub / github.com/LibPDF-js/core / main

Function main

examples/01-basic/load-encrypted.ts:13–58  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11import { formatBytes, loadFixture, saveOutput } from "../utils";
12
13async 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
60main().catch(console.error);

Callers 1

load-encrypted.tsFile · 0.70

Calls 9

loadFixtureFunction · 0.90
saveOutputFunction · 0.90
formatBytesFunction · 0.90
removeProtectionMethod · 0.80
saveMethod · 0.80
getPageCountMethod · 0.65
loadMethod · 0.45
hasOwnerAccessMethod · 0.45
getPageMethod · 0.45

Tested by

no test coverage detected