最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I call an Electron API method from the renderer process? - Stack Overflow

programmeradmin2浏览0评论

I've been trying for a while. I followed the / and with relative success I managed to make an "app" using Bootstrap and Jquery.

But now, I'm trying to use Electron API methods but with no success.

I created a Browser Window, and within that Window I added a new JS file. Within that file, I'm trying to call printToPDF method here:

It just doesn't work and the console logs the following:

Uncaught ReferenceError: mainWindow is not defined

The code goes as this:

main.js

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
 mainWindow = new BrowserWindow({width: 800, height: 600})
 mainWindow.loadURL(`file://${__dirname}/index.html`)
 mainWindow.webContents.openDevTools()

 mainWindow.on('closed', function () {
  mainWindow = null
 })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
 if (process.platform !== 'darwin') {
  app.quit()
 }
})

app.on('activate', function () {
 if (mainWindow === null) {
  createWindow()
 }
})

index.html

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Hello World!</title>
 <link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
</head>
<body>
</body>

<script>window.$ = window.jQuery = require('jquery');</script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script>
 require('./app.js');
</script>
</html>

app.js

$(function() {
 mainWindow.webContents.printToPDF();
});

I've been trying http://electron.atom.io for a while. I followed the http://electron.atom.io/docs/tutorial/quick-start/ and with relative success I managed to make an "app" using Bootstrap and Jquery.

But now, I'm trying to use Electron API methods but with no success.

I created a Browser Window, and within that Window I added a new JS file. Within that file, I'm trying to call printToPDF method here: http://electron.atom.io/docs/api/web-contents/#contentsprinttopdfoptions-callback

It just doesn't work and the console logs the following:

Uncaught ReferenceError: mainWindow is not defined

The code goes as this:

main.js

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
 mainWindow = new BrowserWindow({width: 800, height: 600})
 mainWindow.loadURL(`file://${__dirname}/index.html`)
 mainWindow.webContents.openDevTools()

 mainWindow.on('closed', function () {
  mainWindow = null
 })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
 if (process.platform !== 'darwin') {
  app.quit()
 }
})

app.on('activate', function () {
 if (mainWindow === null) {
  createWindow()
 }
})

index.html

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Hello World!</title>
 <link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
</head>
<body>
</body>

<script>window.$ = window.jQuery = require('jquery');</script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script>
 require('./app.js');
</script>
</html>

app.js

$(function() {
 mainWindow.webContents.printToPDF();
});
Share Improve this question asked Sep 28, 2016 at 11:33 ricardoloboricardolobo 773 silver badges8 bronze badges 1
  • in your served html/js you don't have access to anything in main.js. You can make a function in app.js that emits an event, then in your main.js you can listen for that event and call mainWindow.webContents.printToPDF(); there when the event gets triggered. – Simon Hänisch Commented Sep 28, 2016 at 11:38
Add a ment  | 

1 Answer 1

Reset to default 5

Take a look at the ipc modules, ipcMain and ipcRenderer. The ipc module allows you to send and receive synchronous and asynchronous messages between the main and the render process.

Here the print to PDF example from the ELECTRON API DEMOS app.

Renderer Process

const ipc = require('electron').ipcRenderer

const printPDFBtn = document.getElementById('print-pdf')

printPDFBtn.addEventListener('click', function (event) {
  ipc.send('print-to-pdf')
})

ipc.on('wrote-pdf', function (event, path) {
  const message = `Wrote PDF to: ${path}`
  document.getElementById('pdf-path').innerHTML = message
})

Main Process

const fs = require('fs')
const os = require('os')
const path = require('path')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
const shell = electron.shell

ipc.on('print-to-pdf', function (event) {
  const pdfPath = path.join(os.tmpdir(), 'print.pdf')
  const win = BrowserWindow.fromWebContents(event.sender)
  // Use default printing options
  win.webContents.printToPDF({}, function (error, data) {
    if (error) throw error
    fs.writeFile(pdfPath, data, function (error) {
      if (error) {
        throw error
      }
      shell.openExternal('file://' + pdfPath)
      event.sender.send('wrote-pdf', pdfPath)
    })
  })
})
发布评论

评论列表(0)

  1. 暂无评论