JsPDF - Not allowed to navigate top frame to data URL

Google ChromeFrameJspdf

Google Chrome Problem Overview


After updating Google Chrome, the report jsPDF in a new Window does not work any more.

The console shows the message:

> Not allowed to navigate top frame to data URL: > data:application/pdf;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1....

Can you help-me?

Thanks.

Google Chrome Solutions


Solution 1 - Google Chrome

This works well now that chrome has removed top frame navigation. Only downloading the pdf in chrome gives problem. Download works in well in firefox tho.

var string = doc.output('datauristring');
var iframe = "<iframe width='100%' height='100%' src='" + string + "'></iframe>"
var x = window.open();
x.document.open();
x.document.write(iframe);
x.document.close();

Solution 2 - Google Chrome

Apparently Google Chrome has removed support for top-frame navigation, you can see more informations here: https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/GbVcuwg_QjM

You may try to render the jsPDF to an iFrame

Solution 3 - Google Chrome

I recently had the same problem using FileReader object to read content and show my JSReport.

 var reader = new FileReader();                        
 reader.onload = function (e) {
      window.open(reader.result, "_blank");
 }
 reader.readAsDataURL(blob);

Unfortunatly after chrome update all my report stopped working. I tried to fix this by using Blob object and it's still working but if you have a popup blocker it will not work.

 var file = new Blob([blob], { type: 'application/pdf' });
 var fileURL = URL.createObjectURL(file);
 window.open(fileURL);

I finally find a way to avoid this problem by creating the iFrame dynamically after reading this topic and i decided to share the solution.

 var file = new Blob([blob], { type: 'application/pdf' });
 var fileURL = URL.createObjectURL(file);
 var win = window.open();
 win.document.write('<iframe src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>')

Solution 4 - Google Chrome

Maybe can help, create a function to export with the download attribute html5:

var docPdf = doc.output();
exportToFile(docPdf,defaults.type);

function exportToFile(data,type){
					
    var hiddenElement = document.createElement('a');
	hiddenElement.href = 'data:text/'+type+';filename='+'exportar.'+type+';'+'charset=utf-8,' + encodeURI(data);
	hiddenElement.target = '_blank';
	hiddenElement.download = 'exportar.'+type;
	hiddenElement.click();
}

Solution 5 - Google Chrome

<iframe id="ManualFrame"
        frameborder="0"
        style="border:0"
        allowfullscreen>
</iframe>

<script>
    $(function () {
        setManualFrame();
    });

    function setManualFrame() {
        $("#ManualFrame").attr("height", screen.height);
        $("#ManualFrame").attr("width", screen.width);
        $("#ManualFrame").attr("src", "data:application/pdf;base64," + Your_PDF_Data);
    }
</script>

Solution 6 - Google Chrome

var pdfUrl = doc.output('datauri').substring(doc.output('datauri').indexOf(',')+1);
var binary = atob(pdfUrl.replace(/\s/g, ''));
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
    view[i] = binary.charCodeAt(i);
}
       
var blob = new Blob( [view], { type: "application/pdf" });
var url = URL.createObjectURL(blob);

function openPDF(){
    window.open(url);
}

Solution 7 - Google Chrome

As chrome removed its support for - Top-frame navigation. Luckily jsPDF has an API - "save()", which offers the same functionality as doc.output('datauri')

Below is the example implementation of save()

var doc = new jsPDF();
doc.addImage(imgData, 'JPEG', 0, 0, 300, 160);    
doc.save('fileName.pdf');

save(filename, options) โ†’ {jsPDF|Promise}

Saves as PDF document. An alias of jsPDF.output('save', 'filename.pdf'). Uses FileSaver.js-method saveAs. Refer JSPDF documentation for more information -

Solution 8 - Google Chrome

the download property of the a element has to contain a file name.

