Viewed   1.4k times

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?

 Answers

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

Monday, October 10, 2022
4

Place

ob_clean(); 

immediately before

$mpdf->Output();

Without this mpdf sometimes includes the website page HTML and not just the HTML that you want in the PDF, probably because headers have already been sent elsewhere in the code. That can mess up your PDF so Adobe won't open it.

Monday, December 12, 2022
 
ckram
 
1

PHP is server-side, it can't do anything in the browser, only deliver content.

To add a div, you use Javascript in the page, e.g.

var el = document.getElementById('idoftarget');
el.innerHTML += '<div>new stuff</div>

... I think. I always use jQuery:

$('#idoftarget').append('<div>new stuff</div>');
Wednesday, August 10, 2022
 
ark
 
ark
4
  1. You must create the css file in this route: /module_name/static/src/css/module_name.css. Example of file:
.openerp .classname{
    margin: 12px 0px 12px 0px;
}
  1. Create the file /module_name/views/module_name.xml with this content:
<?xml version="1.0"?>
<openerp>
    <data>
        <template id="assets_backend" name="module_name assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <link rel="stylesheet" href="/module_name/static/src/css/module_name.css"/>
            </xpath>
        </template>
    </data>     
</openerp>
  1. Add the xml file to your __openerp.__py
'data': [
    'views/module_name.xml',
],
  1. Add the class to the elements in the view
<div class="classname">                            
    <field name="field_name" class="other_class"/>
</div>
Wednesday, October 12, 2022
4

You cannot directly import CSS or any other static files into typescript using only the typescript compiler, but you can with the help of some build tools...

For example using webpack, you can set up the css-loader and style-loader to search your source code for require('./whatever.css') and add it to the build before safely compiling your typescript. If you also have webpack generate your HTML then your CSS will be automatically injected as a stylesheet.

Wednesday, October 5, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :