I have an angular application that has some buttons for downloading PDF data to PC. I converted web app to desktop app with electron. But the functionality for opening documents in new tabs (window.open(url, "_blank");) don't work with electron.
I tried with mainWindow.webContents.setWindowOpenHandler(...) but I this will only open blank window and dialog with where to save the file...
The closes I came to displaying a new window with electron with PDF inside is using the code below:
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
my_url = app.getPath("temp") + "/" + item.getFilename();
log.info(`item: ${item.getFilename()}\tURL: ${my_url}`);
item.setSavePath(my_url);
item.once('done', (event, state) => {
let subWindow = new BrowserWindow({
title: item.getFilename(),
x: width - mWidth - 3,
y: height - mHeight - 3,
width: mWidth,
height: mHeight,
minWidth: mWidth,
minHeight: mHeight,
useContentSize: false,
icon: __dirname + '/src/logo.ico',
minimizable: false,
maximizable: true,
fullscreen: false,
frame: true,
resizable: true,
movable: true,
alwaysOnTop: false,
webPreferences: {
nodeIntegration: true,
zoomFactor: 1.0
}
});
subWindow.removeMenu(null);
subWindow.loadURL(my_url);
});
});
So this basically saves the document to user's temp folder and then displays it inside a window, but this why it creates 2 windows, 1 empty and 1 with the downloaded file.
My question is, how could I open my document inside a new window?
After some time, I found a solution that works fine for now:
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
my_url = app.getPath("temp") + "/" + item.getFilename();
log.info(`item: ${item.getFilename()}\tURL: ${my_url}`);
item.setSavePath(my_url);
item.once('done', (event, state) => {
let w = BrowserWindow.getAllWindows().reduce((max, current) => max.id > current.id = max : current);
w.loadUrl(my_url);
});
});
Basically, main window gets the ID value 1 and every other window created will be n+1. So with this I will put PDF data to the last window created.