1
- import { PDFArray , PDFDocument , PDFName } from 'src/index' ;
1
+ import fs from 'fs' ;
2
+ import { PDFArray , PDFDocument , PDFName , StandardFonts } from 'src/index' ;
3
+
4
+ const birdPng = fs . readFileSync ( 'assets/images/greyscale_bird.png' ) ;
2
5
3
6
describe ( `PDFDocument` , ( ) => {
4
- describe . only ( `getSize() method` , ( ) => {
5
- it ( `Returns the width and height of the the page's MediaBox` , async ( ) => {
7
+ describe ( `getSize() method` , ( ) => {
8
+ it ( `returns the width and height of the the page's MediaBox` , async ( ) => {
6
9
const pdfDoc = await PDFDocument . create ( ) ;
7
10
const page = pdfDoc . addPage ( ) ;
8
11
page . node . set ( PDFName . MediaBox , pdfDoc . context . obj ( [ 5 , 5 , 20 , 50 ] ) ) ;
9
12
expect ( page . getSize ( ) ) . toEqual ( { width : 15 , height : 45 } ) ;
10
13
} ) ;
11
14
} ) ;
12
15
13
- describe . only ( `setSize() method` , ( ) => {
14
- it ( `Sets the width and height of only the the page's MediaBox when the other boxes are not defined` , async ( ) => {
16
+ describe ( `setSize() method` , ( ) => {
17
+ it ( `sets the width and height of only the the page's MediaBox when the other boxes are not defined` , async ( ) => {
15
18
const pdfDoc = await PDFDocument . create ( ) ;
16
19
const page = pdfDoc . addPage ( ) ;
17
20
@@ -33,7 +36,7 @@ describe(`PDFDocument`, () => {
33
36
expect ( page . node . ArtBox ( ) ) . toBeUndefined ( ) ;
34
37
} ) ;
35
38
36
- it ( `Sets the width and height of the the page's CropBox, BleedBox, TrimBox, and ArtBox when they equal the MediaBox` , async ( ) => {
39
+ it ( `sets the width and height of the the page's CropBox, BleedBox, TrimBox, and ArtBox when they equal the MediaBox` , async ( ) => {
37
40
const pdfDoc = await PDFDocument . create ( ) ;
38
41
const page = pdfDoc . addPage ( ) ;
39
42
@@ -64,7 +67,7 @@ describe(`PDFDocument`, () => {
64
67
expect ( page . getArtBox ( ) ) . toEqual ( { x : 5 , y : 5 , width : 15 , height : 45 } ) ;
65
68
} ) ;
66
69
67
- it ( `Does not set the width and height of the the page's CropBox, BleedBox, TrimBox, or ArtBox when they do not equal the MediaBox` , async ( ) => {
70
+ it ( `does not set the width and height of the the page's CropBox, BleedBox, TrimBox, or ArtBox when they do not equal the MediaBox` , async ( ) => {
68
71
const pdfDoc = await PDFDocument . create ( ) ;
69
72
const page = pdfDoc . addPage ( ) ;
70
73
@@ -95,4 +98,54 @@ describe(`PDFDocument`, () => {
95
98
expect ( page . getArtBox ( ) ) . toEqual ( { x : 0 , y : 5 , width : 20 , height : 25 } ) ;
96
99
} ) ;
97
100
} ) ;
101
+
102
+ // https://github.com/Hopding/pdf-lib/issues/1075
103
+ it ( `drawImage() does not reuse existing XObject keys` , async ( ) => {
104
+ const pdfDoc1 = await PDFDocument . create ( ) ;
105
+ const image1 = await pdfDoc1 . embedPng ( birdPng ) ;
106
+ const page1 = pdfDoc1 . addPage ( ) ;
107
+
108
+ expect ( page1 . node . normalizedEntries ( ) . XObject . keys ( ) . length ) . toEqual ( 0 ) ;
109
+ page1 . drawImage ( image1 ) ;
110
+ expect ( page1 . node . normalizedEntries ( ) . XObject . keys ( ) . length ) . toEqual ( 1 ) ;
111
+
112
+ const key1 = page1 . node . normalizedEntries ( ) . XObject . keys ( ) [ 0 ] ;
113
+
114
+ const pdfDoc2 = await PDFDocument . load ( await pdfDoc1 . save ( ) ) ;
115
+ const image2 = await pdfDoc2 . embedPng ( birdPng ) ;
116
+ const page2 = pdfDoc2 . getPage ( 0 ) ;
117
+
118
+ expect ( page2 . node . normalizedEntries ( ) . XObject . keys ( ) . length ) . toEqual ( 1 ) ;
119
+ page2 . drawImage ( image2 ) ;
120
+ expect ( page2 . node . normalizedEntries ( ) . XObject . keys ( ) . length ) . toEqual ( 2 ) ;
121
+
122
+ const key2 = page2 . node . normalizedEntries ( ) . XObject . keys ( ) [ 1 ] ;
123
+ expect ( key1 ) . not . toEqual ( key2 ) ;
124
+ expect ( page2 . node . normalizedEntries ( ) . XObject . keys ( ) ) . toEqual ( [ key1 , key2 ] ) ;
125
+ } ) ;
126
+
127
+ // https://github.com/Hopding/pdf-lib/issues/1075
128
+ it ( `setFont() does not reuse existing Font keys` , async ( ) => {
129
+ const pdfDoc1 = await PDFDocument . create ( ) ;
130
+ const font1 = await pdfDoc1 . embedFont ( StandardFonts . Helvetica ) ;
131
+ const page1 = pdfDoc1 . addPage ( ) ;
132
+
133
+ expect ( page1 . node . normalizedEntries ( ) . Font . keys ( ) . length ) . toEqual ( 0 ) ;
134
+ page1 . setFont ( font1 ) ;
135
+ expect ( page1 . node . normalizedEntries ( ) . Font . keys ( ) . length ) . toEqual ( 1 ) ;
136
+
137
+ const key1 = page1 . node . normalizedEntries ( ) . Font . keys ( ) [ 0 ] ;
138
+
139
+ const pdfDoc2 = await PDFDocument . load ( await pdfDoc1 . save ( ) ) ;
140
+ const font2 = await pdfDoc2 . embedFont ( StandardFonts . Helvetica ) ;
141
+ const page2 = pdfDoc2 . getPage ( 0 ) ;
142
+
143
+ expect ( page2 . node . normalizedEntries ( ) . Font . keys ( ) . length ) . toEqual ( 1 ) ;
144
+ page2 . setFont ( font2 ) ;
145
+ expect ( page2 . node . normalizedEntries ( ) . Font . keys ( ) . length ) . toEqual ( 2 ) ;
146
+
147
+ const key2 = page2 . node . normalizedEntries ( ) . Font . keys ( ) [ 1 ] ;
148
+ expect ( key1 ) . not . toEqual ( key2 ) ;
149
+ expect ( page2 . node . normalizedEntries ( ) . Font . keys ( ) ) . toEqual ( [ key1 , key2 ] ) ;
150
+ } ) ;
98
151
} ) ;
0 commit comments