Skip to content

happypdf / PDFImage

Class: PDFImage

Defined in: src/api/PDFImage.ts:11

Represents an image that has been embedded in a [[PDFDocument]].

Implements

Properties

doc

ts
readonly doc: PDFDocument;

Defined in: src/api/PDFImage.ts:30

The document to which this image belongs.


height

ts
readonly height: number;

Defined in: src/api/PDFImage.ts:36

The height of this image in pixels.


ref

ts
readonly ref: PDFRef;

Defined in: src/api/PDFImage.ts:27

The unique reference assigned to this image within the document.


width

ts
readonly width: number;

Defined in: src/api/PDFImage.ts:33

The width of this image in pixels.

Methods

embed()

ts
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&lt;void&gt;

Resolves when the embedding is complete.

Implementation of

Embeddable.embed


of()

ts
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

ParameterTypeDescription
refPDFRefThe unique reference for this image.
docPDFDocumentThe document to which the image will belong.
embedderImageEmbedderThe embedder that will be used to embed the image.

Returns

PDFImage


scale()

ts
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:

js
image.width  // => 500
image.height // => 250

const scaled = image.scale(0.5)
scaled.width  // => 250
scaled.height // => 125

This operation is often useful before drawing an image with [[PDFPage.drawImage]] to compute the width and height options.

Parameters

ParameterTypeDescription
factornumberThe factor by which this image should be scaled.

Returns

ts
{
  height: number;
  width: number;
}

The width and height of the image after being scaled.

height
ts
height: number;
width
ts
width: number;

scaleToFit()

ts
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 // => 375

The 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

ParameterTypeDescription
widthnumberThe bounding box's width.
heightnumberThe bounding box's height.

Returns

ts
{
  height: number;
  width: number;
}

The width and height of the image after being scaled.

height
ts
height: number;
width
ts
width: number;

size()

ts
size(): {
  height: number;
  width: number;
};

Defined in: src/api/PDFImage.ts:114

Get the width and height of this image. For example:

js
const { width, height } = image.size()

Returns

ts
{
  height: number;
  width: number;
}

The width and height of the image.

height
ts
height: number;
width
ts
width: number;

MIT Licensed. A fork of pdf-lib.