function window_download( datauri )
{
  const match = datauri.match(/(filename=)([^;]+)/);
  const fileName = match ? match[2] : '' ;
  if ( fileName )
  {
    const downloadLink = document.createElement("a");
    downloadLink.download = fileName;
    downloadLink.innerHTML = "Download File";
    downloadLink.href = datauri ;
    downloadLink.click();
  }
else
{
throw 'missing download file name' ;
}


   // get contents of pdf from jsPDF as a data uri.
    const uri = pdf.output('datauristring', 'packing-list.pdf' );
    window_download( uri ) ;

Solution 9 - Google Chrome

@kuldeep-choudhary

Hi, in fact, to solve i'm using object tag with angularJS 1.5.xx

<object ng-attr-data="{{data}}" type="application/pdf"></object>

and in script:

$scope.doc.data = $sce.trustAsResourceUrl(doc.output("datauristring"));

In pure javascript, maybe like this works:

html:

<object id="obj" type="application/pdf" ></object>

js:

document.elementById('obj').data = doc.output("datauristring");

please, try and correct-me if I wrong.

Solution 10 - Google Chrome

Based on Joyston's answer, but without reparsing and therefore without additional need to escape something:

x=window.open();
iframe=x.document.createElement('iframe')
iframe.width='100%'
iframe.height='100%'
iframe.frameBorder=0
iframe.style="border: 0"
iframe.src='data:text/html........' //data-uri content here
x.document.body.appendChild(iframe);

Solution 11 - Google Chrome

/**
 * Creates an anchor element `<a></a>` with
 * the base64 pdf source and a filename with the
 * HTML5 `download` attribute then clicks on it.
 * @param  {string} pdf 
 * @return {void}     
 */
function downloadPDF(pdf) {
    const linkSource = `data:application/pdf;base64,${pdf}`;
    const downloadLink = document.createElement("a");
    const fileName = "vct_illustration.pdf";

    downloadLink.href = linkSource;
    downloadLink.download = fileName;
    downloadLink.click();
}

Source from: https://medium.com/octopus-labs-london/downloading-a-base-64-pdf-from-an-api-request-in-javascript-6b4c603515eb

Solution 12 - Google Chrome

Not related to jspdf, but did help me here (and this question is the top hit at google): If specifying a download="..." attribute to the anchor tag, the download prompt will properly open up.

Solution 13 - Google Chrome

Using download="" made me able to download the file

Solution 14 - Google Chrome

In angular2+ -

app.component.html -

 <object id="obj"  [attr.data]  type="application/pdf"> </object>

app.component.ts

 document.getElementById('obj').dataset.data = doc.output("datauristring");

  var blob = doc.output("blob");
  window.open(URL.createObjectURL(blob));

Solution 15 - Google Chrome

Add attrbute download

<a href="data:image/jpg;base64,/AA/AABBCCDD..." download="name.jpg"></a>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionM&#225;rcio RossatoView Question on Stackoverflow
Solution 1 - Google ChromeJoystonView Answer on Stackoverflow
Solution 2 - Google ChromePedro CalderonView Answer on Stackoverflow
Solution 3 - Google ChromeSadek LallouaniView Answer on Stackoverflow
Solution 4 - Google Chromenikoz84View Answer on Stackoverflow
Solution 5 - Google ChromeLonely PlaneteerView Answer on Stackoverflow
Solution 6 - Google ChromeSurya TView Answer on Stackoverflow
Solution 7 - Google ChromeSatheesh KView Answer on Stackoverflow
Solution 8 - Google ChromeRockBoroView Answer on Stackoverflow
Solution 9 - Google ChromeMárcio RossatoView Answer on Stackoverflow
Solution 10 - Google ChromeT SView Answer on Stackoverflow
Solution 11 - Google ChromePhuc DuongView Answer on Stackoverflow
Solution 12 - Google Chromephil294View Answer on Stackoverflow
Solution 13 - Google ChromeAfíf KraiemView Answer on Stackoverflow
Solution 14 - Google ChromeTechdiveView Answer on Stackoverflow
Solution 15 - Google ChromeJhonattanView Answer on Stackoverflow