Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iframe not recognizing jsPDF output as pdf format

Tags:

pdf

jspdf

I'm having issues displaying jsPDF output as an actual pdf. I'm trying to mimic something similar to the official site - display a preview of the pdf and give them the option to download it.

With the code below, I managed to set a source for the iframe, but the iframe doesn't recognize the content as an actual pdf. So the browser (chrome/firefox) doesn't have the default pdf options when hovering over the iframe - ie: zoom in, zoom out, print, save, etc.

Is there any way to doc.output(...) the doc as an actual pdf? Is it a different option that I need to pass in? I've tried bloburl, bloburi and datauristring.

// html
<iframe></iframe>
<button>Display pdf</button

.

// js
$('button').on('click', function(){
    var doc = new jsPDF('l', 'pt', 'letter')
    doc.text(20, 20, 'some text' )

    setTimeout(function(){
        var data = doc.output('datauri')
        $('iframe').attr('src', data)
    }, 10)
})
like image 397
cat-t Avatar asked Mar 26 '15 17:03

cat-t


2 Answers

Change your iframe to have a type of "application/pdf".

<iframe type="application/pdf"></iframe>
like image 54
Hamer Avatar answered Oct 17 '22 14:10

Hamer


Html:

<iframe src="" id="order_print" type="application/pdf" class="d-none"></iframe>

Js:

$("#order_print").attr("src", doc.output('bloburl'))

I have used the above method for a long time and it worked perfectly in all Chrome. I didn't test on other browsers.

like image 23
Kavindu Pasan Kavithilaka Avatar answered Oct 17 '22 14:10

Kavindu Pasan Kavithilaka