()
| 16 | import { formatBytes, loadFixture, saveOutput } from "../utils"; |
| 17 | |
| 18 | async function main() { |
| 19 | console.log("Demonstrating incremental saves...\n"); |
| 20 | |
| 21 | // Load a PDF |
| 22 | const bytes = await loadFixture("basic", "rot0.pdf"); |
| 23 | const pdf = await PDF.load(bytes); |
| 24 | |
| 25 | console.log("=== Original Document ==="); |
| 26 | console.log(`Page Count: ${pdf.getPageCount()}`); |
| 27 | console.log(`Original Size: ${formatBytes(bytes.length)}`); |
| 28 | console.log(`Can Save Incrementally: ${pdf.canSaveIncrementally()}`); |
| 29 | console.log(`Has Changes: ${pdf.hasChanges()}`); |
| 30 | |
| 31 | // Make a modification - update the document title |
| 32 | console.log("\n=== Making Changes ==="); |
| 33 | pdf.setTitle("Modified Document Title"); |
| 34 | pdf.setAuthor("Example Author"); |
| 35 | |
| 36 | console.log(`Has Changes: ${pdf.hasChanges()}`); |
| 37 | console.log(`Title: ${pdf.getTitle()}`); |
| 38 | console.log(`Author: ${pdf.getAuthor()}`); |
| 39 | |
| 40 | // Save with full rewrite (default) |
| 41 | console.log("\n=== Full Rewrite Save ==="); |
| 42 | const fullRewriteBytes = await pdf.save({ incremental: false }); |
| 43 | console.log(`Full rewrite size: ${formatBytes(fullRewriteBytes.length)}`); |
| 44 | |
| 45 | // Save with incremental update |
| 46 | console.log("\n=== Incremental Save ==="); |
| 47 | if (pdf.canSaveIncrementally()) { |
| 48 | const incrementalBytes = await pdf.save({ incremental: true }); |
| 49 | console.log(`Incremental size: ${formatBytes(incrementalBytes.length)}`); |
| 50 | |
| 51 | // Incremental saves are typically larger because they append |
| 52 | // the original content plus the new changes |
| 53 | console.log(`\nNote: Incremental saves append changes, so the file size includes`); |
| 54 | console.log(`the original document plus the modifications.`); |
| 55 | |
| 56 | // Save the incremental version |
| 57 | const outputPath = await saveOutput("01-basic/incremental-save.pdf", incrementalBytes); |
| 58 | console.log(`\nSaved to: ${outputPath}`); |
| 59 | |
| 60 | // Verify the incremental save worked |
| 61 | const verifyPdf = await PDF.load(incrementalBytes); |
| 62 | console.log(`\nVerification:`); |
| 63 | console.log(` Title: ${verifyPdf.getTitle()}`); |
| 64 | console.log(` Author: ${verifyPdf.getAuthor()}`); |
| 65 | console.log(` Page Count: ${verifyPdf.getPageCount()}`); |
| 66 | } else { |
| 67 | console.log("This document cannot be saved incrementally."); |
| 68 | console.log("(Newly created documents or those with structural changes may not support it)"); |
| 69 | } |
| 70 | |
| 71 | console.log("\n=== Done ==="); |
| 72 | } |
| 73 | |
| 74 | main().catch(console.error); |
no test coverage detected