Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: webContents.print() fails on macOS with failureReason "no valid printers available" #28192

Closed
3 tasks done
caffeinatedbits opened this issue Mar 15, 2021 · 4 comments
Closed
3 tasks done

Comments

@caffeinatedbits
Copy link

Preflight Checklist

Electron Version

12.0.1

What operating system are you using?

macOS

Operating System Version

macOS Mojave 10.14.6

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

Calling webContents.print() should show the system print dialog.

Actual Behavior

Calling webContents.print() returns an error (via its callback, argument 2, failureReason) "no valid printers available" because I have no printers installed.

Testcase Gist URL

No response

I have a BrowserWindow which contains "print" button, and multiple BrowserViews. I tried calling view.webContents.print() (where view == the currently active BrowserView) when the print button is clicked, however, because I have no printers installed, the callback is immediately called with success == false and failureReason == "no valid printers available".

I am aware of the following issues which have been closed but not fixed:

#25498
#24664

In the meantime, as a workaround, I am sending a message to the main script using ipcRenderer in the BrowserWindow preload script like so:

const { ipcRenderer } = require('electron');
ipcRenderer.send("print");

In the main script, I am listening for that message using ipcMain, and then redirecting it to the appropriate BrowserView using view.webContents.send() like so:

const { ipcMain } = require('electron');
let currentBrowserView; // stores reference to the currently active BrowserView
ipcMain.on("print", (event) => {
    currentBrowserView.webContents.send("print");
});

Finally, in the preload script for the BrowserView, I am listening for that message using ipcRenderer, and then calling window.print() like so:

const { ipcRenderer } = require('electron');
ipcRenderer.on("print", (event) => {
    window.print();
});

This gets me the system print dialog which, despite not having any installed printers, offers me additional macOS features such as: Open in Preview, Save as PDF, Save as PostScript, Send in Mail, Add to Apple Books, Send via Messages, Save to iCloud Drive, Save to Web Receipts, and Save to Adobe PDF. As I do not have a printer installed, all of these other options are the only way I have to achieve "print" capability.

Thus, I believe that BrowserView.webContents.print() should do the same thing as calling window.print() from within that BrowserView: show me my system print dialog even though I have no physical printers installed.

@CBMasri
Copy link

CBMasri commented Jun 11, 2021

Alternative workaround without IPC:

webContents.executeJavaScript('window.print()')

@bobber205
Copy link

@CBMasri This doesn't allow options like webContents.print does -- do you know of any workaround?

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

@github-actions github-actions bot added the stale label Oct 6, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2022

This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants