The PDF/ODT/ODP embedded viewer based on ViewerJS for Electron.
$ npm i --save electron-viewerjs
Enable webviewTag
in main.js
:
mainWindow = new BrowserWindow({
...
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
webviewTag: true //Enable webviewTag
}
...
})
Declare container in index.html
<div id="sample-viewer-1" style="width: 100%; height: 100%;"></div>
<div id="sample-viewer-2" data-src="./sample-pdf2-doc.pdf" style="width: 100%; height: 100%;"></div>
Declare viewer
in renderer.js
const viewer = require('electron-viewerjs');
There are 3 ways of initialize
- Load file at constructor
let ctx = document.getElementById('sample-viewer-1');
viewer.new(ctx, {path: './sample-pdf1-doc.pdf'});
- Load file by attribute
data-src="..."
let ctx = document.getElementById('sample-viewer-2');
viewer.new(ctx); //No need to put path here.
- Load file after initialized
let ctx = document.getElementById('sample-viewer-1');
let preview = viewer.new(ctx);
preview.loadSync('./sample-odt-doc.odt'); //Load ODT document file after initialized.