Skip to content
Open
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
2 changes: 1 addition & 1 deletion apps/web/test18.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Prev
</button>
<button onclick="test()">Run Test</button>
<button disabled onclick="window.location.href = '/apps/web/test19.html'">
<button onclick="window.location.href = '/apps/web/test19.html'">
Next
</button>
</div>
Expand Down
72 changes: 72 additions & 0 deletions apps/web/test19.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self' 'unsafe-inline' blob: resource: https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.js;
object-src 'self' blob:;
frame-src 'self' blob:;
"
/>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" type="text/css" href="/apps/web/index.css" />
<title>Test 19</title>
<script type="text/javascript" src="/dist/pdf-lib.js"></script>
<script type="text/javascript" src="/apps/web/utils.js"></script>
<script
type="text/javascript"
src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.js"
></script>
</head>

<body>
<div id="button-container">
<button onclick="window.location.href = '/apps/web/test18.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button disabled onclick="window.location.href = '/apps/web/test20.html'">
Next
</button>
</div>
<div id="animation-target"></div>
<iframe id="iframe"></iframe>
</body>

<script type="text/javascript">
startFpsTracker('animation-target');

const fetchAsset = (asset) =>
fetch(`/assets/${asset}`)
.then((res) => res.arrayBuffer())
.then((res) => new Uint8Array(res));

const renderInIframe = (pdfBytes) => {
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const blobUrl = URL.createObjectURL(blob);
document.getElementById('iframe').src = blobUrl;
};

// This test loads an existing PDF document with many pages.
// Internally, the file contains multiple updates which rendered
// incorrectly before fix of issue #951
async function test() {
const { PDFDocument, rgb } = PDFLib;

const inputPdfBytes = await Promise.all([
fetchAsset('pdfs/form_incremental_updates.pdf'),
]);

const pdfDoc = await PDFDocument.load(inputPdfBytes[0], {
updateMetadata: false,
});

const pdfBytes = await pdfDoc.save();

renderInIframe(pdfBytes);
}
</script>
</html>
Binary file added assets/pdfs/form_incremental_updates.pdf
Binary file not shown.
12 changes: 9 additions & 3 deletions src/core/PDFContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PDFContext {
ID?: PDFObject;
};
rng: SimpleRNG;
needsReordering: boolean;

private readonly indirectObjects: Map<PDFRef, PDFObject>;

Expand All @@ -64,6 +65,7 @@ class PDFContext {

private constructor() {
this.largestObjectNumber = 0;
this.needsReordering = false;
this.header = PDFHeader.forVersion(1, 7);
this.trailerInfo = {};

Expand All @@ -85,6 +87,7 @@ class PDFContext {

register(object: PDFObject): PDFRef {
const ref = this.nextRef();
this.needsReordering = true;
this.assign(ref, object);
return ref;
}
Expand Down Expand Up @@ -179,9 +182,12 @@ class PDFContext {
}

enumerateIndirectObjects(): [PDFRef, PDFObject][] {
return Array.from(this.indirectObjects.entries()).sort(
byAscendingObjectNumber,
);
const entries = Array.from(this.indirectObjects.entries());

if (this.needsReordering) {
return entries.sort(byAscendingObjectNumber);
}
return entries;
}

obj(literal: null | undefined): typeof PDFNull;
Expand Down