Skip to content

Is there other way to create pdf in php?

Hello I’ve got a problem with my page, cuz I want to generate PDF file async with jquery, command:

$(".btn").click(function(){
   $.get('/pl/home/generujPDF', function( data ) {      
});

In my controller it looks somethink like this

function generujPDF()
{
    require_once 'vendor/autoload.php';
    
    
    $mpdf = new MpdfMpdf();
    $mpdf->WriteHTML('<h1>Hello world!</h1>');
    $mpdf->Output("mpdf.pdf", "D");exit;

}

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 🙂

Answer

Try to use tag with download attribute and click programmatically.

<a href="/pl/home/generujPDF" download class="download-pdf"></a>

<script>
$(function() {
    $(".download-pdf").click();
})
</script>

Maybe it works

EDIT

You can see more answers here: Download and open PDF file using Ajax