@openworkbook/pdf
v0.2.2
Published
Direct, vector, text-selectable PDF export for @openworkbook/core workbooks (browser and Node.js, no print dialog, no headless browser)
Maintainers
Readme
@openworkbook/pdf
Direct PDF export for OpenWorkbook workbooks: a vector, text-selectable PDF of one sheet, several
sheets or the whole workbook, in the browser and in Node.js (Next.js route handlers, Server Actions), with no print
dialog, no headless browser and no canvas. Dependencies: @openworkbook/core and fflate. MIT.
import { exportPdf, pdfResponse, downloadPdf } from '@openworkbook/pdf';
const bytes = exportPdf(workbook, { sheets: 'all', title: 'Q3 report' }); // Uint8Array
export async function GET() { return pdfResponse(book, 'report.pdf'); } // Next.js route handler
downloadPdf(workbook, 'report.pdf'); // browserIn the Studio: <Studio pdf={pdf} /> with import * as pdf from '@openworkbook/pdf' enables File, Export to PDF.
What it does
- Same pages as printing: each sheet's page setup (paper, orientation, margins, scale and fit to page, print
areas, print titles, manual page breaks, page order, centring, first page number, gridlines, headings, black and
white, draft) through core's
layoutPrintPages, the pagination of the print helpers and Print Preview. - Same drawing as the grid: cells are painted by a page painter that follows the grid's rules and reuses its
helpers; charts, shapes, text effects, pictures (PNG, JPEG, SVG; GIF, WebP and BMP are converted to PNG in a browser by the asynchronous export), barcodes, slicers, timelines, ink and sparklines
are drawn by the grid's
ObjectPainterthrough a PDF implementation of the canvas 2D context, so they stay vector. - Text: the 14 standard PDF fonts with their metrics when no font file is given; embedded TrueType / OpenType fonts as subset CID fonts with ToUnicode maps (selectable and searchable), fallback fonts for missing characters, bidi reordering and Arabic shaping. Characters no font can draw are reported, never dropped silently.
- Document: clickable cell and object links (web and internal), one bookmark per sheet, document properties,
header / footer codes with formatting and pictures (
&G), odd / even / first-page headers and footers, notes and comments (as displayed or at the end), optional AES-256 encryption (PDF 2.0 security handler, user / owner passwords, permissions). - Warnings for everything that is approximated or left out.
Documentation
- PDF export: guide, options, fonts, fidelity table, warnings, limitations
- API: @openworkbook/pdf
Tests
pnpm test runs the unit tests (writer, encryption with known-answer tests of SHA-2 and AES, fonts and subsetting
with a synthetic TrueType font, bidi and Arabic shaping, pagination equality with sheetPrintLayout, page geometry
for every paper size, header / footer codes, links, pictures, Unicode, options). test/oracle.local.test.ts
additionally opens the output with PyMuPDF, poppler (pdftotext, pdfinfo) and Ghostscript (and, on macOS, PDFKit
for encrypted files) and extracts the text back with real fonts when those tools and fonts are installed; it skips
otherwise.
