Hello I’ve got a problem with my page, cuz I want to generate PDF file async with jquery, command:
JavaScript
x
4
1
$(".btn").click(function(){
2
$.get('/pl/home/generujPDF', function( data ) {
3
});
4
In my controller it looks somethink like this
JavaScript
1
11
11
1
function generujPDF()
2
{
3
require_once 'vendor/autoload.php';
4
5
6
$mpdf = new MpdfMpdf();
7
$mpdf->WriteHTML('<h1>Hello world!</h1>');
8
$mpdf->Output("mpdf.pdf", "D");exit;
9
10
}
11
And that doesn’t work, But if I type in URL /pl/home/generujPDF it just work fine. I check how it looks in “Network” tab in browser and I have 2 other types of this URL- … document (working fine) and xhr(it doesn’t work) Network Tab How to fix that, Thank you in advance 🙂
Advertisement
Answer
Try to use tag with download attribute and click programmatically.
JavaScript
1
8
1
<a href="/pl/home/generujPDF" download class="download-pdf"></a>
2
3
<script>
4
$(function() {
5
$(".download-pdf").click();
6
})
7
</script>
8
Maybe it works
EDIT
You can see more answers here: Download and open PDF file using Ajax