DOMPDF sometimes work,and sometimes not - php

im using DOMPDF to make downloadable webpage.
but sometime it works, and Sometimes the page takes so long and finally displays "This site can not be reached, The connection was reset".
how to fix this?
its code error or its error on my webbrowser?
thanks :)
function __construct()
{
parent::__construct();
$this->load->model("Mproses");
}
public function download($db,$p,$id) {
// Load all views as normal
$data['hasil'] = $this->Mproses->selectWhere($db,$p,$id);
$this->load->view('hasil',$data);
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
$today = date('D-M-Y h:i:s');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream($today.".pdf",array('Attachment'=>1));
//or without preview in browser
//$this->dompdf->stream("welcome.pdf");
}
thats my controller for dompdf
HTML file im tryin to download :
<div class="row">
<div class="container">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Hasil Pengecekan! <p class="pull-right"><span class="fa fa-backward" aria-label="Kembali"></span> | <b>Kembali</b></p></h3>
</div>
<div class="panel-body">
<p style="color:black;">
<?php if(count($hasil)>0) { ?>
<?php foreach($hasil as $d) { ?>
<hr>
<br>
<center>
<h2 style="color:black;">Hasil Dari User : <?php echo $d->nama; ?></h2>
</center>
<br>
<hr>
<br>
<ul>
<li style="color:black;"><h5>Penyakit</h5><p style="font-size:20px;"><?php echo $d->penyakit;?></p></li>
<li style="color:black;"><h5>Pilihan</h5><p style="font-size:20px;">"<?php echo $d->pil;?>"</p></li>
<li style="color:black;"><h5>Nilai CF</h5><p style="font-size:20px;"><?php echo $d->nilaicf;?> || <?php echo $d->nilaicf*100;?>%</p></li>
</ul>
<?php } ?>
<?php } else { echo 'Maaf Data Yang Dicari Tidak Ada !'; } ?>
</p>
<br>
<hr>
<br>
<center><?php echo anchor(URL.'index.php/topdf/download/riwayat/id_riwayat/'.$d->id_riwayat,'Download',array('style' => 'color:black;font-size:14px;padding:14px;border:1px black solid;')); ?></center>
<br>
</div>
</div>
</div>
</div>
</div>

Related

dompdf rendering empty file or doesn't render database rows

