Image
pdfx/pdf-imageFreeComponent
Image component with 7 display variants, fit modes, and optional captions
See it running
Open the demo on pdfx
pdfx.akashpise.dev/docs/components/pdf-imageInstall it
npx shadcn@latest add https://pdfx.akashpise.dev/r/pdf-image.jsonSource
1import { Image, Text as PDFText, StyleSheet, View } from '@react-pdf/renderer';2import type { Style } from '@react-pdf/types';3import { usePdfxTheme, useSafeMemo } from '../lib/pdfx-theme-context';4type PdfxTheme = ReturnType<typeof usePdfxTheme>;56/** HTTP method used when fetching the image from a URL. */7export type PdfImageHTTPMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';89export type PdfImageSrc =10 | string11 | { uri: string; method?: PdfImageHTTPMethod; headers?: Record<string, string>; body?: string };1213export type PdfImageFit = 'cover' | 'contain' | 'fill' | 'none';1415export type PdfImageVariant =16 | 'default'17 | 'full-width'18 | 'thumbnail'19 | 'avatar'20 | 'cover'21 | 'bordered'22 | 'rounded';2324/**25 * Image element with layout presets, caption, and aspect ratio support.26 * Props - `src` | `variant` | `width` | `height` | `fit` | `position` | `caption` | `aspectRatio` | `borderRadius` | `noWrap` | `style`27 * @see {@link PdfImageProps}28 */29export interface PdfImageProps {30 src: PdfImageSrc;31 /**32 * @default 'default'33 */34 variant?: PdfImageVariant;35 width?: number | string;36 height?: number | string;37 fit?: PdfImageFit;38 /**39 * @default '50% 50%'40 */41 position?: string;42 caption?: string;43 aspectRatio?: number;44 borderRadius?: number;45 /**46 * @default true47 */48 noWrap?: boolean;49 style?: Style;50}5152interface VariantDefaults {53 width?: number | string;54 height?: number | string;55 fit: PdfImageFit;56 borderRadius?: number;57}5859const VARIANT_DEFAULTS: Record<PdfImageVariant, VariantDefaults> = {60 default: { fit: 'contain' },61 'full-width': { width: '100%', fit: 'cover' },62 thumbnail: { width: 80, height: 80, fit: 'cover' },63 avatar: { width: 48, height: 48, fit: 'cover', borderRadius: 999 },64 cover: { width: '100%', height: 160, fit: 'cover' },65 bordered: { width: '100%', fit: 'contain' },66 rounded: { width: 200, fit: 'contain', borderRadius: 8 },67};6869const UNSUPPORTED_FORMATS = ['webp', 'avif', 'heic', 'heif', 'ico'];7071function detectFormat(src: PdfImageSrc): string | null {72 if (typeof src !== 'string') return null;73 const dataMatch = src.match(/^data:image\/([a-zA-Z0-9+.-]+)/);74 if (dataMatch) return dataMatch[1].toLowerCase();75 return src.split('?')[0].split('.').pop()?.toLowerCase() ?? null;76}7778function warnIfUnsupported(src: PdfImageSrc): void {79 const fmt = detectFormat(src);80 if (fmt && UNSUPPORTED_FORMATS.includes(fmt)) {81 console.warn(82// … truncated
What it pulls in
npm packages
Files it writes
More from pdfx
All 34 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Alertalert | pdfx | Components | Free | 1 dep | |
| Badgebadge | pdfx | Components | Free | 1 dep | |
| Cardcard | pdfx | Components | Free | 1 dep | |
| DataTabledata-table | pdfx | Components | Free | 1 dep · 3 files | |
| Dividerdivider | pdfx | Components | Free | 1 dep | |
| Formform | pdfx | Components | Free | 1 dep · 3 files | |
| Graphgraph | pdfx | Components | Free | 1 dep · 4 files | |
| Headingheading | pdfx | Components | Free | 1 dep |
