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

webContents.print() doesn't work under Windows #16792

Closed
gozzoo opened this issue Feb 6, 2019 · 3 comments
Closed

webContents.print() doesn't work under Windows #16792

gozzoo opened this issue Feb 6, 2019 · 3 comments

Comments

@gozzoo
Copy link

gozzoo commented Feb 6, 2019

  • Electron Version 4.0.4
  • Operating System Windows 10 32 bit
  • Last known working Electron version 3.1.3

Expected Behavior

webContents.print() shold send print job to a printer

Actual behavior

Nothing happens. Callback doesn't gets called.

printToPDF works though.

To Reproduce

let deviceName = 'HP LaserJet 1020'
let options = {deviceName, silent: true}
win.webContents.print({}, (err) => {
    if (!err)
      console.error('printing failed')
})
@gozzoo
Copy link
Author

gozzoo commented Feb 8, 2019

Here is the code for my testing app:

const { app, shell, BrowserWindow, Menu } = require('electron')
var path = require('path');
var fs = require('fs');

let win 

function createWindow () {
  win = new BrowserWindow({ width: 800, height: 600 })
  win.loadFile('note.html')
  win.webContents.openDevTools({mode: 'undocked'})
}

app.on('ready', createWindow)

const template = [
{ label: 'Print', submenu: [
    { label: 'Print PDF A4', click() { printToPDF('A4') } },
    { label: 'Print PDF A5', click() { printToPDF('A5') } },
    { label: 'Print to printer', click() { printToPrinter() } }
  ]
}
]
var menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu); 

function printToPrinter() {
  win.webContents.print({}, (err) => {
    if (!err)
      console.error('printing failed')
  })
}

function printToPDF(pageSize) {
  let options = {marginsType:2, pageSize, landscape: pageSize == 'A5'}
  win.webContents.printToPDF(options, printToPdfCallBack)
}

function printToPdfCallBack(error, data)  {
  if (error)
    return console.error(error)
  let userDataDir = app.getPath('userData')
  let pdf = path.join(userDataDir, 'note.pdf')
  fs.writeFile(pdf, data, (err) => {
    if (err)
      return console.error(err)
    shell.openExternal('file://' + pdf )
  })
}  

@Thomas101
Copy link
Contributor

We've also seen this on macOS.

There are two issues here...

  1. Silent is not initialized properly, meaning it's normally true
  2. When the arguments are sent from JavaScript to C, they're not serialized properly, meaning the C side has an array, rather two optional arguments of object and callback.

This has the side effect of it calling print with silent=true and no printer set which makes it fail silently. I've opened a PR to address both of these

@sofianguy
Copy link
Contributor

fixed: #17052

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

3 participants