How to use Bootstrap CSS in dompdf

5.3k views Asked by At

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" />
1

There are 1 answers

0
Prakhar Gyawali On

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();