2

I am using the Mpdf in symfony. I have installed the mpdf through composer like:

composer require mpdf/mpdf 

After that require the Mpdf.php in autoload.php.

Then use the code for mpdf is:

$mpdf = new mPDF();
$html = '<p style="color:red;">PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');

CSS is not affecting on the HTML. When I'm using the style on tag then it's working fine.

$mpdf = new mPDF();
$html = '<style>p{color:red;}</style><p>PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');

When I try to use the CSS with class or ID then also not affecting.

$mpdf = new mPDF();
$html = '<style>p.text-color{color:red;}</style><p class="text-color">PDF Generating...</p>';
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output('demo.pdf', 'F');
1
  • When the page loads, check if the source is exactly as you need it. Code might need to be escaped. Only have a little php experience but that springs to mind. php.net/manual/en/function.htmlspecialchars.php I think it's the double quotes that's screwing you over Commented May 29, 2018 at 10:58

1 Answer 1

4

You don't need the style tag you can do it as follow:

$stylesheet = file_get_contents('style.css');

$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);

So write your CSS first then your html that was working for me the last time.

https://mpdf.github.io/css-stylesheets/introduction.html

Sign up to request clarification or add additional context in comments.

1 Comment

I also tried this solution. It's working but only with HTML tags not with Class/ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.