首页 > 解决方案 > 在打开窗口的情况下打开 pdf 在 Chrome 中不起作用

问题描述

我正在使用下面的代码在 window.open 中打开 pdf 以获取 pdf 的动态标题。

此代码工作正常,但在 Chrome 浏览器中无法正常工作。

我在打开时看到 pdf 的标题,但 pdf 没有打开。

function titlepath(path,name)
{
  var prntWin = window.open();
  prntWin.document.write("<html><head><title>"+name+"</title></head><body>"
  + '<embed width="100%" height="100%" name="plugin" src="'+path+'" '
  + 'type="application/pdf" internalinstanceid="21"></body></html>');
  prntWin.document.close();
}

标签: javascriptpdf

解决方案


假设相同的协议和可能相同的来源

注意:假设 PDF 也来自网站,因为您可能会遇到从文件系统加载的问题

注意:如果服务器发送x-frame-options,您可能无法显示它

尝试 iFrame

function titlepath(path,name) {
  var prntWin = window.open("");
  prntWin.document.write("<html><head><title>"+name+"</title></head><body>" + 
  '<iframe width="100%" height="100%" name="plugin" src="'+path+'"></iframe></body></html>');
  prntWin.document.close();
}

或至少关闭嵌入

function titlepath(path,name) {
  var prntWin = window.open("");
  prntWin.document.write("<html><head><title>"+name+"</title></head><body>" +
  '<embed width="100%" height="100%" name="plugin" src="'+path+'"></embed>'+
  '</body></html>');
  prntWin.document.close();
}

推荐阅读