Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda