import { useEffect, useState } from 'react';
import { Viewer, DocumentGate, saveFile, useDocuments } from '@embedpdf/react/runtime';
import type { OpenInput, PdfSaveMode } from '@embedpdf/react/runtime';
import { Stage, stagePlugin } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import { cloudEngine } from '@cloudpdf/engine';
import './download.css';
const engine = cloudEngine();
const plugins = [stagePlugin(), renderPlugin()];
const welcome: OpenInput = { kind: 'grant', grant: 'shr_gvpJcG1ULUMon19C0D1wODSY' };
const megabytes = (bytes: Uint8Array) => `${(bytes.byteLength / 1_000_000).toFixed(2)} MB`;
function DownloadBar() {
const documents = useDocuments();
const [mode, setMode] = useState('incremental');
const [sizes, setSizes] = useState | null>(null);
// How big each kind of download is: the same document, written two ways.
useEffect(() => {
const cancel = new AbortController();
const { signal } = cancel;
Promise.all([
documents.download(undefined, { signal }),
documents.download(undefined, { mode: 'rewrite', signal }),
])
.then(([incremental, rewrite]) =>
setSizes({ incremental: megabytes(incremental), rewrite: megabytes(rewrite) }),
)
.catch(() => {}); // cancelled: the example went away
return () => cancel.abort();
}, [documents]);
const download = async () =>
saveFile(await documents.download(undefined, { mode }), 'welcome.pdf');
return (
);
}
export default function App() {
return (
Opening…
}>
{() => }
);
}