Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pdf-repair example and fix cross-reference stream index count #106

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/structure/CrossReferenceStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,16 @@ export class CrossReferenceStream extends PDFStream implements CrossReference {

const indexes = this.Index || [{ start: 0, size: this.Size }];

// Some documents may have more indexes in the cross-reference stream than represented in the Index,
// which can lead to errors. To avoid this, we pre-determine the number of objects (totalCount) and
// exit the loop if we reach this count.
let totalCount = 0;
for (const index of indexes) {
totalCount += index.size;
}

let count = 0;
while (!streamDataReader.isEOF) {
while (!streamDataReader.isEOF && count < totalCount) {
const field1 = num(streamDataReader.read(w[0]), 1);
const field2 = num(streamDataReader.read(w[1]));
const field3 = num(streamDataReader.read(w[2]));
Expand Down
5 changes: 3 additions & 2 deletions packages/repair/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install @peculiarventures/pdf-repair

## Usage

Example usage of `@peculiarventures/pdf-repair``:
Example usage of `@peculiarventures/pdf-repair`:

```javascript
const fs = require('fs');
Expand All @@ -28,7 +28,8 @@ const data = fs.readFileSync("file.pdf");
const doc = PDFDocument.load(data);

const repair = new PDFRepair();
repair.repairDocument(doc);
const repairNotes = await repair.repairDocument(doc);
console.log(repairNotes);

const raw = doc.save();
fs.writeFileSync("newFile.pdf", raw, { flag: "w+" });
Expand Down
Loading