使用外部应用程序打开Electron中打包的PDF

时间:2019-04-08 08:37:13

标签: reactjs pdf electron create-react-app electron-builder

我们想在我们的应用程序中分发一组PDF文件。最初,我们是使用React PDF查看器打开这些文件的,但是用户确实需要能够将这些文件保存到他们的计算机中,以便他们可以在自己的时间读取/注释/打印这些文件。

我们正在Electron中使用Create React App-您有什么建议吗?

谢谢!

设法使用以下方法使其工作:

PDFLink= (e)=> { // Open PDF with `shell` method
    console.log("Clicked PDF: ");
    var attrs=e.currentTarget.attributes;
    const shell = window.require('electron').shell;
    const remote = window.require('electron').remote;
    const appPath = remote.app.getAppPath();
    console.log('appPath: ', appPath);

    for (var a=0;a<attrs.length;a++)
    {
      console.log(attrs[a].name+"="+attrs[a].value);
      switch (attrs[a].name)
      {
        case 'data-pdf':
        //console.log(app.getAppPath());
        shell.openItem(appPath+'\\public\\pages\\test1.pdf');

          break;

        default:
          break;
      }
    }

  }

1 个答案:

答案 0 :(得分:1)

If the PDFs are shipped with your app, you could just use the shell module to open them: shell.openItem(fullPath).

shell

Manage files and URLs using their default applications.

shell.openItem(fullPath)

Returns Boolean - Whether the item was successfully opened

Open the given file in the desktop's default manner.