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

Skip any whitespace after the first object in linearized PDFs (issue 17665) #17666

Merged
merged 1 commit into from
Feb 13, 2024
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
9 changes: 8 additions & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,14 @@ class PDFDocument {
// Find the end of the first object.
stream.reset();
if (find(stream, ENDOBJ_SIGNATURE)) {
startXRef = stream.pos + 6 - stream.start;
stream.skip(6);

let ch = stream.peekByte();
while (isWhiteSpace(ch)) {
stream.pos++;
ch = stream.peekByte();
}
startXRef = stream.pos - stream.start;
}
} else {
// Find `startxref` by checking backwards from the end of the file.
Expand Down
3 changes: 3 additions & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ class WorkerMessageHandler {
.ensureXRef("trailer")
.then(trailer => trailer.get("Prev"));
});
handler.on("GetStartXRefPos", function (data) {
return pdfManager.ensureDoc("startXRef");
});
handler.on("GetAnnotArray", function (data) {
return pdfManager.getPage(data.pageIndex).then(function (page) {
return page.annotations.map(a => a.toString());
Expand Down
7 changes: 7 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ class PDFDocumentProxy {
Object.defineProperty(this, "getXRefPrevValue", {
value: () => this._transport.getXRefPrevValue(),
});
Object.defineProperty(this, "getStartXRefPos", {
value: () => this._transport.getStartXRefPos(),
});
Object.defineProperty(this, "getAnnotArray", {
value: pageIndex => this._transport.getAnnotArray(pageIndex),
});
Expand Down Expand Up @@ -2349,6 +2352,10 @@ class WorkerTransport {
value: () =>
this.messageHandler.sendWithPromise("GetXRefPrevValue", null),
});
Object.defineProperty(this, "getStartXRefPos", {
value: () =>
this.messageHandler.sendWithPromise("GetStartXRefPos", null),
});
Object.defineProperty(this, "getAnnotArray", {
value: pageIndex =>
this.messageHandler.sendWithPromise("GetAnnotArray", { pageIndex }),
Expand Down
12 changes: 12 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,18 @@ describe("api", function () {
await loadingTask.destroy();
});

it("checks the `startxref` position of a linearized pdf doc (issue 17665)", async function () {
const loadingTask = getDocument(buildGetDocumentParams("empty.pdf"));
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);

const pdfDocument = await loadingTask.promise;

const startXRefPos = await pdfDocument.getStartXRefPos();
expect(startXRefPos).toEqual(116);

await loadingTask.destroy();
});

it("checks that `docId`s are unique and increasing", async function () {
const loadingTask1 = getDocument(basicApiGetDocumentParams);
expect(loadingTask1 instanceof PDFDocumentLoadingTask).toEqual(true);
Expand Down