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.

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

What i need is that the all the content in html get assign to $content variable. It is not working because it is between php execution function.


Try like this.

function getInvoice($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(); } ?>


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

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> <body style="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 (<em>valid html</em>) content is collected into $content as it appears is your intent.

function getInvoice($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 style="text-align:right; font-size: 12px;">Page {PAGENO} of {nbpg}

'); //$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"; }


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

<a href="https://i.stack.imgur.com/KFTyE.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/KFTyE.png" data-original="https://i.stack.imgur.com/KFTyE.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


<?php function getInvoice($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.


  • how to add multiple css file in mpdf
  • Setting a custom TTF Font in mPDF
  • MPDF showing an extra page at the end
  • how to auto create pdf using highchart graphs
  • R do something after a warning (like tryCatch a warning, then edit an object)
  • Can i say that MVP = 3 Tier Archi?
  • Calculating the occurrences of numbers in the subsets of a data.frame
  • Collecting wall posting permission when using the Facebook Registration plugin
  • Scala's collect inefficient in Spark?
  • How is user.dir configured for Tomcat servlets?
  • Errno 2 using python shutil.py No such file or directory for file destination
  • How to import Navit into Eclipse and use it in own android project
  • Is there a way to directly consume a Rayon chain without collecting it first?
  • C++ String tokenisation from 3D .obj files
  • Spring MVC redirect with custom http headers
  • Determining the length of a read stream in node js
  • Updating and removing unique join relationships in CakePHP
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • How do I check if System::Collections:ArrayList is empty / nullptr / null?
  • Creating a C++ function that calls other Lua function
  • incomplete type 'struct' error in C
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How to use function wrapper in mustache.php?
  • AppleScript : find open tab in safari by name and open it
  • Using a canvas object in a thread to do simple animations - Java
  • Deserializing XML into class C#
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • php design question - will a Helper help here?
  • Use group_by to filter specific cases while keeping NAs
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • python draw pie shapes with colour filled
  • reshape alternating columns in less time and using less memory
  • How to Embed XSL into XML