0

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.

Bernhard
  • 686
  • 8
  • 20

1 Answers1

0

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

Bernhard
  • 686
  • 8
  • 20
  • I seem to be getting this error when I try run it in firefox, any idea why? ERROR DOMException [InvalidCharacterError: "String contains an invalid character" code: 5 nsresult: 0x80530005 location: http://localhost:4200/vendor.bundle.js:7133] –  Aug 14 '17 at 09:44
  • Could you try with a minimal PDF in the datauri string? Maybe you are on windows and your filename contains a backslash from the filename path. – Bernhard Aug 15 '17 at 09:59
  • It appears to be caused if you try and add an image using addImage. in my case doc.addImage(Data, 'JPEG', 115, 35, 80, 80) Data being the data uri string of the image. –  Aug 17 '17 at 09:21
  • In case anyone has this issue, the cause was caused by a quotation mark at the end of the data uri and couldnt be removed using a substring, I solved this by using this.URI = this.URI.replace('"', ""); to remove it. –  Aug 17 '17 at 10:01
  • Had any luck with Edge? –  Aug 18 '17 at 07:37
  • No, sorry; Edge does not display the embedded PDF; I do not know why. I tried to see if there is an extension required, but had no success. Anyhow, Edge is not among the Browsers supported by Zupfnoter, so I do not care too much. But if there would be something, I could do, i would. So hints are welcome. – Bernhard Aug 18 '17 at 10:14