happypdf / PDFImage
Class: PDFImage
Defined in: src/api/PDFImage.ts:11
Represents an image that has been embedded in a [[PDFDocument]].
Implements
Properties
doc
readonly doc: PDFDocument;Defined in: src/api/PDFImage.ts:30
The document to which this image belongs.
height
readonly height: number;Defined in: src/api/PDFImage.ts:36
The height of this image in pixels.
ref
readonly ref: PDFRef;Defined in: src/api/PDFImage.ts:27
The unique reference assigned to this image within the document.
width
readonly width: number;Defined in: src/api/PDFImage.ts:33
The width of this image in pixels.
Methods
embed()
embed(): Promise<void>;Defined in: src/api/PDFImage.ts:127
> NOTE: You probably don't need to call this method directly. The > [[PDFDocument.save]] and [[PDFDocument.saveAsBase64]] methods will > automatically ensure all images get embedded.
Embed this image in its document.
Returns
Promise<void>
Resolves when the embedding is complete.
Implementation of
of()
static of(
ref,
doc,
embedder): PDFImage;Defined in: src/api/PDFImage.ts:23
> NOTE: You probably don't want to call this method directly. Instead, > consider using the [[PDFDocument.embedPng]] and [[PDFDocument.embedJpg]] > methods, which will create instances of [[PDFImage]] for you.
Create an instance of [[PDFImage]] from an existing ref and embedder
Parameters
| Parameter | Type | Description |
|---|---|---|
ref | PDFRef | The unique reference for this image. |
doc | PDFDocument | The document to which the image will belong. |
embedder | ImageEmbedder | The embedder that will be used to embed the image. |
Returns
PDFImage
scale()
scale(factor): {
height: number;
width: number;
};Defined in: src/api/PDFImage.ts:73
Compute the width and height of this image after being scaled by the given factor. For example:
image.width // => 500
image.height // => 250
const scaled = image.scale(0.5)
scaled.width // => 250
scaled.height // => 125This operation is often useful before drawing an image with [[PDFPage.drawImage]] to compute the width and height options.
Parameters
| Parameter | Type | Description |
|---|---|---|
factor | number | The factor by which this image should be scaled. |
Returns
{
height: number;
width: number;
}The width and height of the image after being scaled.
height
height: number;width
width: number;scaleToFit()
scaleToFit(width, height): {
height: number;
width: number;
};Defined in: src/api/PDFImage.ts:96
Get the width and height of this image after scaling it as large as possible while maintaining its aspect ratio and not exceeding the specified width and height. For example:
image.width // => 500
image.height // => 250
const scaled = image.scaleToFit(750, 1000)
scaled.width // => 750
scaled.height // => 375The width and height parameters can also be thought of as the width and height of a box that the scaled image must fit within.
Parameters
| Parameter | Type | Description |
|---|---|---|
width | number | The bounding box's width. |
height | number | The bounding box's height. |
Returns
{
height: number;
width: number;
}The width and height of the image after being scaled.
height
height: number;width
width: number;size()
size(): {
height: number;
width: number;
};Defined in: src/api/PDFImage.ts:114
Get the width and height of this image. For example:
const { width, height } = image.size()Returns
{
height: number;
width: number;
}The width and height of the image.
height
height: number;width
width: number;