Asked  7 Months ago    Answers:  5   Viewed   422 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

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

Wednesday, March 31, 2021
 
njai
answered 7 Months ago
74

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.

Wednesday, March 31, 2021
 
Uours
answered 7 Months ago
29
<style>
@page :first {
    background: url("http://example.com/first.svg") 50% 0 repeat;
}
@page {
    background: url("http://example.com/page.svg") 50% 0 repeat;
}
...
</style>
Wednesday, March 31, 2021
 
rblarsen
answered 7 Months ago
14

A couple of notes first: when you use Data/data1.txt as an argument, should it really be /Data/data1.txt (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming /Data/data1.txt and .txt files only:

#!/bin/bash
for filename in /Data/*.txt; do
    for ((i=0; i<=3; i++)); do
        ./MyProgram.exe "$filename" "Logs/$(basename "$filename" .txt)_Log$i.txt"
    done
done

Notes:

  • /Data/*.txt expands to the paths of the text files in /Data (including the /Data/ part)
  • $( ... ) runs a shell command and inserts its output at that point in the command line
  • basename somepath .txt outputs the base part of somepath, with .txt removed from the end (e.g. /Data/file.txt -> file)

If you needed to run MyProgram with Data/file.txt instead of /Data/file.txt, use "${filename#/}" to remove the leading slash. On the other hand, if it's really Data not /Data you want to scan, just use for filename in Data/*.txt.

Tuesday, June 1, 2021
 
nighter
answered 5 Months ago
65
  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>
Thursday, July 29, 2021
 
conmen
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :