import { useState } from 'react'; import { Viewer, DocumentGate } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { actionsPlugin } from '@embedpdf/react/actions'; import { FormLayer, formPlugin, toFieldRef, useFormEvent, useFormValue, } from '@embedpdf/react/form'; import { localEngine } from '@embedpdf/engine'; import './scripts.css'; const engine = localEngine(); // The form's scripts run with JavaScript on in the actions plugin. const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), formPlugin(), actionsPlugin({ javascript: { enabled: true } }), ]; const invoice = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/invoice.pdf'); return { kind: 'bytes', id: 'invoice', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // The amounts and the total are calculated by the form's scripts. function InvoiceBar() { const total = useFormValue(toFieldRef('invoice.total')); const [refused, setRefused] = useState(null); // A script refused a value, such as a date that isn't day/month/year. useFormEvent( (form) => form.onValidationRejected, ({ field, issues }) => setRefused(`${field.name}: ${issues[0]?.message ?? 'refused'}`), ); return (
Total: {total && 'value' in total && total.value ? total.value : 'type a quantity'} {refused && {refused}}
); } export default function App() { return ( Loading…

}> {() => ( <> )}
); }