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 { SelectionLayer, selectionPlugin } from '@embedpdf/react/selection'; import { interactionPlugin } from '@embedpdf/react/interaction'; import { AnnotationLayer, AnnotationTransfer, annotationPlugin, useAnnotation, useAnnotationList, useAnnotationState, } from '@embedpdf/react/annotation'; import { localEngine } from '@embedpdf/engine'; import './export-import.css'; const engine = localEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), selectionPlugin(), annotationPlugin(), ]; // [!doc-source report] const report = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/report.pdf'); return { kind: 'bytes', id: 'report', bytes: new Uint8Array(await response.arrayBuffer()) }; }; // [!/doc-source] // The bundle as one JSON text: what you'd store in your own database. function Transfer() { const annotation = useAnnotation(); const ready = useAnnotationState((state) => state.status === 'ready'); const count = useAnnotationList().length; const [text, setText] = useState(''); const [status, setStatus] = useState(''); const started = useRef(false); const exportAll = async () => { const bundle = await annotation.export(); // everything setText(AnnotationTransfer.stringify(bundle)); setStatus(`Exported ${bundle.items.length}`); }; const deleteAll = async () => { await Promise.all(annotation.list().map((each) => annotation.delete(each.ref))); setStatus('Deleted them all'); }; const importText = async () => { try { const { annotations, dropped } = await annotation.import( await AnnotationTransfer.parse(text), ); setStatus(`Imported ${annotations.length}, left out ${dropped.length}`); } catch (error) { setStatus(error instanceof Error ? error.message : String(error)); } }; // On load: everything in the document, exported at once. useEffect(() => { if (!ready || started.current) return; started.current = true; void exportAll(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [ready]); return (
{status}