Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron PDF viewer

I have an Electron app that loads URL from PHP server. And the page contains an iFrame having a source to PDF. The PDF page seems absolutely ok in a normal web browser but asks for download in Electron. Any help?

My codes for html page is

<h1>Hello World!</h1>
Some html content here...
<iframe src="http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf" width="1200" height="800"></iframe>

And my js code is something like

mainWindow = new BrowserWindow({width: 800, height: 600})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))

app.on('ready', createWindow)

Any help would be really greatful...

like image 659
Hari Babu Avatar asked Apr 05 '17 01:04

Hari Babu


2 Answers

Electron is shipping already with an integrated PDF viewer.

So you can load PDF files just like normal HTML files, the PDF viewer will automatically show up.

E.g. in BrowserWindow with .loadURL(…), in <iframes>, <object> and also with the, at the moment discouraged, <webview>.

PS: The need to enable the plugins property in the BrowserWindow or <webview> is no more needed since Electron 9.

like image 98
flori Avatar answered Oct 18 '22 19:10

flori


You will need https://github.com/gerhardberger/electron-pdf-window

Example:

const { app } = require('electron')
const PDFWindow = require('electron-pdf-window')

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

win.loadURL('http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf')

})

like image 26
Jason Livesay Avatar answered Oct 18 '22 18:10

Jason Livesay