firefox hangs when displaying a pdf via data url

2019-08-29 04:45发布

问题:

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