0

How does one apply Bootstrap CSS to dompdf? I tried it with $dompdf->set_base_path but it's not working.

<link rel="stylesheet" href="vendor/bootstrap/dist/css/bootstrap.css" />
Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
Dipankar Naskar
  • 326
  • 1
  • 5
  • 13
  • As i know, I use old version of dompdf before.. Dompdf not a css friendly.. If youre using modern and fancy css... Dompdf unable to render it as youre expected.. Most pdf library have the same resriction on modern css.. Correct me if i'm wrong – weirdo Dec 19 '16 at 16:50
  • Dompdf works just fine with CSS generally, but is deficient in a few ways in regards to Bootstrap. The next release, while still not quite there yet, provides a big improvement in Bootstrap styling. – BrianS Dec 20 '16 at 16:13
  • The question is a bit confusing. Are the styles not applied at all or just not very well? – BrianS Dec 20 '16 at 16:13
  • The styles are applied properly. In HTML view the styles are seeing fine but when it exports in pdf using dompdf the styles are not there. Can you advise any other php plugins which supports bootstrap css?? – Dipankar Naskar Dec 21 '16 at 07:44
  • Did you ever get this to work? – wuno Mar 25 '17 at 03:07

1 Answers1

0

the problem with dom pdf is that it takes the raw html defined on the page itself and combines it with it own default css to build a pdf. to solve this issue you can do various tricks according to the bootstrap version and the dom pdf plugin you are using.

the first way is that you set the default path of dom pdf at run time. you can do this by command

$pdf->set_base_path(.......)

if you are using barryvdh/laravel-dompdf v0.8.2 and Bootstrap v4.0.0. Laravel Cashier creates PDFs by rendering a Blade template. So you could just link to the CDN-hosted version of Bootstrap 4: http://getbootstrap.com/docs/4.1/getting-started/introduction/#css

if you are using any other dom/pdf. you can also change the default css to use by modifying
const DEFAULT_STYLESHEET = "/lib/res/html.css"

at

vendor\dompdf\dompdf\src\Css\Stylesheet.php

the most effective way to do this same task is by adding following commmand to your css file during run time.

$output = '
        <style>
'.file_get_contents("full path to your bootstrap css file").'
</style>'
;

$output .='other table data........'

then this downloads the raw css into your html code.

$pdf = new Dompdf();    
    $pdf->loadHtml($output);
    $pdf->set_option('isHtml5ParserEnabled', true);
    $pdf->render(); 
Prakhar Gyawali
  • 527
  • 4
  • 18