Skip to content

Data Grid PDF export

toPdf(grid, options?) writes a Data Grid’s view as a PDF document (Promise<Blob>), with our own PDF writer: no dependencies, nothing uploaded. For paper, the grid’s own print() prints a real table without a file.

import { DataGrid } from '@absoluteui/components/data-grid';
import { toPdf } from '@absoluteui/components/data-grid/pdf';
interface Invoice {
id: string;
total: number;
}
const host = document.createElement('div');
host.style.setProperty('block-size', '200px');
document.body.append(host);
const grid = DataGrid.create<Invoice>(host, {
label: 'Invoices',
rows: [{ id: 'INV-1', total: 120 }],
rowId: (r) => r.id,
columns: [
{ id: 'id', header: 'Invoice', field: 'id' },
{ id: 'total', header: 'Total', field: 'total', type: 'number' },
],
});
const blob = await toPdf(grid, { title: 'Invoices', pageSize: 'a4', orientation: 'landscape' });
console.log(blob.type, blob.size);
  • Pages carry the title, the column group and header rows repeated on every page, bold group and total rows, end-aligned numbers, and “Page n of N”.
  • A4 or Letter (pageSize), landscape by default (orientation), with margin, fontSize, and pageLabel (“Page {page} of {pages}”); the grid’s column widths are scaled down to fit the page.
  • Latin text uses the standard PDF fonts. Other scripts need fonts: { regular, bold } (TrueType or OpenType bytes): the fonts are embedded with a ToUnicode map so text can be searched and copied, and fonts whose license forbids embedding are refused. parseFont reads a font’s tables.
  • Right-to-left grids mirror the columns. visualOrder(text, rtl) is the simplified bidirectional ordering used for right-to-left text (not the full Unicode algorithm; no Arabic shaping).
  • The document properties carry the title (shown as the window title) and the grid’s language.
  • Invalid options throw a TypeError that names the property (“Invalid PDF export: … must be …”).
  • A font that cannot be read or may not be embedded throws “Invalid PDF font: …”.

toPdf

The grid as a PDF Blob: the toCsv row and column choices, column widths scaled to the page, header rows repeated on every page, bold group and total rows, end-aligned numbers, and page numbers. More than 100,000 rows throws. The PDF is not tagged: assistive technology reads the text in reading order; for structure, prefer the grid itself, CSV, or Excel.

function toPdf<Row>(grid: DataGrid<Row>, options?: DataGridPdfOptions): Promise<Blob>;

visualOrder

Simplified bidirectional ordering for right-to-left text: runs of right-to-left letters are reversed and the runs are laid out from the right; digits and Latin runs keep their order. Not the full Unicode algorithm; no Arabic shaping.

function visualOrder(text: string, rtl: boolean): string;

DataGridPdfOptions

interface DataGridPdfOptions extends Omit<DataGridExportOptions, 'text'> {
readonly title?: string;
readonly pageSize?: 'a4' | 'letter';
readonly orientation?: 'portrait' | 'landscape';
readonly margin?: number;
readonly fontSize?: number;
readonly fonts?: {
readonly regular: Uint8Array | ArrayBuffer;
readonly bold?: Uint8Array | ArrayBuffer;
};
readonly rightToLeft?: boolean;
readonly columnGroups?: boolean;
readonly pageLabel?: string;
}

parseFont

Parse a TrueType or OpenType font (throws for other files or fonts whose license forbids embedding).

function parseFont(input: Uint8Array): ParsedFont;

winAnsiByte

The WinAnsi byte of a code point, or null when the standard fonts cannot show it.

function winAnsiByte(cp: number): number | null;

ParsedFont

The parts of a TrueType or OpenType font the writer needs.

interface ParsedFont {
readonly bytes: Uint8Array;
readonly cff: boolean;
readonly unitsPerEm: number;
readonly ascent: number;
readonly descent: number;
readonly capHeight: number;
readonly bbox: readonly [number, number, number, number];
readonly italicAngle: number;
readonly name: string;
glyph(cp: number): number;
advance(glyph: number): number;
}

PdfFont

A font the writer can use: its PDF resource, how to measure, and how to encode text.

interface PdfFont {
width(text: string, size: number): number;
encode(text: string): string;
}