seanchas116 / electron-pdfjs

Electron + PDF.js viewer example
32 stars 11 forks source link

How to use it in my app? #1

Open BesatZardosht opened 8 years ago

BesatZardosht commented 8 years ago

I am trying to open a pdf in my Electron app. I have tried almost everything. but I am confused. I basically want to click on a button and open a pdf viewer as you have in your example. But I cannot figure it out. would you please help me with this?

Gillian-D commented 8 years ago

I've got more and less the same problem. I take the example, I try to edit it for my own app, in vain. First, the pdf called "compressed.tracemonkey-pldi-09.pdf" is not displayed correctly in Electron. No text or illisible text, empty pages, only graphics are loaded. In one word : awful result and I don't know why. See the link below :

capture d ecran 2016-06-21 a 14 42 50

Then I try to change this loaded pdf by mine. I put the file in the good directory, I changed the path in the viewer.js file. But I think it's not as simple as that... I don't know what to do to replace the default pdf by another one.

Also, I would like to ask if it was possible to display the pdf viewer only in a "div" on the right side of the app window ? Because I would like to keep a menu on the left side.

Thank you very much for your attention.

Mercieral commented 8 years ago

It's funny I actually just came across this as well, at the moment it is working flawlessly for me (a little laggy but I'm not complaining). The guy who made this really should put something in his README on how we go about using this.

Here's my snippet (in the render process in a function that is called when the 'preview button' is pressed) I'm using https URL's and they are working. The trickiest part was getting that modalPath correct.

export function previewPdf(url) {
    const BrowserWindow = require(`electron`).remote.BrowserWindow;
    const path = require(`path`);
    const qs = require(`query-string`);

    const param = qs.stringify({file: url});
    const modalPath = path.join('file://' + __dirname + '/pdfjs/web/viewer.html?' + param);

    let pdfWindow = new BrowserWindow({
        width: 600,
        height: 1000,
        webPreferences: {
            nodeIntegration: false, 
            webSecurity: false
        }
    });

    pdfWindow.on('closed', function () { pdfWindow = null });
    pdfWindow.loadURL(modalPath);
    pdfWindow.show();
}

I get this with all of the features working as expected. pdf_viewer