import { useEffect, useRef, 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 {
FormLayer,
FormTransfer,
formPlugin,
useForm,
useFormState,
type FormBundle,
} from '@embedpdf/react/form';
import { localEngine } from '@embedpdf/engine';
import './data.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), formPlugin()];
// [!doc-source forms]
const forms = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/forms.pdf');
return { kind: 'bytes', id: 'forms', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// [!/doc-source]
/** On load: the form filled in. */
function useFilledIn() {
const form = useForm();
const status = useFormState((state) => state.status);
const started = useRef(false);
useEffect(() => {
if (status !== 'ready' || started.current) return;
started.current = true;
void form.importValues({ name: 'Ada Lovelace', email: 'ada@example.com', framework: 'Svelte' });
}, [form, status]);
}
function DataToolbar() {
useFilledIn();
const form = useForm();
const [bundle, setBundle] = useState(null);
const [result, setResult] = useState('');
return (
{result && }
);
}
/** The values as plain data, keyed by full name: what you'd send to your backend. */
function Values() {
const form = useForm();
// The state changes with every value, so this reads them again each time.
useFormState((state) => state.fields);
return
{JSON.stringify(form.exportValues(), null, 2)}
;
}
export default function App() {
return (
Loading…