user3700562
user3700562

Reputation: 827

Can't open PDF files in Electron

I followed the suggestions in the answers of related questions, i.e. specifying

webPreferences: {
      plugins: true
}

as part of the options when creating the BrowserWindow instance, but it is simply not working. Whenever I try to load/open/view a PDF file in electron, all I get is what looks like an empty/broken chromium PDF viewer like on this screenshot:

enter image description here

My electron version is "^13.1.2" according to my package.json and this is my main.js

// main.js

// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      plugins: true
    }
  });

  // and load the index.html of the app.
  win.loadFile('test.pdf');
}

app.whenReady().then(() => {
  createWindow()
})

app.on('window-all-closed', function () {
      app.quit()
})

Can anyone tell me what I'm doing wrong?

Upvotes: 2

Views: 1966

Answers (2)

Mäqs Wma
Mäqs Wma

Reputation: 92

An upgrade of electron helped me. i switched from 13.x to 16.x and it works now

Upvotes: 1

akaushik
akaushik

Reputation: 91

Use PDFWindow package

`//main.js

app.on('ready', () => { const win = new PDFWindow({ width: 800, height: 600 })

win.loadURL('test.pdf') })

//package.json

"dependencies": { "electron-pdf-window": "^1.0.12", },

`

Upvotes: 0

Related Questions