From c92b5d92f557589fe769a6c4aac59e1e2de2eedc Mon Sep 17 00:00:00 2001 From: Quint Guvernator Date: Tue, 30 Jan 2024 18:24:23 +0100 Subject: [PATCH] expose open method via postMessage --- web/viewer.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/web/viewer.js b/web/viewer.js index ff40485c37d7c4..413822b5c66e8f 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -206,6 +206,24 @@ function webViewerLoad() { } } PDFViewerApplication.run(config); + window.addEventListener('message', handlePostMessage); +} + +const postMessageHandlers = { + open(msg) { + const { pdfData } = msg; + PDFViewerApplication.open({ data: pdfData }) + } +}; + +function handlePostMessage(event) { + const { type, msg } = event.data; + const handler = postMessageHandlers[type]; + if (!handler) { + console.error(`pdf.js: no handler for postMessage with type ${type}`) + return; + } + return handler(msg); } // Block the "load" event until all pages are loaded, to ensure that printing