Skip to content

Commit a24c1d6

Browse files
committed
Add image embedding example to README
1 parent c6f8b20 commit a24c1d6

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [Document Creation](#document-creation)
4444
- [Document Modification](#document-modification)
4545
- [Copying Pages](#copying-pages)
46+
- [Embed PNG and JPEG Images](#embed-png-and-jpeg-images)
4647
- [Embed Font and Measure Text](#embed-font-and-measure-text)
4748
- [Installation](#installation)
4849
- [Prior Art](#prior-art)
@@ -171,6 +172,48 @@ pdfDoc.insertPage(0, secondDonorPage)
171172
const pdfBytes = await pdfDoc.save()
172173
```
173174

175+
### Embed PNG and JPEG Images
176+
177+
_This example produces [this PDF](assets/pdfs/examples/embed_png_and_jpeg_images.pdf)_ (when [this image](assets/images/cat_riding_unicorn.jpg) is used for the `jpgImageBytes` variable and [this image](assets/images/minions_banana_alpha.jpg) is used for the `pngImageBytes` variable).
178+
179+
<!-- prettier-ignore -->
180+
```js
181+
import { PDFDocument } from 'pdf-lib'
182+
183+
// These should be a Uint8Arrays.
184+
// This data can be obtained in a number of different ways.
185+
// If your running in a Node environment, you could use fs.readFile().
186+
// In the browser, you could make a fetch() call and use res.arrayBuffer().
187+
const jpgImageBytes = ...
188+
const pngImageBytes = ...
189+
190+
const pdfDoc = await PDFDocument.create()
191+
192+
const jpgImage = await pdfDoc.embedJpg(jpgImageBytes)
193+
const pngImage = await pdfDoc.embedPng(pngImageBytes)
194+
195+
const jpgDims = jpgImage.scale(0.25)
196+
const pngDims = pngImage.scale(0.5)
197+
198+
const page = pdfDoc.addPage()
199+
200+
page.drawImage(jpgImage, {
201+
x: page.getWidth() / 2 - jpgDims.width / 2,
202+
y: page.getHeight() / 2 - jpgDims.height / 2,
203+
width: jpgDims.width,
204+
height: jpgDims.height,
205+
})
206+
207+
page.drawImage(pngImage, {
208+
x: page.getWidth() / 2 - pngDims.width / 2 + 75,
209+
y: page.getHeight() / 2 - pngDims.height,
210+
width: pngDims.width,
211+
height: pngDims.height,
212+
})
213+
214+
const pdfBytes = await pdfDoc.save()
215+
```
216+
174217
### Embed Font and Measure Text
175218

176219
`pdf-lib` relies on a sister module to support embedding custom fonts: [`@pdf-lib/fontkit`](https://www.npmjs.com/package/@pdf-lib/fontkit). You must add the `@pdf-lib/fontkit` module to your project and register it using `pdfDoc.registerFontkit(...)` before embedding custom fonts.
Binary file not shown.

scratchpad/index.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
1-
import fontkit from '@pdf-lib/fontkit';
21
import fs from 'fs';
3-
import { PDFDocument, rgb } from 'src/index';
2+
import { PDFDocument } from 'src/index';
43

54
(async () => {
6-
// This should be a Uint8Array.
5+
// These should be a Uint8Arrays.
76
// This data can be obtained in a number of different ways.
87
// If your running in a Node environment, you could use fs.readFile().
98
// In the browser, you could make a fetch() call and use res.arrayBuffer().
10-
const fontBytes = fs.readFileSync('assets/fonts/ubuntu/Ubuntu-R.ttf');
9+
const jpgImageBytes = fs.readFileSync('assets/images/cat_riding_unicorn.jpg');
10+
const pngImageBytes = fs.readFileSync(
11+
'assets/images/minions_banana_alpha.png',
12+
);
1113

1214
const pdfDoc = await PDFDocument.create();
1315

14-
pdfDoc.registerFontkit(fontkit);
16+
const jpgImage = await pdfDoc.embedJpg(jpgImageBytes);
17+
const pngImage = await pdfDoc.embedPng(pngImageBytes);
1518

16-
const customFont = await pdfDoc.embedFont(fontBytes);
19+
const jpgDims = jpgImage.scale(0.25);
20+
const pngDims = pngImage.scale(0.5);
1721

1822
const page = pdfDoc.addPage();
1923

20-
const text = 'This is text in an embedded font!';
21-
const textSize = 35;
22-
const textWidth = customFont.widthOfTextAtSize(text, textSize);
23-
const textHeight = customFont.heightAtSize(textSize);
24-
25-
// Draw text on page
26-
page.drawText(text, {
27-
x: 40,
28-
y: 450,
29-
size: textSize,
30-
font: customFont,
31-
color: rgb(0, 0.53, 0.71),
24+
page.drawImage(jpgImage, {
25+
x: page.getWidth() / 2 - jpgDims.width / 2,
26+
y: page.getHeight() / 2 - jpgDims.height / 2,
27+
width: jpgDims.width,
28+
height: jpgDims.height,
3229
});
3330

34-
// Draw a box around the text
35-
page.drawRectangle({
36-
x: 40,
37-
y: 450,
38-
width: textWidth,
39-
height: textHeight,
40-
borderColor: rgb(1, 0, 0),
41-
borderWidth: 1.5,
31+
page.drawImage(pngImage, {
32+
x: page.getWidth() / 2 - pngDims.width / 2 + 75,
33+
y: page.getHeight() / 2 - pngDims.height,
34+
width: pngDims.width,
35+
height: pngDims.height,
4236
});
4337

4438
const pdfBytes = await pdfDoc.save();

0 commit comments

Comments
 (0)