Need Guidance. TCPDF error on output - php

I am having an issue with my TCPDF output error and I'm not sure how to solve it. It said
"Warning: Cannot modify header information - headers already sent by
(output started at C:\xampp\htdocs\Printing\listing_p.php:14) in
C:\xampp\htdocs\tcpdf\tcpdf.php on line 9043 TCPDF ERROR: Some data
has already been output to browser, can't send PDF file"
I'm quite new to TCPDF and it doesn't have proper tutorial to guide me through. Examples dont help much though...
Here is my codes:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>insert page</title></head>
<body>
<?php
ob_start();
require_once("../tcpdf/tcpdf.php");
require_once("../tcpdf/config/lang/eng.php");
$pdf = & new TCPDF("P","mm","A4",true,"UTF-8",false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(false);
$link = mysql_connect("localhost","root");
if(!$link){
die('Could not connect: '.mysql_error());
}
if(mysql_select_db("New_People",$link)){
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT * FROM People1");
$pdf->SetMargins(15,20,15);
$pdf->AddPage();
$pdf->SetFont('FreeSerif','B',12);
$pdf->SetFillColor(255,255,255);
$i = 0;
$max = 30;
$row_height = 5;
$backup_group = "";
$pdf->Cell(60,$row_height,'Id',1,0,'C',1);
$pdf->Cell(60,$row_height,'Code',1,0,'C',1);
$pdf->Cell(60,$row_height,'Name',1,0,'C',1);
while($row = mysql_fetch_array($result))
{
$Id = $row['P_Id'];
$Code = $row['P_Code'];
$Name = $row['P_Name'];
if($backup_group != $Id)
{
$pdf->SetFont('FreeSerif','B',12);
$pdf->Cell(120,$row_height,$Id,1,1,'C',1);
}
if($i >$max){
$pdf->AddPage();
$pdf->SetFont('FreeSerif','B',12);
$pdf->SetFillColor(255,255,255);
$pdf->Cell(60,$row_height,'Id',1,0,'C',1);
$pdf->Cell(60,$row_height,'Code',1,0,'C',1);
$pdf->Cell(60,$row_height,'Name',1,0,'C',1);
}
if(!empty($group)){
$pdf->SetFont('FreeSerif','',12);
$pdf->Cell(60,$row_height,$Id,1,0,'C',1);
$pdf->Cell(60,$row_height,$Code,1,0,'C',1);
$pdf->Cell(60,$row_height,$Name,1,0,'C',1);
}
$backup_group = $Id;
$i++;
}
mysql_close($link);
ob_end_clean();
$pdf->Output('mypdf.pdf','I');
}
else{
die("Error: ".mysql_error());
}
?>
</body>
</html>
Any suggestion or help is appreciated. Thanks!

Problem Solved, just removed
require_once("../tcpdf/config/lang/eng.php");
from the top of the codes then everything is going well.

Related

How to properly display an HTML page after PHP script?

somehow I couldn't find an answer to the following problem when searching the web.
I'm not familiar with PHP and am trying to get the below PHP code, which I put right at the beginning of the file, to display the HTML page that follows, instead of "Welcome.".
Many thanks for your time!
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo "Welcome.";
?>
<!DOCTYPE html>
<html lang="De">
<head>
<meta charset="UTF-8"/>
...
I found the solution...
<?php
ob_start();
session_start();
if(!isset($_SESSION['userid'])) {
die('<script>window.location = "https://test.com/login.php"</script>');
}
$userid = $_SESSION['userid'];
echo <<<HTML
--> HTML section/code <--
HTML;
?>

How to redirect after fopen() in php

I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).

Ping server every 1 second infinitely till closed?

I'm wanting to set up a loop or maybe a page refresh that pings my server over and over again and tells me the milliseconds.
This is the code I'm using but not sure how to make it keep refreshing and giving me the response live. Can someone show me how to make it live so it constantly updates every 1 second or even every 2 or 3 seconds is fine also. Just need it to be live.
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
Edited with Paul's Code still no luck:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
function pingDomain($domain){
$starttime = microtime(true);
// supress error messages with #
$file = #fsockopen($domain, 80, $errno, $errstr, 10);
$stoptime = microtime(true);
$status = 0;
if (!$file){
$status = -1; // Site is down
}
else{
fclose($file);
$status = ($stoptime - $starttime) * 1000;
$status = floor($status);
}
return $status;
}
?>
Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br>
</body>
</html>
There are two ways I can think of:
1.Setting up refresh attribute in the page header
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;" />
<meta http-equiv="refresh" content="1; url='yourPage.php'" />
</head>
<body>
<?php
//your php code here
?>
</body>
</html>
2.Use a crontab job to execute this command every second:
w3m http://yourhost/yourPage.php
I think the first solution is closer to what you need.

