Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!