examples icon indicating copy to clipboard operation
examples copied to clipboard

Feature Request: Allow printing PDF without rendering <Viewer /> into the DOM

Open rahitecho opened this issue 6 months ago • 0 comments
trafficstars

Hi, first of all—thanks for the great library! I'm using @react-pdf-viewer/print along with @react-pdf-viewer/core and everything works smoothly when rendering the PDF using the <Viewer /> component.

However, I'd like to request a feature that allows printing a PDF without rendering it into the DOM using <Viewer />.

❓Use Case: I have a use case where I want to programmatically print a PDF (e.g., shipping label) without showing it to the user in the interface. Currently, the printPlugin().print() method only works after <Viewer /> has been rendered and loaded into the DOM. If Viewer is not mounted, print() doesn't do anything.

🧩 What I tried: const printPluginInstance = printPlugin(); const { print } = printPluginInstance; print(); // Doesn’t work unless <Viewer /> is mounted

🛠️ Feature Suggestion: Expose a method in the printPlugin that allows printing a PDF from a URL directly, without requiring <Viewer /> to be rendered. Example API idea:

const printPluginInstance = printPlugin(); await printPluginInstance.printFromUrl(pdfUrl);

This could internally:

Use PDF.js to load the file

Render pages into an off-screen canvas or hidden iframe

Trigger the browser's print dialog

🔄 Workaround: Right now, the only way to achieve this is by manually creating an iframe with the PDF URL and triggering iframe.contentWindow.print(), but this approach:

Has cross-origin limitations

Doesn’t allow control over page size or print layout

Doesn't benefit from the plugin’s customization options

✅ Expected Outcome: Ability to print a PDF directly via plugin, without visually rendering it.

Works in headless or minimized UI workflows (e.g., batch label printing).

Let me know if this is feasible or if there’s any recommended workaround using existing APIs!

Thanks again for your great work 🙏

rahitecho avatar May 21 '25 05:05 rahitecho