Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add css file in mpdf

Tags:

html

ajax

php

mpdf

I am trying to convert html into pdf using mpdf. Problem is that i am unable to apply css to pdf file..

Here is my code of php:

<?php

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

?>

What it is doing is taking html through ajax on my this php page. But the output it gives doesn't come with css which i've written for it..

Please tell me that to do now?

like image 617
saad Avatar asked Mar 20 '13 09:03

saad


People also ask

How do I load CSS in Mpdf?

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

How do I use CSS in PDF?

First you add the CSS rules to the custom CSS. The CSS rules are then applied when you create a PDF. Using custom CSS can cause unpredictable, incorrect views. Only use custom CSS in the app, if you have reasonable experience with CSS and check the PDF.

How do I add a page in Mpdf?

If writing a DOUBLE-SIDED document, a conditional page-break ( $type = "E" or "O" ) will add a new page only if required to make the current page match the type (i.e. ODD or EVEN); a page-break with $type = "NEXT-ODD" or "NEXT-EVEN" will add one or two pages as required to make the current page match the type (i.e. ODD ...

What is the default stylesheet?

Default style sheet: Default style sheet is also known as Browser style sheet Or User-agent style sheet. This is the style sheet which browser applies by default for every web page which it renders. So if the author of a web page did not apply any styling from his side, even then the web page will not be styleless.


1 Answers

 <?php

$html = $divPrint;

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

?>

1st assign your html in $html then include mpdf.php file.

like image 176
Haseeb Avatar answered Oct 10 '22 04:10

Haseeb