JsPDF - Not allowed to navigate top frame to data URL

后端 未结 14 2152
Happy的楠姐
Happy的楠姐 2020-11-27 02:53

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

相关标签:
14条回答
  • 2020-11-27 03:35

    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));
    
    0 讨论(0)
  • 2020-11-27 03:38

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

    0 讨论(0)
  • 2020-11-27 03:42

    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();
    
    0 讨论(0)
  • 2020-11-27 03:42
    <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>
    
    0 讨论(0)
  • 2020-11-27 03:42
    /**
     * 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

    0 讨论(0)
  • 2020-11-27 03:43

    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.

    0 讨论(0)
提交回复
热议问题