Use bootstrap style to generate pdf file with php

Asked

Viewed 4,614 times

3

Is there any way to use bootstrap to style an html page that will later become a pdf? I’m using the mpdf60 plugin in php, and I only need the style boostrap offers to use it in exporting a pdf file.

Consider the following code:

    <div class="row"><div class="col-lg-4"></div><div class="col-lg-4">
    <h3>Teste PDF</h3></div><div class="col-lg-4"></div></div>

When I use it inside the $mpdf->Writehtml() function, the bootstrap style does not take, coming only html, or any inline css. Using this mpdf60 plugin or any other, there is some way I can use bootstrap as css?

  • 1

    Can you tell if bootstrap files are being loaded from within the method $mpdf->WriteHTML()? When it’s called the method $mpdf->Output() a new page is generated, bootstrap relationships will not be inherited. Test...

  • No, only the page that is generating the pdf is who has the link to the bootstrap files...

  • Within the method $mpdf->WriteHTML() I use the call from css to pdf via <style> define all the css there, because absolutely nothing is inherited, worth trying to call from within the method in <head> the relationship links with the bootstrap.

  • I get it, if I then put the css content of bootstrap in there (it would be huge, I know), it would work?

  • Probably, but the PHP/PDF libraries as far as I have tested do not support all CSS. At the time of conversion it "kills" some tags and parameters, test there for us to know! =)

  • 1

    It is worth considering other options outside the mpdf, like the phantomjs and jsPDF, see that response from Transform HTML to PDF, the proposed solution generates a PDF very faithful to the rendered on the page

Show 1 more comment

1 answer

4

You can add css files to mpdf to solve this problem, but you should refer to the mpdf manual to see if it supports all bootstrap settings: mPdf Supported CSS

include('mpdf.php');
$mpdf= new mPDF();
$stylesheet = file_get_contents('bootstrap.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output();
exit; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.