So if I just render the file like this :
<?php
// Include autoloader
require_once '../dompdf/autoload.inc.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// Load content from html file
$html = file_get_contents("../orderconfirmation.php");
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codexworld", array("Attachment" => 0));
But this gives me a pdf file with no data from the database, so I tried converting the file to a HTML file as suggested by many different places.
I did the following:
<?php
ob_start();
include("orderconfirmation.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
?>
But this gives me an empty file. Any suggestions?
BTW, this is the code for my orderconfirmation.php file :
<?php
session_start();
include 'includes/restrictions.inc.php';
logged_in();
$title = 'Order confirmation';
include_once 'includes/header.php';
include_once 'includes/dbh.inc.php';
?>
<div id="" class="section orderconsec content">
<div class="container">
<div class="pb-4">
<h3>Thank you for your order</h3>
</div>
<div class="border-new border border-dark rounded p-3">
<div class="pt-2 pr-4 pl-4">
<b><h3>Order number</h3></b>
<h3><?php
$order_id = $_SESSION["order_id"];
$ordernosql = "SELECT * FROM invoice WHERE order_id = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $ordernosql)) {
echo "SQL statement failed";
}
else
{
mysqli_stmt_bind_param($stmt, "s", $order_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if($row = mysqli_fetch_assoc($result))
{
echo $row['order_id'];
?></h3>
<form method="post" action="includes/printpdf.inc.php">
<button type="submit" name="print-vouchers" class="btn btn-danger mt-3">Print</button>
</form>
</div>
<hr>
<div>
<p>We are currently processing your order and we will email you with confirmation shortly.</p>
<p>For your convenience you may want to save your order confirmation.</p>
</div>
<div class="pt-5 pr-4 pl-4">
<b><h3>Delivery Details</h3></b>
</div>
<hr>
<div style="padding: 10px;">
<p><b>Delivery for</b></p>
<p>
<?php
echo $row['uidUsers'];
?>
</p>
</div>
<div style="padding: 10px;">
<p><b>Address</b></p>
<p>
<?php
echo $row['user_address'];
?>
</p>
</div>
<div style="padding: 10px;">
<p><b>Delivery method</b></p>
<p>
<?php
echo $row['delivery_method'];
?>
</p>
</div>
<div class="pt-5 pr-4 pl-4">
<b><h3>Order summary</h3></b>
</div>
<hr>
<div style="padding: 10px;">
<p>
<?php
$output = str_replace(',', '<br />', $row['order_summary']);
echo $output;
?>
</p>
</div>
<div class="pt-5 pr-4 pl-4">
<h3>Payment information</h3>
</div>
<hr>
<div style="padding: 10px;">
<p><b>Payment type</b></p>
<p>
<?php
echo $row['payment_type'];
?>
</p>
</div>
<div style="padding: 10px;">
<p><b>Billing address</b></p>
<p>
<?php
echo $row['billing_address'];
}
}
?>
</p>
</div>
</div>
</div>
</div>
<?php
include_once 'includes/footer.php';
?>
You're loading the content of a PHP file into a variable via file_get_contents then passing that variable to Dompdf:
$html = file_get_contents("../orderconfirmation.php");
$dompdf->loadHtml($html);
This won't work. Dompdf does not parse PHP prior to rendering, that's something you need to do yourself. Try:
ob_start();
require("../orderconfirmation.php");
$html = ob_get_clean();
ob_end_clearn();
$dompdf->loadHtml($html);

Codeigniter - How to retrieve specific data on view after getting result array from model

So this is the function from model Publicationdata that sends the result array to controller based on the author id that controller provides it->
public function getSpPubData($tId)
{
$this->db->order_by("pub_year", "desc");
$q = $this->db->get_where('rsc_faculty_publications', array('pub_author_id' => $tId));
$error = $q->num_rows() > 0 ? FALSE : TRUE;
if ($error === FALSE)
{
return $q->result();
}
else{
return $error;
}
}
now on controller i am saving the array as-
$data['pubData'] = $this->Publicationdata->getSpPubData("10006);
the rsc_faulty_publications table has a field pub_year.. now on view when I am accessing $pubData on view.. i want to print the publications yearwise. they will be printed in a loop when the current year publications will be at top.. so what I want is to fetch the yearwise publications from $pubData through loop.. the view is like this-
<?php
for($i=0;$i<sizeof($pubData);$i++){ ?>
<div class="panel panel-info">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion<?php echo $pubData[$i]->pub_year; ?>" href="#year<?php echo $pubData[$i]->pub_year; ?>" title="Click to expand/collapse">
<h4 class="panel-title">Publication Year: <?php echo $pubData[$i]->pub_year; if($pubData[$i]->pub_year==date('Y')){echo " (".sizeof($pubData)." Publications until now)";}?></h4>
</div>
<div id="year<?php echo $pubData[$i]->pub_year; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php while($pubData[$i]->pub_year==$pubData[$i+1]->pub_year){
?>
<div class="well well-info">
<p><?php echo $pubData[$i]->pub_title; ?></p>
<p><?php echo $pubData[$i]->pub_authors; ?></p>
<p><?php echo $pubData[$i]->pub_publisher; ?></p>
</div>
<?php
}?>
</div>
</div>
</div>
<?php } ?>
First load the view file in controller and pass the data.
$this->load->view('view_file', $data);
Use following code in the view file to retrieve the data.
foreach($pubData->brands as $brand): ?>
after getting the value from model say
$data['value']=$this->model1->getValue();
Now you can pass the value you obtained to view page like this
$this->load->view('viewpagename',$data);
Now in the view page you can easily access it using the array name you passed .ie value. So in the view page do
foreach($value as $val){
print what you want here
}
<?php $i=0;
$n=0;
$brands = array('info','warning','primary','success');
$len=sizeof($pubData);
while($i<$len){ ?>
<?php if($i==0||$i-1!==-1 && $pubData[$i]->pub_year!==$pubData[$i-1]->pub_year){ ?>
<div class="panel panel-<?php echo $brands[$n];?>">
<div class="panel-heading" data-toggle="collapse" data-parent="#accordion1" href="#year<?php echo $pubData[$i]->pub_year; ?>" title="Click to expand/collapse">
<h4 class="panel-title">Publication Year: <?php echo $pubData[$i]->pub_year; if($pubData[$i]->pub_year==date('Y')){echo " (".sizeof($pubData)." Publications until now)";}?></h4>
</div>
<div id="year<?php echo $pubData[$i]->pub_year; ?>" class="panel-collapse collapse">
<div class="panel-body">
<?php
}?>
<div class="well well-<?php echo $brands[$n];?>">
<p><?php echo $pubData[$i]->pub_title; ?></p>
<p><?php echo $pubData[$i]->pub_authors; ?></p>
<p><?php echo $pubData[$i]->pub_publisher; ?></p>
</div>
<?php if($i==$len-1 || $i+1!==$len && $pubData[$i]->pub_year!==$pubData[$i+1]->pub_year){
if($n==3){$n=0;}else{$n++;} ?>
</div>
</div>
</div >
<?php } $i++;
} ?>

PHP code to change image source on each page with URL or page Title

I am working on running website which is loading main.php file on Each page and it has thousands of pages. Each page url is different e.g,
mywebsite.com/United-Kingdom.php
and
mywebsite.com/United-Stats-of-america.php.
each page having unique page title which is being load by PHP from Database,
(<h4 style="font-size:20px;color:#148ADB;"><?php echo $cityname['country']; ?>Information</h4>).
i want to change header jpg image automatically with url or with page title e.g if user go united-kingdom url than image should be loaded /images/country/united-kingdom.jpg on header and when user open United Stats page it should load image /images/country/united-stats.jpg on header.
How can i achieve this ?
= now() ORDER BY fare");
$arr1=mysql_fetch_array($sel1);
?>
<div id="content-top-txt" style="height:auto">
<div id="content-top-inner1">
<div id="content-top-txt-inner1" style="height:auto">
<div class="content">
<h4 style="font-size:20px;color:#148ADB;"><?php echo $cityname['country']; ?>specials</h4>
<p style='text-align:justify; font-size:13px; color:#3c3c3c;'>
<strong style="text-transform:uppercase;color:#148ADB;"><?php echo $cityname['country']; ?></strong> with choice of range prices think of comfort for lesser cost<strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong> starts from <?php
$tempcode=$arr1['des_code'];
$stemp=mysql_query("SELECT * FROM cities_var WHERE code='$tempcode'");
$temp=mysql_fetch_array($stemp);
if($temp['code']=='')
{
?>
<strong><?php echo $arr1['des_name'];?> (<?php echo $arr1['des_code']; ?>) </strong>
<?php
}
else
{
?>
<strong><?php echo $arr1['des_name'];?> (<?php echo $arr1['des_code']; ?>) </strong>
<?php
}
?>
# <strong>£<?php echo $arr1['fare']; ?></strong> with & table below<?php echo $arr1['des_name'];?> & other cities in <strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong> About<strong>UK</strong>.General information About <strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong>call # 123456789 or use Form or email:- <strong>Email#address.com</strong></p>
<p><?php echo $cvar['overview2']; ?></p>
<p>Note:-Prices given are excluding taxes</p>
</div>
<div id="content-top-inner2">
<div id="content-top-txt-inner2" style="height:auto">
<!-- slider start here -->
<div class="slider-main">
</div> <!-- slider end here-->
</div><!-- Content-top-txt-inner2 end-->
</div><!-- content-top-inner2 end here-->
</div><!--contaent-top-txt end here -->
</div><!--content-top end here -->
<div id="content-middle">
<div id="content-middle-txt">
<?php
include("../../files/lb1.php");
?>
<div class="content-middle-txt-right">
<?php
include("../../files/d-search-compare.php");
?>
</div>
<div style="clear:both"></div>
<div class="more"><p><span>Note : </span><?php //echo $sql_rec['note']; ?></p><p>paragraph text.</p>
</div> <!--content-middle-txt end -->
</div> <!--content-middle end -->
<div id="content-bottom">
<div id="content-bottom-txt">
<div id="bottom-all-box">
<?php
include("../../files/lb2.php");
?>
<div id="content-bottom-right">
<?php
include("../../files/rb1.php");
?>
</div>
</div>
<?php
/*
include("../../files/country-left-box3.php");
include("../../files/country-right-box1.php");
*/
?>
I would change all the image names to the exact page name of the PHP file. So for example you have /united-kingdom.php your image should be named united-kingdom.jpg.
With that the same you can get the current page URL which you requested using $_SERVER['REQUEST_URI'].
Now you can define the image using the current page name.
$url= $_SERVER['REQUEST_URI'];
$imageName = substr($url, 0, strpos($imageName, "."));
The variable $imageName will contain united-kingdom.
So you can define your image like:
<img src="<?php echo $imageName; ?>.jpg" alt="" />
This code is not completely tested but is just to give you an idea on how you can handle it.

Parsing page source using PHP

I'm having a lot of trouble parsing the page source of a results page. The results page returns data about businesses in a city. This data includes name, address, phone number, owner name and URL. Any help would be much appreciated.
This is an example of one of the results (There are hundreds in the original file):
<div class="ListingResults_All_CONTAINER ListingResults_Level3_CONTAINER">
<div class="ListingResults_Level3_HEADER">
<div class="ListingResults_All_ENTRYTITLERIGHT">
<div><img src="/external/wcpages/images/L3more.gif" alt="317 at Montgomery"></div>
</div>
<div class="ListingResults_All_ENTRYTITLELEFT">
<div class="ListingResults_All_ENTRYTITLELEFTBOX"><strong><span itemprop="name">317 at Montgomery</span></strong></div>
</div>
</div>
<div class="ListingResults_Level3_MAIN">
<div class="ListingResults_Level3_MAINRIGHT">
<div class="ListingResults_Level3_MAINRIGHTBOX">
<div class="ListingResults_Level3_LOGO"><img src="http://www.centerstateceo.com/external/wcpages/wcwebcontent/webcontentpage.aspx?contentid=2071" class="ListingResults_Level3_LOGOIMG"><div style="width:100%;height:1px;overflow:hidden;"></div>
</div>
<div class="ListingResults_MAINRIGHTBOXDIVIDER" style="width:100%;overflow:hidden;height:1px;">_</div>
<div class="ListingResults_Level3_AFFILIATIONS"></div>
</div>
</div>
<div class="ListingResults_Level3_MAINLEFT">
<div class="ListingResults_Level3_MAINLEFTBOX" itemtype="http://data-vocabulary.org/Address" itemscope="" itemprop="address"><span itemprop="street-address">317 Montgomery St.</span><br><span itemprop="locality">Syracuse</span>, <span itemprop="region">NY</span> <span itemprop="postal-code">13202 </span><div class="ListingResults_Level3_MAINCONTACT"><img src="/external/wcpages/images/maincontact.gif" alt="Mr. Dean Whittles">Mr. Dean Whittles</div>
<div class="ListingResults_Level3_PHONE1"><img src="/external/wcpages/images/phone.gif" alt="Work Phone: (315) 214-4267">(315) 214-4267</div>
</div>
</div>
</div>
<div class="ListingResults_Level3_FOOTER">
<div class="ListingResults_Level3_DESCRIPTION">
<div class="ListingResults_Level3_DESCRIPTIONBOX"></div>
</div>
<div class="ListingResults_Level3_FOOTERRIGHT">
<div class="ListingResults_Level3_FOOTERRIGHTBOX">
<div class="ListingResults_Level3_SOCIALMEDIA"></div>
</div>
</div>
<div class="ListingResults_Level3_FOOTERRIGHT">
<div class="ListingResults_Level3_FOOTERRIGHTBOX">
<div class="ListingResults_Level3_COUPONS"></div>
</div>
</div>
<div class="ListingResults_Level3_FOOTERLEFT">
<div class="ListingResults_Level3_FOOTERLEFTBOX"><span class="ListingResults_Level3_LEARNMORE"><a href="/Restaurants/317-at-Montgomery-7897" class="level3_footer_left_box_a friendly">
Learn More
</a></span><span class="ListingResults_Level3_VISITSITE"> | <a href="http://www.317syr.com" onclick="recordReferralOnClick('20947', '7897', 'W');" target="_blank">
Visit Site
</a></span><span class="ListingResults_Level3_MAP"> | Show on Map</span></div>
</div>
</div>
</div>
PHP Code from Comment:
<?php
$dom = new DOMDocument();
$dom->loadHtml($data);
$spans = $dom->getElementsByTagName('span');
foreach ($spans as $el) {
$children = $el->childNodes->item(1);
if (is_object($children) AND $children->tagName == 'a') {
$url = $children->getAttribute('href');
echo $url;
continue;
}
$user_param = $el->getAttribute('itemprop');
$value = $el->nodeValue;
if ($user_param != "") {
echo $user_param . " " . $value . "\n";
}
}
?>

javascript executing php code

i have a php web application project ... and i want to implement a javascript onclick code ..
for example :
when user clicks follow this post ... the MySQL query inserts into database that the user followed this ... so the page is refreshed and after that it will appear as FOLLOWED ..
what's the javascript code needed to do this ... or is there any example may fit ??
here's a sample code
<div id="main_content">
<?php
if(isset($_GET['et']))
{
$et = $_GET['et'];
}
$result = mysql_query("select * from events where event_type =$et") ;
require_once('queries/category_extract.php') ;
?>
<div id="body_content">
<div id="event_tag">
<a><?php echo $cattitle["categ_title"]?>s</a>
</div>
<?php
while($row=mysql_fetch_array($result) )
{
require('queries/followers_extract.php')
?>
<div id="postpreview">
<div id="postpreview_up">
<div id="postpreview_up_left">
<div id="postpreview_up_left_left">
<a>Event Title :</a>
</div>
<div id="postpreview_up_left_right">
<a><?php echo $row["event_title"] ?></a>
</div>
</div>
<div id="postpreview_up_right">
<img src="images/read_more.gif" />
</div>
</div>
<div id="postpreview_bottom">
<div id="postpreview_bottom_left">
<div id="postpreview_bottom_left_left">
<a>Date :</a>
</div>
<div id="postpreview_bottom_left_right">
<a><?php echo $row["event_date"] ?></a>
</div>
</div>
<div id="postpreview_bottom_right">
<div id="postpreview_bottom_right_left">
<?php
if($follower['follower_id'] ==NULL){echo " <img src='images/follow_button.jpg' /> " ; }
else { echo " <img src='images/follow_closed.jpg' /> " ;}
?>
</div>
<div id="postpreview_bottom_right_right">
<?php
if($follower['follower_id'] !=NULL){ echo " <img src='images/following_button.jpg' /> " ; }
else { echo " <img src='images/following_Closed.jpg' /> " ;}
?>
</div>
<div id="postpreview_bottom_right_right">
<?php
if($follower['follower_id'] !=NULL){ echo " <img src='images/unfollow_button.jpg' /> " ; }
else { echo " <img src='images/unfollow_closed.jpg' /> " ;}
?>
</div>
</div>
</div>
</div>
<div id="splitter"></div>
<?php
}
?>
<!--End Of Post Preview-->
<!--End Of Post Preview-->
ok go for this: http://www.9lessons.info/2009/04/exactly-twitter-like-follow-and-remove.html
you have to learn the basics as i read.

Categories