Skip to content

Instantly share code, notes, and snippets.

@softtama
Created April 6, 2017 08:01
Show Gist options
  • Save softtama/465425d714df63408d9bbadca73fa69c to your computer and use it in GitHub Desktop.
Save softtama/465425d714df63408d9bbadca73fa69c to your computer and use it in GitHub Desktop.
Get PDF with jQuery AJAX and show it inside an iframe
/**
* Get PDF with jQuery AJAX and show it inside an iframe.
*
* Using mPDF as PDF generator in PHP, set the output mode to inline:
* $mpdf->Output('I'); // where $mpdf is an instance of mPDF
*
* HTML demo:
* <button id="btn-generate-pdf" class="btn btn-primary">Generate PDF</button>
* <hr>
* <div id="div-iframe"></div>
*/
var pdf_url = 'http://example.com/generate_pdf';
var div_iframe = $('#div-iframe');
div_iframe.hide();
$(document).ready(function () {
$('#btn-generate-pdf').click(function () {
div_iframe.hide();
if ($('#iframe-pdf').length > 0) {
$('#iframe-pdf').remove();
}
var btn_generate_pdf = $('#btn-generate-pdf');
var old_text = btn_generate_pdf.text();
btn_generate_pdf.text('Generating...').addClass('disabled').css('pointer-events', 'none');
$.ajax({
url: '',
type: 'GET',
processData: false,
xhrFields: { withCredentials: true },
success: function () {
var iframe = $('<iframe id="iframe-pdf" class="iframe-pdf" width="100%" ' +
'height="800" frameborder="0"></iframe>');
iframe.attr('src', pdf_url);
iframe.load(function () {
btn_generate_pdf.text(old_text).removeClass('disabled').css('pointer-events', '');
div_iframe.show();
});
div_iframe.append(iframe);
}
});
});
});
@fhuo-ch
Copy link

fhuo-ch commented Nov 14, 2019

Hi
do you have also the PHP part for this request?
Best regards
Freddy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment