Skip to content

Commit 2f36ae3

Browse files
authored
Merge pull request #1036 from Hopding/1033-cleanup
Deterministic key generation using pseudo randomness (tweaks)
2 parents 07bf247 + 60b3498 commit 2f36ae3

File tree

13 files changed

+79
-60
lines changed

13 files changed

+79
-60
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,15 @@ const secondDonorPdfBytes = ...
493493
const firstDonorPdfDoc = await PDFDocument.load(firstDonorPdfBytes)
494494
const secondDonorPdfDoc = await PDFDocument.load(secondDonorPdfBytes)
495495

496-
// Copy the 1st page from the first donor document, and
496+
// Copy the 1st page from the first donor document, and
497497
// the 743rd page from the second donor document
498498
const [firstDonorPage] = await pdfDoc.copyPages(firstDonorPdfDoc, [0])
499499
const [secondDonorPage] = await pdfDoc.copyPages(secondDonorPdfDoc, [742])
500500

501501
// Add the first copied page
502502
pdfDoc.addPage(firstDonorPage)
503503

504-
// Insert the second copied page to index 0, so it will be the
504+
// Insert the second copied page to index 0, so it will be the
505505
// first page in `pdfDoc`
506506
pdfDoc.insertPage(0, secondDonorPage)
507507

@@ -606,11 +606,11 @@ const preamble = await pdfDoc.embedPage(usConstitutionPdf.getPages()[1], {
606606
top: 575,
607607
})
608608

609-
// Get the width/height of the American flag PDF scaled down to 30% of
609+
// Get the width/height of the American flag PDF scaled down to 30% of
610610
// its original size
611611
const americanFlagDims = americanFlag.scale(0.3)
612612

613-
// Get the width/height of the preamble clipping scaled up to 225% of
613+
// Get the width/height of the preamble clipping scaled up to 225% of
614614
// its original size
615615
const preambleDims = preamble.scale(2.25)
616616

@@ -813,8 +813,8 @@ import { PDFDocument } from 'pdf-lib'
813813
const existingPdfBytes = ...
814814

