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

print job is stuck at Spooling when using webContents.print() #22695

Closed
SmellydogCoding opened this issue Mar 15, 2020 · 9 comments
Closed

print job is stuck at Spooling when using webContents.print() #22695

SmellydogCoding opened this issue Mar 15, 2020 · 9 comments

Comments

@SmellydogCoding
Copy link

Issue Details

  • Electron Version:
    • 9.0.0-beta-7
  • Operating System:
    • Windows 10 Version 2004 Build 19041.153
  • Last Known Working Electron version:
    • unknown

Expected Behavior

Using webContents.print() will invoke the Windows print dialog which will print the contents of the BrowserWindow.

Actual Behavior

The Windows dialog box does appear, but after clicking Print the print job is stuck at Spooling.
screenshot: https://1drv.ms/u/s!AuHiR7uxGtQWgfxRefLP6MkcrwsQmQ?e=ZrQwP1

Code

ipcMain.on('print', (event, file) => {
    // create new window
    let printWindow = new BrowserWindow({ width: 800, height: 600, show: false })
    // load the file (URL in development, file path in production).
    printWindow.loadURL(file)
    // make sure the file is loaded and the window is ready before continuing
    let p1 = new Promise((resolve) => printWindow.webContents.on('did-finish-load', resolve))
    let p2 = new Promise((resolve) => printWindow.on('ready-to-show', resolve))
    Promise.all([p1, p2])
      .then(() => {
        console.log('resolved') // did both promises reslove?
        printWindow.webContents.print({ printBackground: true }, (success, error) => {
          console.log('print function invoked') // was the print function called?
          if (success) {
            console.log('success')
            printWindow = null
          } else if (error) {
            console.log(error)
          } else { console.log('unknown') }
        })
      })
  })

Additional Information

When this runs 'resolved' is printed to the console but 'print function invoked' is not. I can't tell if webContents.print() is firing at all since nothing in the callback seems to run.

@t57ser
Copy link
Contributor

t57ser commented Mar 17, 2020

I have experienced this too, it seems to happen if there is an iframe with a cross-domain src on the page. Does your page include an iframe too?

@SmellydogCoding
Copy link
Author

The print window is opening the pdf using chromium's built in pdf viewer. It doesn't use an iframe but it does use an embed:

<embed name="E9D59870AA124573B4DD3044D575490A" style="position:absolute; left: 0; top: 0;"
 width="100%" height="100%" src="about:blank" type="application/pdf"
 internalid="E9D59870AA124573B4DD3044D575490A">

The source is in the same domain:

http://localhost:8080/7a8cfb16a78903084dc0ba5003b4e6cd.pdf

In production the file would be on the local machine

@sofianguy sofianguy added this to Unsorted Issues in 9-x-y Mar 18, 2020
@sofianguy sofianguy moved this from Unsorted Issues to Does Not Block Stable in 9-x-y Mar 18, 2020
@markleutloff
Copy link

markleutloff commented Nov 27, 2020

This behaves like #20634.

Issuing a print job through either webContents.print, webContents.printToPdf or window.print when the page contains the pdf viewer extension will in the current versions of Electron never finish the print job.

Tested with Electron (Windows 10 x64):
11.0.3
12.0.0-beta.3
12.0.0-beta.4

@mikkorantalainen
Copy link

If the document to print contains Youtube iframe embed, is there any known workaround to be able to get the printToPDF() to not hang? I would want something else but setting display:none to all <iframe> elements.

@codebytere
Copy link
Member

Closing in favor of #27605

@ashishgehlot
Copy link

I am still facing this issue with Electron 18.3.1.
Attaching minimal reproduction repo.

https://github.com/ashishgehlot/Electron_PrintStuck.git

@MarcoGiorgi
Copy link

I confirm the bug is still there. printToPDF using webContents stuck at pooling.

  let win: any = new BrowserWindow({ show: false });

  win.loadURL(args.url);

  win.webContents.once('did-finish-load', () => {
    win.webContents
      .printToPDF({ printBackground: true, silent: true })
      .then((data: any) => {
        const buf = Buffer.from(data).toString('base64');
        const url = `data:application/pdf;base64,${buf}`;

        // @ts-ignore
        win.webContents.on('ready-to-show', () => {
          if (process.env.NODE_ENV === 'development') win.show();
          win.webContents.print(
            { printBackground: true, silent: true },
            (success, failureReason) => {
              console.log('Print Initiated in Main...');
              if (!success) console.log('[print][failed]', failureReason);
            }
          );
        });

        // @ts-ignore
        win.webContents.on('closed', () => {
          win = null;
        });

        return win.loadURL(url);
      })
      .catch((error) => {
        console.log(error);
      });
  });

@sassouiabd
Copy link

sassouiabd commented Aug 30, 2022

Hello,
I am also still facing this issue when trying to print using webContents.print(.., callback).
The print job got stuck in spooling status and the callback inside the print method is never called.
Has any one found a solution or workaround to this issue?
@codebytere i have tried the others PRs you have listed like:
#27605
#33654
but it didn't solve this issue.

I am using Electron version V.18.0.1
I have also tried with the latest version V.20.0.1 (same issue)

  const win = new BrowserWindow({width: 800, height: 600, show: false });
  win.loadURL("file://" + filePath);
  win.webContents.on("did-finish-load", () => {
    win.webContents.print(
      {silent: true},
      (success, failureReason) => {
        if (success) {
          console.log("Printer Initiated");
        } else {
          throw new Error(failureReason);
        }
      }
    );
  });

image

@priyeshshah11
Copy link

Closing in favor of #27605

@codebytere I don't think this issue is the same as the one referenced in your comment.
Either way this bug still seems to exist (at least on windows), does anyone know the root cause of this? Or any solutions?

Thus, this issue should be re-opened and investigated further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
9-x-y
Does Not Block Stable
Development

No branches or pull requests