i have functions which takes stylesheet & gets output from other classes using OOP in PHP.
i need to generate the pdf using TCPDFm but i am facing problem how to pass in $html variable, i have read the manual for XHTML+ CSS using that does not explain the PHP variable output.
I am pating my code here, please help.
<?php
// define some HTML content with style
$html = <<<EOF
<?php $frame->HTML_DOCTYPE(); ?>
<html>
<head><?php $frame->HTML_Title($page); $frame->HTML_CSS($page); $frame->HTML_JS($page); ?></head>
<body>
<div class='container_pngfix'>
<?php echo $frame->Print_Top($page); ?>
<div class='container_magazine'>
<div class='magazine_left'>
<?php
/*if(FALSE == empty($_SESSION['wi_id']) && FALSE == empty($_SESSION['wi_type']) && 0 == strcasecmp('bride',$_SESSION['wi_type'])){*/
if ($_REQUEST['magazineId']!="") {
echo $article->Print_Magazine_Issue($_REQUEST['magazineId']);
} else if ($_REQUEST['latestissue']) {
echo $article->Print_Magazine_Issue($article->getLatestMagazineId());
} else {
if (isset($_REQUEST['magazinedate'])) { $magazinedate=$_REQUEST['magazinedate']; } else { $magazinedate=""; }
if ($magazinedate=="") { $magazinedate=date("Y-m")."-01"; }
echo $article->Print_Magazine($magazinedate);
}
/*}else{
echo "<h2>Please Login/Register first to see Magazine details</h2>";
}*/
?>
</div>
<div class='magazine_right'><?php echo $frame->Print_RightSide_Articles($page); ?></div>
<div class='clearfloat'> </div>
<?php echo $frame->get_ContextualWeb($page,"low"); ?>
</div>
</div>
<?php echo $frame->Print_Bottom_Links($page); ?>
<?php $frame->Print_Bottom($page); ?>
</div>
</body>
</html>EOF;
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'I');
?>
In this code i am calling multiple functions using objects of those classes.
All html & content of page is generating dynamically. i have problem of passing the PAGE html & PHP code in $html variable.
You can use ob_start() and ob_get_clean() to get any output.
<?php
ob_start();
?>
<!-- HTML and PHP code here -->
<?php
$html = ob_get_clean();
?>
$html will have your data that you can send to TCPDF
example for your situation:
<?php
// your other script
ob_start();
?>
<?php $frame->HTML_DOCTYPE(); ?>
<html>
<head><?php $frame->HTML_Title($page); $frame->HTML_CSS($page); $frame->HTML_JS($page); ?></head>
<body>
<!-- rest of the page -->
</body>
</html>
<?php
$html = ob_get_clean();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'I');
?>
Related
I need to include html code inside a php function,after to use the function in another html code.
The HTML code should be in another file, i want to call that code in specified row in another file.
HTML code
<span>
<?php
$register_date = new Moment\Moment($u->get("date"), date_default_timezone_get());
$register_date->setTimezone($AuthUser->get("preferences.timezone"));
$format = $AuthUser->get("preferences.dateformat");
if (!$format) {
$format = "Y-m-d";
}
?>
<?= __("Register date: %s", $register_date->format($format)) ?>
</span>
The function
function thefunction (){
//the html code here
}
The code i want to include the function
<?php if ($u->get("expire_date") < "2050"): ?>
<div class="meta">
<?php ?>
function thefunction ()//The function
?>
</div>
<?php endif ?>
Normal code without the function
<?php if ($u->get("expire_date") < "2050"): ?>
<div class="meta">
<span>
<?php
$register_date = new Moment\Moment($u->get("date"), date_default_timezone_get());
$register_date->setTimezone($AuthUser->get("preferences.timezone"));
$format = $AuthUser->get("preferences.dateformat");
if (!$format) {
$format = "Y-m-d";
}
?>
<?= __("Register date: %s", $register_date->format($format)) ?>
</span>
</div>
<?php endif ?>
One (more elegant) approach, all in php, would be:
<?php
//php code
function sayHello (){
echo '<p>Hello</p>';
}
sayHello();
sayHello();
?>
Another approach would be:
<?php
//php code
function sayHello(){ //leave that bracket open ?>
<!--this is html but you're still inside the function. this won't be printed until you call the function-->
<p>Hello</p>
<?php } //function ends here ?>
Then, in any point:
<?php sayHello(); ?>
and your html will be printed
How to delete all data in div by id after using file_get_contents php [Using PHP]?
I want to delete all data inside div id="one"
This is coding on https://www.example.com
<div id="main">
<div id="inner">
<div id="one">
<div>
HELLO
</div>
HELLO
</div>
<div id="two">
TEST
</div>
</div>
</div>
.
<?PHP
$home_page = file_get_contents("https://www.example.com");
echo $home_page;
?>
When finished i want to get data like this
<div id="main">
<div id="inner">
<div id="two">
TEST
</div>
</div>
</div>
How can i do ?
Best regards,
mongkon tiya
Edit: Readed you want an plain PHP solution, sry this is a JS solution.
echo '<script type="text/javascript">',
'var elem = document.getElementById("one"); elem.remove();',
'</script>'
Just call in php via echo after your file_get_contents a script. I write it in plain js, if its not working and ure using an older browser use:
echo '<script type="text/javascript">',
'var elem = document.getElementById("one"); elem.parentNode.removeChild(elem);
'</script>
If you use Jquery u can also just use the function .remove();
In order to accomplish this sort of DOM manipulation using only PHP you need to use output buffering and DOMDocument
<?php
ob_start(); /* tell php to buffer the output */
?>
<!-- typical html page - there MUST be a doctype though!-->
<!--
<!doctype html>
<html>
<head>
<title>Output buffer test</title>
</head>
<body>
<div id='main'>
<div id='inner'>
<div id='one'>
<div>
Hello World
</div>
hello
</div>
<div id='two'>
Goodbye Cruel World
</div>
</div>
</div>
</body>
</html>
-->
<?php
echo file_get_contents('http://www.example.com');
?>
<!-- manipulate the DOM using PHP only -->
<?php
libxml_use_internal_errors( true );
/* read the currently stored buffer and load into DOMDocument */
$buffer=#ob_get_contents();
$dom=new DOMDocument;
$dom->loadHTML( $buffer );
libxml_clear_errors();
/* Find the DOM element you wish to remove */
$div=$dom->getElementById('one');
$div->parentNode->removeChild( $div );
/* Save the current DOM and flush the buffer */
$buffer=$dom->saveHTML();
#ob_end_clean();
echo $buffer;
if( #ob_get_level() > 0 ) {
for( $i=0; $i < ob_get_level(); $i++ ) #ob_flush();
#ob_end_flush();
}
$dom=null;
?>
Every page of my website consist of:
generic header
the page itself
generic footer
In code the page looks like this:
<?php
require_once('header.php');
load_header('title', 'meta description'); // loads also MENU
?>
// Unique content of the page
<?php
// footer settings
$load_contact_info = true;
// load footer
require_once('footer.php');
load_footer($load_contact_info);
?>
On same pages I would like to load extra content (like more JS libraries) in the footer.php (right before - closing body tag):
<?php
function load_footer($load_contact_info = false) {
?>
<footer>
<?php if ($load_contact_info) { ?>
<div id="contact-us">
// contact info
</div>
<?php } ?>
<div id="inner-footer">
// more generic footer content
</div>
</footer>
<script src="<?php echo($url); ?>js/vendor/jquery.js"></script>
// I want to load more UNIQUE JS content here at SOME pages
</body>
</html>
<?php } ?>
What is the best practise for loading extra libraries in the footer? Should I extend the footer.php with more if-yes-load-more like this:
if ($google_maps_api) {
// map settings
echo "<script src=\"https://maps.googleapis.com/maps/api/js?callback=initMap\" async defer></script>";
}
if ($other_api) {
echo "<script src=\"https://example.com/other-api/\" async defer></script>";
}
Or are there any other possibilites to do it better?
Load extra libraries should not be the task for a footer. A footer displays the footer. you should create a new php file and name it something like "handle_js.php"
function print_script_tag($src, $options) {
echo "<script src=\"" . $src . "\" " . $options . "></script>\n";
}
function print_all_script_tags_for_url($url) {
if($url === 'example') {
print_script_tag('library.js','async');
}
}
and in your footer file
</footer>
print_scripts($url);
</body>
I didn't test the code.
I would like to mail the content of a Web page that looks like:
<html>
<body>
<?php
function sendPageContentToEmail($destEmail)
{
ob_start();
$buffer = ob_get_contents();
ob_end_clean();
$subject = 'Subject name';
mail($destEmail, $subject, $buffer);
}
?>
<div style="width:400px; margin:0 auto;">
<p>
Name: <?php print($customerData['customer_name']); ?>
</p>
<p>
....
</p>
</div>
</body>
</html>
<?php
sendPageContentToEmail($customerData['customer_email']);
//erase all temp data
session_destroy();
?>
$buffer is always empty (ob_get_content()) regardless of where the sendPageContentToEmail() is called.
Where should this function be called (assuming that it is the right way to do it)?
What ob_start does is start caching all the output, so, if you output something before calling it, that part won't be cached.
Just at the start, before <html> do a <?php ob_start(); ?>
I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>