Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Open pdf in new electron window

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?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda