6

Help me I am in desperate need for help. Now to the real issue.

I have been trying to get my pdf to open up in new tab in Safari but till now I've only faced disappointment. I am using jsPDF for generating my PDFs and earlier I used it's

doc.save(pdfName+".pdf")

method to do the same, as in the latest safari version "9.1.1(10601.6.17)" it broke. I can still generate the pdf using jsPDF's inbuilt option:

pdf.output('dataurl');

but, it tends to open the PDF in the same tab, and this behaviour is highly undesirable. I tried various solutions available on the net to fix the issue but none works. Some of the solutions that I tried are :

  1. Using window.open(), doesn't work.
  2. Using location.href, doesn't work.
  3. Using doc.output('save', 'filename.pdf'), doc.output('datauri'), doc.output('dataurlnewwindow'), nothing works.
  4. Also tried fake clicking by creating an anchor tag and setting it's target as '_blank' and then using eventDispatcher to stimulate a mouse click but it also didn't work. Something like this :

    var a = window.document.createElement("a");
    a.target = '_blank';
    a.href = 'http://www.google.com';
    
    var e = window.document.createEvent("MouseEvents");
    e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    a.dispatchEvent(e);
    

Any suggestion on what I should do ? I want my pdf to open up in a new tab and not in the same one as that of my application.

P.S : Each of the solutions that I tried for Safari above work flawlessly in Chrome and Mozilla Firefox, I just can't figure out what's the deal with Safari. Any help would be appreciated.

hkm93
  • 1,070
  • 8
  • 14

1 Answers1

-1

You could try the blob output. Then generate a Data URI to this, and open it in a new window:

var blob = pdf.output("blob");
window.open(URL.createObjectURL(blob));
demarchisd
  • 724
  • 3
  • 16
  • still, opens the pdf in the same Tab ; _ ; – hkm93 Jun 20 '16 at 13:09
  • that's weird.. if you try to open any other URL, does it still open in the same tab? Anyway, maybe using the second argument could be the solution: `window.open(URL.createObjectURL(blob), '_blank');` – demarchisd Jun 20 '16 at 13:18
  • didn't work, not being rude but went through tthe doccumentation of Apple and found our that the "The standard window.open() JavaScript method cannot be used to open a new tab and window from a global HTML file or an extension bar" https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html – hkm93 Jun 20 '16 at 13:40
  • yeah, sorry, now I got what the real problem is.. using your 4th attempt (faking a click event), were there any error messages in the console? [here](http://stackoverflow.com/a/2381862/2209617) is a very useful function for firing events – demarchisd Jun 20 '16 at 14:03
  • I'm sorry for the late reply, got sidetracked on a different project. Well, no there are no error messages in console, except for an 'undefined' that is printed, without any event. The same action results in 'undefined' printed in console + google opening in new tab while using Chrome. I'm sorry to say that neither Dispatcher not the FireEvent works. – hkm93 Jun 21 '16 at 11:05