Documentation
Creating PDFs
PDFs in Vue
PDFs in Nuxt
PDFs in React
PDFs in Angular
Images
Image places a raster image, PNG or JPEG. Give it a source, and optionally a size.
import { Image } from "@jasy/pdf";
Image("logo.png", { width: 120 });
Hand it an SVG and it stays a vector instead of becoming a bitmap - same call, no flag:
Image("logo.svg", { width: 120 });
See SVG and Canvas for what that path covers.
Sources
| Source | Example | |
|---|---|---|
| a file path | Image("logo.png") | Node only - the browser has no paths |
| a URL | Image("https://example.com/logo.png") | http(s): and data:, fetched (below) |
| the bytes | Image(bytes) | a Uint8Array - an upload, a fetch() |
your own CustomImage | Image(myImage) | any other source you load yourself |
What the image is comes from its bytes, never from its name, so .../photo?id=7 works. A file is read
when you call Image, so a wrong path fails on that line, not later in the render.
- PNG keeps its transparency.
- JPEG keeps the color space it was saved in: gray, RGB or CMYK. A photo's EXIF orientation is applied, so a phone picture stands upright, as it does in a browser.
From a URL since beta.4
An http(s): or data: address works wherever a file path does - PNG, JPEG and SVG alike:
Image("https://example.com/logo.png", { width: 120 });
Image("https://example.com/logo.svg", { width: 120 }); // still a vector
The image is fetched once per render, before layout, so a width-only image still gets its height from
the real aspect ratio. A dead link fails with an ImageUrlError that names the address - it is never
silently left out. Each fetch has a 15 s timeout and a 32 MB ceiling, enforced while the bytes arrive.
A URL image that is first created inside a PageBuilder cannot be fetched before layout. Give it a
width and a height; without them it fills the space it is offered and reports image-unsized (see
Output).
Size changed in beta.5
With no size, an image is as large as its file says: its pixels at the resolution the file declares
(a PNG's pHYs, a JPEG's JFIF or EXIF), else at 96 dpi - a CSS pixel. A 240 px logo without a
declaration is 180 pt wide; a 144 dpi screenshot comes out at half its pixel count. Wider than the
space it is offered, it is scaled down to fit, keeping its ratio. It is never scaled up.
Before beta.5 a pixel was a point, and the resolution in the file was ignored.
Image("logo.png"); // its own size, scaled down to fit
Image("logo.png", { width: 120 }); // the height follows the ratio
Image("banner.png", { width: "100%" }); // the full width it is offered
Image("photo.jpg", { width: 160, height: 100, fit: "cover" }); // a box of its own
Give one side and the other follows the image's ratio. A size is points or a percentage of the
space offered. aspectRatio replaces the image's own ratio, and minWidth / maxWidth / minHeight
/ maxHeight clamp the result, as on any element (see Layout).
Fitting the box
fit controls how the image fills its box. It mirrors CSS object-fit.
fit | What it does |
|---|---|
"fill" | stretched to the box exactly (the default) - may distort |
"contain" | scaled to fit inside the box, whole image visible, may letterbox |
"cover" | scaled to fill the box, cut at the edges, no distortion |
"none" | its own size, not scaled, cut at the edges of the box |
Without a size the box is derived from the image, so the default "fill" draws it undistorted. With
both width and height, pick the fit you want.
Where it sits in its box since beta.5
When fit leaves room or cuts the image, align says where it sits. The default is "center".
Image("portrait.jpg", { width: 120, height: 120, fit: "cover", align: "top" }); // keeps the face
align is "center", "top", "bottom", "left", "right", "top left", "top right",
"bottom left" or "bottom right".
Where the image's box sits in its Column or Row is the parent's align, or the image's own
alignSelf - as for any element.
Rounded and circular
radius rounds the image box (the image is clipped to the rounded rect). A square image with a radius
of half its side becomes a circle, perfect for an avatar.
Image("avatar.jpg", { width: 96, height: 96, fit: "cover", radius: 48 });
Image("photo.jpg", { width: 160, height: 100, fit: "cover", radius: [12, 12, 0, 0] }); // top only
One number rounds all four corners. An object names them ({ topLeft, topRight, bottomRight, bottomLeft }); an array is CSS order, clockwise from the top left.
Options
| Option | Type | What it does |
|---|---|---|
width height | number | "50%" | the size, in points or a percentage of the space offered |
fit | "fill" | "contain" | "cover" | "none" | how the image fills its box (default "fill") |
align since beta.5 | see above | where the image sits in its box (default "center") |
radius | number | object | [tl, tr, br, bl] | rounds the image box |
alt | string | alternate text - in a tagged PDF the image is a figure; without it, decoration |
aspectRatio | number | width / height, replacing the image's own ratio |
minWidth maxWidth minHeight maxHeight | number | "50%" | clamp the size |
alignSelf order flexShrink | its place in a Column or Row, as on any element (Layout) |
An SVG through Image takes the size and layout options and alt. A fit other than "contain",
align and radius are a named error there: a drawing is always scaled to fit and centered, which is
SVG's own default.
What it refuses
Nothing is silently dropped or drawn wrong. Each of these stops the render with an error that says what to do:
- a file that cannot be read, a URL that cannot be fetched (
ImageUrlError), or a format other than PNG, JPEG and SVG; - a JPEG that PDF viewers cannot decode - lossless, hierarchical or arithmetic-coded (
JpegFormatError); - a CMYK JPEG in a PDF/A document, whose sRGB output intent forbids CMYK (
JpegFormatError) - convert the image to RGB first.
See it all in one file
Copy this next to an image named photo.png and run it with npx tsx images.ts.
import { writeFileSync } from "node:fs";
import { Document, Page, Row, Column, Text, Image, renderToBytes } from "@jasy/pdf";
async function build() {
const doc = Document([
Page({ size: "A4", margin: 56 }, [
Column({ gap: 16 }, [
Text("Images", { size: 22, bold: true, color: "#1450aa" }),
Image("photo.png", { width: 200 }),
Row({ gap: 16 }, [
Image("photo.png", { width: 120, height: 90, fit: "contain" }),
Image("photo.png", { width: 120, height: 90, fit: "cover", align: "top" }),
Image("photo.png", { width: 90, height: 90, fit: "cover", radius: 45 }),
]),
]),
]),
]);
writeFileSync("images.pdf", await renderToBytes(doc));
}
build();