PDFImage
Represents an image that has been embedded in a [[PDFDocument]].
Class PDFImage
class PDFImage implements defaultRepresents an image that has been embedded in a [[PDFDocument]].
Properties
doc
PDFDocumentThe document to which this image belongs.
height
numberThe height of this image in pixels.
ref
PDFRefThe unique reference assigned to this image within the document.
width
numberThe width of this image in pixels.
of
(ref: PDFRef, doc: PDFDocument, embedder: ImageEmbedder) => PDFImageNOTE: 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
Methods
embed
embed(): Promise<void>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.
scale
scale(factor: number): { height: number; width: number }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
factor: numberThe factor by which this image should be scaled.
Returns { height: number; width: number }
The width and height of the image after being scaled.
scaleToFit
scaleToFit(width: number, height: number): { height: number; width: number }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
width: numberThe bounding box's width.height: numberThe bounding box's height.
Returns { height: number; width: number }
The width and height of the image after being scaled.
size
size(): { height: number; width: number }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.