import {
PDFMergeSortSplitClass,
PDFMergeSortSplit,
usePdfMergeSortSplit,
} from 'pdfmergesortsplit';
The React helpers live in the pdfmergesortsplit/react entry point.
new PDFMergeSortSplitClass(container, config?)
Creates the iframe inside container (an element or a selector) and starts the
handshake. Throws when the container does not exist.
| Member | Description |
|---|---|
whenReady() |
Resolves when the editor took the configuration; rejects after readyTimeout. |
loadDocument(options) / loadDocument(name, content) |
Adds a PDF. content is a File, Blob, ArrayBuffer, Uint8Array, base64 string or data URL. Resolves when the editor took it. |
getPages() |
The pages with their provenance — see Page provenance. |
exportDocument(reference?, mode?) |
Builds a PDF (mode: "pdf", default) or a ZIP of all parts (mode: "zip") and resolves with a Blob. reference is a part index or a part name; without it all pages go into one document. |
on(event, handler) |
Subscribes; returns an unsubscribe function. |
off(event, handler) |
Removes a handler. |
destroy() |
Removes the iframe, stops listening and rejects pending requests. |
send(message) |
Escape hatch: posts a raw message to the editor. |
request(op, responseOp, payload?) |
Escape hatch: a request that resolves with the editor's answer. |
iframe |
The <iframe> element. |
loadDocument
await editor.loadDocument({ name: 'contract.pdf', content: file });
// one provenance entry per page (any JSON), e.g. from your own records
await editor.loadDocument({
name: 'merged-2024.pdf',
content: arrayBuffer,
provenance: [
{ source: 'a.pdf', page: 3, case: '2024-117' },
{ source: 'b.pdf', page: 1, case: '2024-118' },
],
});
Without provenance every page is recorded as {source: name, page: n}.
exportDocument
await editor.exportDocument(); // all pages → one PDF (Blob)
await editor.exportDocument(0); // part 0 → PDF
await editor.exportDocument('Invoice'); // the part named "Invoice" → PDF
await editor.exportDocument(undefined, 'zip'); // every part → one ZIP
Rejects with an Error when the editor reports a problem (for example when
there is nothing to export).
Events
| Event | Payload | Fires when |
|---|---|---|
action |
{action, where, reference, part} |
Someone clicked a button you configured in actions. where is loaders, saveAll or savePart; reference is the part name and part its index for savePart. |
export |
{reference, mode, fileName, pages, blob} |
exportDocument finished — same data as its result, plus the suggested file name. |
license |
{licensed, reason} |
The editor accepted or rejected the licence, including the initial no licence presented. |
editor.on('action', async ({ action, where, part }) => {
if (action === 'save') {
download(await editor.exportDocument(where === 'saveAll' ? undefined : part));
}
});
React
import { PDFMergeSortSplit, usePdfMergeSortSplit } from 'pdfmergesortsplit/react';
function App() {
const { ref, loadDocument, exportDocument, getPages } = usePdfMergeSortSplit();
return (
<PDFMergeSortSplit
ref={ref}
config={{ apiKey: '<licence JWT>' }}
style={{ height: 640 }}
onAction={({ action }) => console.log(action)}
onExport={({ blob, fileName }) => console.log(fileName, blob.size)}
onLicense={({ licensed, reason }) => console.log(licensed, reason)}
onReady={(editor) => console.log('ready', editor)}
/>
);
}
usePdfMergeSortSplit() returns {ref, loadDocument, exportDocument, getPages},
all bound to the instance the component created. The component renders a
full-size <div> (override with style/className). Callbacks may change on
every render without reloading the iframe; the config is read once, so
remount with a key to change it.
Types
The package ships TypeScript declarations (types/index.d.ts,
types/react.d.ts): PDFMergeConfig, PDFMergeActionEvent,
PDFMergeExportEvent, PDFMergeLicenseEvent, PDFMergePage.