Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add multiple css file in mpdf

Tags:

html

css

php

mpdf

I am trying to convert my html page into pdf format using mpdf. problem is that i am unable to apply more than one css to pdf file.. here is my code of php

<?php
           $html =file_get_contents('mpdf/test.html');
            include("../mpdf.php");
            $mpdf=new mPDF(); 
           $mpdf->SetDisplayMode('fullpage','two');
            // LOAD a stylesheet 1
            $stylesheet = file_get_contents('assets/css/main.css');
            $mpdf->WriteHTML($stylesheet,1);    // The parameter 1 tells that this is css/style only and no body/html/text
          // LOAD a stylesheet 2
            $stylesheetextra = file_get_contents('assets/css/test.css');
            $mpdf->WriteHTML($stylesheetextra ,1);  // The parameter 1 tells that this is css/style only and no body/html/text
            $mpdf->WriteHTML($html,2);
          $mpdf->Output();

            exit;
            ?>

Output it gives doesn't come with test.css . main.css is applying properly to pdf file but test.css doesn't applying.please help me? thanking you in advance

like image 419
Sitestrikes Shailesh Avatar asked Sep 29 '15 08:09

Sitestrikes Shailesh


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 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 ...

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.

What is mPDF file?

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF with a number of enhancements. The original author, Ian Back, wrote mPDF to output PDF files 'on-the-fly' from his website, handling different languages.


2 Answers

You can do it just storing the CSS content in one variable, like this:

$stylesheet  = '';
$stylesheet .= file_get_contents('css/bootstrap.min.css');
$stylesheet .= file_get_contents('css/style.css');
$stylesheet .= file_get_contents('css/impressao_ctr.css');
like image 193
Gustavo Tarchiani Avatar answered Sep 20 '22 07:09

Gustavo Tarchiani


It may be an ugly solution but i ran into a similar problem before. What i did was, since it wasn't a huge problem, i took all the css from the 2nd one i couldn't add, and added it to the "main.css".

like image 33
somdow Avatar answered Sep 20 '22 07:09

somdow