Skip to content Skip to sidebar Skip to footer

Putting Html Content Between Php Function

i need to put some html content in a variable for generating pdf using mpdf, is there any way to assign html content to a variable in a php function. this is what i am trying.

Solution 1:

Try like this.

functiongetInvoice($conn,$uid,$id,$invoice_no)
{    
     echo$something = "something";

     ob_start();

     $content='<html>    
     <body style="padding-top: 0px">
     <page>
     my html content                 
     </page><body>'.$something.'</body><html>';    

     $content .= ob_get_clean();
  }

?>

Solution 2:

Using the output buffer will only collect data sent to stdout after ob_start and before ob_get_clean. Your code was broken as it looks like you are trying to set the php variable $contents in html, then collecting that.

The result when you ran $contents .= ob_get_clean(); was being set to this value - which you were then passing as html to mPDF to make a pdf file from. The following is not valid html.

$content='<html><bodystyle="padding-top: 0px"><page>
my html content                 
</page><body>something</body><html>'

Additionally, the use of .- is to add two strings together. Since you want the $content to contain valid html, this was mucking things up.

The fix below ensures that the (valid html) content is collected into $content as it appears is your intent.

functiongetInvoice($conn,$uid,$id,$invoice_no) {
    ob_start();
$content='<html>
<head>
<link rel="stylesheet" href="//classes.gymate.co.in/assets/bower_components/uikit/css/uikit.almost-flat.min.css" media="all">
<link rel="stylesheet" href="../assets/css/main.min.css" media="all">
<style>body{font-family:\'roboto\'; .md-card {box-shadow: none; } .uk-width-small-3-5 {width: 40%;}</style>
</head>
<body style="padding-top: 0px">
<page>
my html content                 
</page><body>' . "something" . '</body><html>';

    echo$content;

    $content = ob_get_clean();

    include("../plugins/mpdf/mpdf.php");
    $mpdf=new mPDF('utf-8', 'A4','','',10,10,5,5,5,5);
    $mpdf->SetFont('roboto');
    $mpdf->SetHTMLFooter('<p style="padding-top: 18px; font-size: 12px;"></p><p style="text-align:right; font-size: 12px;">Page {PAGENO} of {nbpg}</p></p>');
    //$stylesheet = file_get_contents('../assets/css/main.min.css');$mpdf->WriteHTML($stylesheet,1);
    $mpdf->writeHTML($content);
    $mpdf->Output("pdf-save-data/reciept.pdf","F");

    echo"PDF File Created";
}

Solution 3:

You can use <<<html and html to achieve this. Example is below

enter image description here

Solution 4:

<?phpfunctiongetInvoice($conn,$uid,$id,$invoice_no) {
    echo$something = "something";
    ob_start(); 
    $content='<html><body style="padding-top: 0px"><page>my html content</page><body>'.$something.'</body><html>';
    $content .= ob_get_clean();
}
?>

Just make sure you have your single and double quotes closed correctly.

Note: Also i personally prefer not to give line breaks in the html when you are going to assgin it to a variable.

Post a Comment for "Putting Html Content Between Php Function"