815815
// Load a PDFDocument without updating its existing metadata
816-
const pdfDoc = await PDFDocument.load(existingPdfBytes, {
817-
updateMetadata: false
816+
const pdfDoc = await PDFDocument.load(existingPdfBytes, {
817+
updateMetadata: false
818818
})
819819

820820
// Print all available metadata fields
@@ -1228,7 +1228,7 @@ When working with PDFs, you will frequently come across the terms "character enc
12281228
const pdfDoc = await PDFDocument.create()
12291229
const courierFont = await pdfDoc.embedFont(StandardFonts.Courier)
12301230
const page = pdfDoc.addPage()
1231-
page.drawText('Some boring latin text in the Courier font', {
1231+
page.drawText('Some boring latin text in the Courier font', {
12321232
font: courierFont,
12331233
})
12341234
```
@@ -1248,7 +1248,7 @@ When working with PDFs, you will frequently come across the terms "character enc
12481248
const ubuntuFont = await pdfDoc.embedFont(fontBytes)
12491249

12501250
const page = pdfDoc.addPage()
1251-
page.drawText('Some fancy Unicode text in the ŪЬȕǹƚü font', {
1251+
page.drawText('Some fancy Unicode text in the ŪЬȕǹƚü font', {
12521252
font: ubuntuFont,
12531253
})
12541254
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"DkDavid (https://github.com/DkDavid)",
2929
"Bj Tecu (https://github.com/btecu)",
3030
"Brent McSharry (https://github.com/mcshaz)",
31-
"Tim Knapp (https://github.com/duffyd)"
31+
"Tim Knapp (https://github.com/duffyd)",
32+
"Ching Chang (https://github.com/ChingChang9)"
3233
],
3334
"scripts": {
3435
"release:latest": "yarn publish --tag latest && yarn pack && yarn release:tag",

src/api/PDFPage.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import {
4444
PDFArray,
4545
} from 'src/core';
4646
import {
47-
addRandomSuffix,
4847
assertEachIs,
4948
assertIs,
5049
assertMultiple,
@@ -700,7 +699,7 @@ export default class PDFPage {
700699
// TODO: Reuse image Font name if we've already added this image to Resources.Fonts
701700
assertIs(font, 'font', [[PDFFont, 'PDFFont']]);
702701
this.font = font;
703-
this.fontKey = addRandomSuffix(this.font.name);
702+
this.fontKey = this.doc.context.addRandomSuffix(this.font.name);
704703
this.node.setFontDictionary(PDFName.of(this.fontKey), this.font.ref);
705704
}
706705

@@ -1060,7 +1059,7 @@ export default class PDFPage {
10601059
assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
10611060
assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
10621061

1063-
const xObjectKey = addRandomSuffix('Image', 10);
1062+
const xObjectKey = this.doc.context.addRandomSuffix('Image', 10);
10641063
this.node.setXObject(PDFName.of(xObjectKey), image.ref);
10651064

10661065
const graphicsStateKey = this.maybeEmbedGraphicsState({
@@ -1135,7 +1134,7 @@ export default class PDFPage {
11351134
assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
11361135
assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
11371136

1138-
const xObjectKey = addRandomSuffix('EmbeddedPdfPage', 10);
1137+
const xObjectKey = this.doc.context.addRandomSuffix('EmbeddedPdfPage', 10);
11391138
this.node.setXObject(PDFName.of(xObjectKey), embeddedPage.ref);
11401139

11411140
const graphicsStateKey = this.maybeEmbedGraphicsState({
@@ -1592,7 +1591,7 @@ export default class PDFPage {
15921591
return undefined;
15931592
}
15941593

1595-
const key = addRandomSuffix('GS', 10);
1594+
const key = this.doc.context.addRandomSuffix('GS', 10);
15961595

15971596
const graphicsState = this.doc.context.obj({
15981597
Type: 'ExtGState',

src/api/form/PDFField.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ import {
2222
PDFAcroTerminal,
2323
AnnotationFlags,
2424
} from 'src/core';
25-
import {
26-
addRandomSuffix,
27-
assertIs,
28-
assertMultiple,
29-
assertOrUndefined,
30-
} from 'src/utils';
25+
import { assertIs, assertMultiple, assertOrUndefined } from 'src/utils';
3126
import { ImageAlignment } from '../image';
3227
import PDFImage from '../PDFImage';
3328
import { drawImage, rotateInPlace } from '../operations';
@@ -321,9 +316,7 @@ export default class PDFField {
321316
);
322317
widget.setRectangle(rect);
323318

324-
if(typeof pageRef !== 'undefined'){
325-
widget.setP(pageRef);
326-
}
319+
if (pageRef) widget.setP(pageRef);
327320

328321
const ac = widget.getOrCreateAppearanceCharacteristics();
329322
if (backgroundColor) {
@@ -495,7 +488,7 @@ export default class PDFField {
495488
options.y = adj.height - borderWidth - imageDims.height;
496489
}
497490

498-
const imageName = addRandomSuffix('Image', 10);
491+
const imageName = this.doc.context.addRandomSuffix('Image', 10);
499492
const appearance = [...rotate, ...drawImage(imageName, options)];
500493
////////////
501494

src/api/form/PDFForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
PDFName,
4242
PDFWidgetAnnotation,
4343
} from 'src/core';
44-
import { addRandomSuffix, assertIs, Cache, assertOrUndefined } from 'src/utils';
44+
import { assertIs, Cache, assertOrUndefined } from 'src/utils';
4545

4646
export interface FlattenOptions {
4747
updateFieldAppearances: boolean;
@@ -550,7 +550,7 @@ export default class PDFForm {
550550
const page = this.findWidgetPage(widget);
551551
const widgetRef = this.findWidgetAppearanceRef(field, widget);
552552

553-
const xObjectKey = addRandomSuffix('FlatWidget', 10);
553+
const xObjectKey = this.doc.context.addRandomSuffix('FlatWidget', 10);
554554
page.node.setXObject(PDFName.of(xObjectKey), widgetRef);
555555

556556
const rectangle = widget.getRectangle();

src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from 'src/api/form';
22
export * from 'src/api/text';
33
export * from 'src/api/colors';
44
export * from 'src/api/errors';
5-
export * from "src/api/image";
5+
export * from 'src/api/image';
66
export * from 'src/api/objects';
77
export * from 'src/api/operations';
88
export * from 'src/api/operators';

src/api/operations.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,19 @@ export const rotateInPlace = (options: {
443443
rotation: 0 | 90 | 180 | 270;
444444
}) =>
445445
options.rotation === 0 ? [
446-
translate(0, 0),
447-
rotateDegrees(0)
446+
translate(0, 0),
447+
rotateDegrees(0)
448448
]
449449
: options.rotation === 90 ? [
450-
translate(options.width, 0),
450+
translate(options.width, 0),
451451
rotateDegrees(90)
452452
]
453453
: options.rotation === 180 ? [
454-
translate(options.width, options.height),
454+
translate(options.width, options.height),
455455
rotateDegrees(180)
456456
]
457457
: options.rotation === 270 ? [
458-
translate(0, options.height),
458+
translate(0, options.height),
459459
rotateDegrees(270)
460460
]
461461
: []; // Invalid rotation - noop

src/core/PDFContext.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import PDFOperator from 'src/core/operators/PDFOperator';
1818
import Ops from 'src/core/operators/PDFOperatorNames';
1919
import PDFContentStream from 'src/core/structures/PDFContentStream';
2020
import { typedArrayFor } from 'src/utils';
21+
import { SimpleRNG } from 'src/utils/rng';
2122

2223
type LookupKey = PDFRef | PDFObject | undefined;
2324

@@ -54,6 +55,7 @@ class PDFContext {
5455
Info?: PDFObject;
5556
ID?: PDFObject;
5657
};
58+
rng: SimpleRNG;
5759

5860
private readonly indirectObjects: Map<PDFRef, PDFObject>;
5961

@@ -66,6 +68,7 @@ class PDFContext {
6668
this.trailerInfo = {};
6769

6870
this.indirectObjects = new Map();
71+
this.rng = SimpleRNG.withSeed(1);
6972
}
7073

7174
assign(ref: PDFRef, object: PDFObject): void {
@@ -287,6 +290,10 @@ class PDFContext {
287290
this.popGraphicsStateContentStreamRef = this.register(stream);
288291
return this.popGraphicsStateContentStreamRef;
289292
}
293+
294+
addRandomSuffix(prefix: string, suffixLength = 4): string {
295+
return `${prefix}-${Math.floor(this.rng.nextInt() * 10 ** suffixLength)}`;
296+
}
290297
}
291298

292299
export default PDFContext;

src/core/annotation/PDFWidgetAnnotation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class PDFWidgetAnnotation extends PDFAnnotation {
4646
return undefined;
4747
}
4848

49-
setP(page: PDFRef){
50-
this.dict.set(PDFName.of('P'),page);
49+
setP(page: PDFRef) {
50+
this.dict.set(PDFName.of('P'), page);
5151
}
5252

5353
setDefaultAppearance(appearance: string) {

src/core/embedders/CustomFontEmbedder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import PDFRef from 'src/core/objects/PDFRef';
77
import PDFString from 'src/core/objects/PDFString';
88
import PDFContext from 'src/core/PDFContext';
99
import {
10-
addRandomSuffix,
1110
byAscendingId,
1211
Cache,
1312
sortedUniq,
@@ -106,7 +105,8 @@ class CustomFontEmbedder {
106105
}
107106

108107
embedIntoContext(context: PDFContext, ref?: PDFRef): Promise<PDFRef> {
109-
this.baseFontName = this.customName || addRandomSuffix(this.fontName);
108+
this.baseFontName =
109+
this.customName || context.addRandomSuffix(this.fontName);
110110
return this.embedFontDict(context, ref);
111111
}
112112

0 commit comments

Comments
 (0)