因此,我有了这个Electron应用程序,并且在一个.html文件中,我链接了另一个脚本,该脚本为该程序提供了一些实用程序功能,其中一个是这个:
function openPDF(filePath){
let pdfWindow = new electron.remote.BrowserWindow({
icon: './build/icon.png',
width: 1200,
height: 800,
webPreferences: {
plugins: true
}
});
pdfWindow.loadURL(url.format({
pathname: filePath,
protocol: 'file:',
slashes: true
}));
pdfWindow.setMenu(null);
pdfWindow.on("closed", function () {
pdfWindow = null
});
}
因此,这应该使用集成的 Electron PDF查看器(使用Chromium)在新窗口中打开PDF。我使用了臭名昭著的
plugins: true
,我尝试了大多数的thousands of preferences you can define for a BrowserWindow,但是它总是打开窗口,然后开始下载文件而不显示它。我三重检查了文件路径,“导入”等,更新了所有内容,但似乎找不到问题。自1.6.4起,Electron本身就支持此功能,但它对我不起作用。
帮我,Stack Overflow,您是我唯一的希望。
最佳答案
2020年更新:
@karthick正确指出,尽管有plugins: true
,这是一个禁用插件的错误。 The Issue存在since 3.0.0(2018年9月18日),今天仍然需要修复。终于在版本9中得到修复!
使用以下命令将 Electron 版本更新为9.X.X或更高版本以启用功能:
npm update electron
您可以在项目文件夹中的devDependencies
中检查package.json
。它看起来应该像这样:"devDependencies": {
"electron": "^9.0.0"
},
旧答案:
由于长期存在的GitHub问题趋于令人困惑,因此我将根据开发的要点来更新此答案。您还可以在答案末尾找到三种解决方法。
更新:
等待better extension support。
开发人员:
解决方法:
npm install electron@"<3.0.0" --save-dev
但是请记住,only the latest three stable major versions are supported by the Electron team意味着 2.X.X不再接收安全补丁。
shell.openItem(fullPath);
只需确保始终使用
fullPath
之类的东西正确解析路径(path.resolve(app.getAppPath(), filePath)
),因为在构建应用程序时它可能会更改。 const { BrowserWindow, session } = require('electron')
session.defaultSession.on('will-download', (event, item, webContents) => {
if (item.getMimeType() === 'application/pdf' && item.getURL().indexOf('blob:file:') != 0) {
event.preventDefault();
new BrowserWindow().loadFile(path.resolve(__dirname, `pdfjs/web/viewer.html?file=${item.getURL()}`));
}
})
关于javascript - 如何在 Electron 浏览器窗口中查看PDF?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52844135/