firefox hangs when displaying a pdf via data url

被刻印的时光 ゝ 提交于 2019-12-13 07:36:06

问题


I have an app which creates pdf in the browser using jspdf. I want to show this pdf in another tab/window.

function open_data_uri_window(url) {
   var html = '<html>' +
     '<style>html, body { padding: 0; margin: 0; } iframe { width: 100%; height: 100%; border: 0;}  </style>' +
    '<body>' +
    '<p>new viewer</p>' +
    '<iframe type="application/pdf" src="' + url + '"></iframe>' +
    '</body></html>';

  var a = window.open("about:blank", "Zupfnoter");
  a.document.write(html);
  a.document.close();
}

It works fine in chrome (60.0.3112.90) but not on Firefox (54.0.1 64 bit MacOs). There the window hangs.


回答1:


Reason is that PDF.js (line 331) tries to extract a suggested filename from the url. This regular expression hangs depending on the dataurl.

Solution is to specify a name in the datauri string, so that an accepted name is always found, as mentioned in comment 1 of Is there any way to specify a suggested filename when using data: URI?

function open_data_uri_window(url) {
   var url_with_name = url.replace("data:application/pdf;", "data:application/pdf;name=myname.pdf;")
   var html = '<html>' +
    '<style>html, body { padding: 0; margin: 0; } iframe { width: 100%; height: 100%; border: 0;}  </style>' +
    '<body>' +
    '<p>new viewer</p>' +
    '<iframe type="application/pdf" src="' + url_with_name + '"></iframe>' +
    '</body></html>';
    var a = window.open("about:blank", "Zupfnoter");
    a.document.write(html);
    a.document.close();
}

Hint: The specified name is not respected by Chrome's builtin viewer



来源:https://stackoverflow.com/questions/45585921/firefox-hangs-when-displaying-a-pdf-via-data-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!