In the browser
The hook and the components of @inkoxide/react/browser that render a document in the page.
The React helpers, from @inkoxide/react/browser; @inkoxide/vue/browser has the same, @inkoxide/svelte/browser the components and createPdf for usePdf, and @inkoxide/angular/browser injectPdf and the directives inkPdfViewer and inkPdfDownload. How each framework uses them is in the guide.
usePdf
Renders element to a PDF in the engine's worker when the component mounts, and again on update(). A render that starts stops the one still running; unmounting stops it too. opts are read when a render starts.
const { url, loading, update } = usePdf(<Invoice invoice={invoice} />);
import { Document, Page, Text, usePdf } from '@inkoxide/react/browser';
const Letter = ({ name }: { name: string }) => (
<Document>
<Page size="A5" className="p-10">
<Text>Dear {name},</Text>
</Page>
</Document>
);
export default function Preview({ name }: { name: string }) {
const { url, loading, error, update } = usePdf(<Letter name={name} />);
if (error) return <p>The PDF did not render.</p>;
return (
<>
<button onClick={() => update(<Letter name={name} />)} disabled={loading}>
{loading ? 'Rendering…' : 'Render again'}
</button>
{url && <iframe src={url} title="Letter" style={{ width: '100%', height: 600 }} />}
</>
);
}
<script setup lang="ts">
import { Document, Page, Text, usePdf } from '@inkoxide/vue/browser';
import { h } from 'vue';
const props = defineProps<{ name: string }>();
// A function that takes props is a functional component.
const Letter = ({ name }: { name: string }) =>
h(Document, null, () => h(Page, { size: 'A5', class: 'p-10' }, () => h(Text, null, () => `Dear ${name},`)));
// Renders again when the getter's props change, and on update().
const { url, loading, error, update } = usePdf(() => h(Letter, { name: props.name }));
</script>
<template>
<p v-if="error">The PDF did not render.</p>
<template v-else>
<button :disabled="loading" @click="update()">{{ loading ? 'Rendering…' : 'Render again' }}</button>
<iframe v-if="url" :src="url" title="Letter" style="width: 100%; height: 600px" />
</template>
</template>
<!-- Letter.svelte -->
<script lang="ts">
import { Document, Page, Text } from '@inkoxide/svelte';
let { name }: { name: string } = $props();
</script>
<Document>
<Page size="A5" class="p-10">
<Text>Dear {name},</Text>
</Page>
</Document>
<!-- Preview.svelte -->
<script lang="ts">
import { createPdf } from '@inkoxide/svelte/browser';
import Letter from './Letter.svelte';
let { name }: { name: string } = $props();
// Renders again when what the function reads changes, and on update().
const pdf = createPdf(() => ({ component: Letter, props: { name } }));
</script>
{#if pdf.error}
<p>The PDF did not render.</p>
{:else}
<button disabled={pdf.loading} onclick={() => pdf.update()}>{pdf.loading ? 'Rendering…' : 'Render again'}</button>
{#if pdf.url}
<iframe src={pdf.url} title="Letter" style="width: 100%; height: 600px"></iframe>
{/if}
{/if}
import { Component, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core';
import { injectPdf } from '@inkoxide/angular/browser';
@Component({
selector: 'app-letter',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<ink-document>
<ink-page size="A5" class="p-10">
<ink-text>Dear {{ name() }},</ink-text>
</ink-page>
</ink-document>
`,
})
export class Letter {
readonly name = input('');
}
@Component({
selector: 'app-preview',
template: `
@if (pdf.error()) {
<p>The PDF did not render.</p>
} @else {
<button [disabled]="pdf.loading()" (click)="pdf.update()">{{ pdf.loading() ? 'Rendering…' : 'Render again' }}</button>
@if (pdf.safeUrl(); as src) {
<iframe [src]="src" title="Letter" style="width: 100%; height: 600px"></iframe>
}
}
`,
})
export class Preview {
readonly name = input.required<string>();
// Renders again when the signals it reads change, and on update().
readonly pdf = injectPdf(() => ({ component: Letter, inputs: { name: this.name() } }));
}
| Argument | Type | Default | Description |
|---|---|---|---|
element | ReactElement | ||
opts | BrowserRenderOptions | {} |
It returns:
| Field | Type | Description |
|---|---|---|
url | string | null | An object URL of blob, revoked when a new one replaces it and on unmount; or null, and null again while the component is hidden (<Activity mode="hidden">) until it renders again. |
update | (document?: ReactElement) => void | |
blob | Blob | null | The PDF of the last render that finished, or null. |
loading | boolean | Whether a render is running. |
error | unknown | Why the last render failed (a RenderError, usually), or null. |
PdfDownloadLink
A link that downloads document as a PDF, once it has rendered. It renders when it mounts and when document changes: an element of another type or key, or with other props.
import { Document, Page, PdfDownloadLink, Text } from '@inkoxide/react/browser';
const Receipt = () => (
<Document>
<Page size="A6" className="p-8">
<Text className="text-lg font-bold">Receipt</Text>
</Page>
</Document>
);
export default function DownloadButton() {
return (
<PdfDownloadLink document={<Receipt />} fileName="receipt.pdf">
{({ loading }) => (loading ? 'Rendering…' : 'Download the receipt')}
</PdfDownloadLink>
);
}
<script setup lang="ts">
import { Document, Page, PdfDownloadLink, Text } from '@inkoxide/vue/browser';
import { h } from 'vue';
const receipt = h(Document, null, () =>
h(Page, { size: 'A6', class: 'p-8' }, () => h(Text, { class: 'text-lg font-bold' }, () => 'Receipt')),
);
</script>
<template>
<PdfDownloadLink :document="receipt" file-name="receipt.pdf" v-slot="{ loading }">
{{ loading ? 'Rendering…' : 'Download the receipt' }}
</PdfDownloadLink>
</template>
<!-- Receipt.svelte -->
<script lang="ts">
import { Document, Page, Text } from '@inkoxide/svelte';
</script>
<Document>
<Page size="A6" class="p-8">
<Text class="text-lg font-bold">Receipt</Text>
</Page>
</Document>
<!-- DownloadButton.svelte -->
<script lang="ts">
import { PdfDownloadLink } from '@inkoxide/svelte/browser';
import Receipt from './Receipt.svelte';
</script>
<PdfDownloadLink document={Receipt} fileName="receipt.pdf">
{#snippet children(pdf)}{pdf.loading ? 'Rendering…' : 'Download the receipt'}{/snippet}
</PdfDownloadLink>
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { InkPdfDownload } from '@inkoxide/angular/browser';
@Component({
selector: 'app-receipt',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<ink-document>
<ink-page size="A6" class="p-8">
<ink-text class="text-lg font-bold">Receipt</ink-text>
</ink-page>
</ink-document>
`,
})
export class Receipt {}
@Component({
selector: 'app-download-button',
imports: [InkPdfDownload],
template: `
<a [inkPdfDownload]="receipt" inkPdfDownloadFile="receipt.pdf" #link="inkPdfDownload">
{{ link.pdf.loading() ? 'Rendering…' : 'Download the receipt' }}
</a>
`,
})
export class DownloadButton {
readonly receipt = { component: Receipt };
}
| Prop | Type | Default | Description |
|---|---|---|---|
document (required) | ReactElement | The <Document> element to render. | |
fileName | string | document.pdf | The name the file is saved under. |
options | BrowserRenderOptions | ||
children | ReactNode | ((pdf: UsePdfResult) => ReactNode) | What the link shows, or a function of the render's state. |
Other props go to the <a> element.
PdfViewer
document, rendered and shown in an <iframe>, in the browser's own PDF viewer. It renders when it mounts and when document changes, and shows the last PDF until the next one is ready.
import { Document, Page, PdfViewer, Text } from '@inkoxide/react/browser';
const Receipt = () => (
<Document>
<Page size="A6" className="p-8">
<Text className="text-lg font-bold">Receipt</Text>
</Page>
</Document>
);
export default function ReceiptPreview() {
return <PdfViewer document={<Receipt />} fallback={<p>Rendering…</p>} style={{ width: '100%', height: 600 }} />;
}
<script setup lang="ts">
import { Document, Page, PdfViewer, Text } from '@inkoxide/vue/browser';
import { h } from 'vue';
const receipt = h(Document, null, () =>
h(Page, { size: 'A6', class: 'p-8' }, () => h(Text, { class: 'text-lg font-bold' }, () => 'Receipt')),
);
</script>
<template>
<PdfViewer :document="receipt" style="width: 100%; height: 600px">
<template #fallback><p>Rendering…</p></template>
</PdfViewer>
</template>
<!-- Receipt.svelte -->
<script lang="ts">
import { Document, Page, Text } from '@inkoxide/svelte';
</script>
<Document>
<Page size="A6" class="p-8">
<Text class="text-lg font-bold">Receipt</Text>
</Page>
</Document>
<!-- ReceiptPreview.svelte -->
<script lang="ts">
import { PdfViewer } from '@inkoxide/svelte/browser';
import Receipt from './Receipt.svelte';
</script>
<PdfViewer document={Receipt} style="width: 100%; height: 600px">
{#snippet fallback()}<p>Rendering…</p>{/snippet}
</PdfViewer>
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { InkPdfViewer } from '@inkoxide/angular/browser';
@Component({
selector: 'app-receipt',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<ink-document>
<ink-page size="A6" class="p-8">
<ink-text class="text-lg font-bold">Receipt</ink-text>
</ink-page>
</ink-document>
`,
})
export class Receipt {}
@Component({
selector: 'app-receipt-preview',
imports: [InkPdfViewer],
template: `<iframe [inkPdfViewer]="receipt" title="Receipt" style="width: 100%; height: 600px"></iframe>`,
})
export class ReceiptPreview {
readonly receipt = { component: Receipt };
}
| Prop | Type | Default | Description |
|---|---|---|---|
document (required) | ReactElement | The <Document> element to render. | |
options | BrowserRenderOptions | ||
fallback | ReactNode | Shown in place of the frame while the first render runs, or when it failed. | |
showToolbar | boolean | true | false asks the viewer to hide its toolbar (#toolbar=0 on the URL). Chromium's viewer hides it; others may not. |
Other props go to the <iframe> element.
Synced from components/browser.md in the inkoxide repository.