Skip to content

Quick Start

Terminal window
npm install nebula-pdf-engine

Note: This package requires native dependencies sharp and @resvg/resvg-js.

Install Preact as a dev dependency so TypeScript can resolve JSX types (runtime Preact is bundled):

Terminal window
npm install -D preact

Configure tsconfig.json:

{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
import { PdfEngine, Page, Text, Image } from 'nebula-pdf-engine';
import * as fs from 'fs';
const engine = new PdfEngine({
fonts: [
{ name: 'Inter', data: fs.readFileSync('./fonts/Inter-Regular.ttf'), weight: 400 },
],
devicePixelRatio: 2,
});
const pdfBuffer = await engine.generate(
<Page size="A4" padding={40}>
<Text style={{ fontSize: 32, marginBottom: 20 }}>Hello Nebula!</Text>
<Image src="https://example.com/logo.png" width={100} height={50} />
<Text style={{ marginTop: 20 }}>
This content will automatically flow across multiple pages if it is too long.
</Text>
</Page>,
);
fs.writeFileSync('output.pdf', pdfBuffer);

Fonts are required. PdfEngine throws if fonts is empty.

Use createElement (alias h) in .ts files:

import { PdfEngine, Page, Text, createElement } from 'nebula-pdf-engine';
const pdf = await engine.generate(
createElement(
Page,
{ size: 'A4' },
createElement(Text, { style: { fontSize: 20 } }, 'Hello Workspace!'),
),
);