How to extract name, description and favicon from a site? [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm trying to create a social bookmarking site using php and mysql.
When I save a website's URL, I want to be able to save the site's title, favicon and description in a table in my database, then print them on my page using ajax.
How can I extract those elements from a website?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$myServer = "localhost";
$myUser = "root";
$myPass = "'100pushups'";
$myDB = "social_bookmarking";
//connection to the database
$connect = mysqli_connect($myServer,$myUser, $myPass)
or die("Couldn't connect to SQLServer on $myServer");
//select a database to work with
$selected = mysqli_select_db($connect, $myDB)
or die("Couldn't open database $myDB");
var_dump($_POST);
//declare the SQL statement that will query the database
$url = "INSERT INTO url (url ) VALUES ('$_POST[url]')";
if (isset($_POST['value']))
{
// Instructions if $_POST['value'] exist
echo 'Your url is ' .$url;
}
$data = get_meta_tags($url);
print_r($data);
if (!mysqli_query($connect, $url)) {
die('Error: ' . mysql_error());
}
else
{
echo "Your information was added to the database";
}
mysqli_close($connect);
?>
</body>
</html>
I know I'm doing something wrong with my url there, but I don't know how to use a variable as an argument in get_meta_tags, since the function only accepts filenames or strings.
You can get the title by using: (courtesy of https://stackoverflow.com/users/54680/jonathan-sampson)
<?php
if ( $_POST["url"] ) {
$doc = new DOMDocument();
#$doc->loadHTML( file_get_contents( $_POST["url"] ) );
$xpt = new DOMXPath( $doc );
$output = $xpt->query("//title")->item(0)->nodeValue;
} else {
$output = "URL not provided";
}
echo $output;
?>
You can get the favicon using:
<?php
$url = $_POST['url'];
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML(file_get_contents($url));
$xml = simplexml_import_dom($doc);
$arr = $xml->xpath('//link[#rel="shortcut icon"]');
echo $arr[0]['href'];
?>
Finally for the description you can use:
<?php
$tags = get_meta_tags($_POST['url']);
$description = $tags['description'];
echo $description;
?>
There are very smart scripts/classes out there that help getting content from the dom. For instance using smart selectors. I recommend using one of those.
This is a nice example:
http://simplehtmldom.sourceforge.net/
To get the content of the page, use file_get_contents or equal function.
You can use file_get_contents() function to get the favicon for a site(unless it thwarts you for https). Example:
$icon = file_get_contents("http://stackoverflow.com/favicon.ico");
// now save it
Another option is using curl. It's an awesome php extension if you know how to use it.
Using these methods, you can fetch the html content from the sites too. And then can parse them any HTML parser library of PHP. Or can use REGEX(which experts doesn't recommend often).

Error on generating chart

In this below code i want to generate pie chart.and i got this error Undefined variable: mysqli, Call to a member function query() on a non-object at line 29.Please any one help me to rectify the problem to get solution.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pie Chart Demo (LibChart)- http://codeofaninja.com/</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<?php
//include the library
include "libchart/libchart/classes/libchart.php";
//new pie chart instance
$chart = new PieChart( 500, 300 );
//data set instance
$dataSet = new XYDataSet();
//actual data
//get data from the database
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from programming_languages";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
if( $num_results > 0){
while( $row = $result->fetch_assoc() ){
extract($row);
$dataSet->addPoint(new Point("{$name} {$ratings})", $ratings));
}
//finalize dataset
$chart->setDataSet($dataSet);
//set chart title
$chart->setTitle("Tiobe Top Programming Languages for June 2012");
//render as an image and store under "generated" folder
$chart->render("generated/1.png");
//pull the generated chart where it was stored
echo "<img alt='Pie chart' src='generated/1.png' style='border: 1px solid gray;'/>";
}else{
echo "No programming languages found in the database.";
}
?>
</body>
</html>
you should not end the <form> until all fields has been printed.
you have a
</form>
<select>....</select>
please output the select first an then close the form. Otherwise the value of your select field will not be submited.

Categories