I am using Mpdf which is working fine locally, the trouble I am having is that the pdf wont output and just get a blank screen
Here is the error
Fatal error: Uncaught Mpdf\MpdfException: Data has already been sent to output (/customers/3/c/2/includes/header.php at line 14), unable to output PDF file in /customers/3/c/2/pdf/vendor/mpdf/mpdf/src/Mpdf.php:9510 Stack trace: #0 /customers/3/c/2: Mpdf\Mpdf->Output('sold.pdf', 'I') #1 /customers/3/c/2/include('/customers/3/c/...') #2 {main} thrown in /customers/3/c/2/pdf/vendor/mpdf/mpdf/src/Mpdf.php on line 9510
Couple of things I have tried, encoding is correct at UTF and NOT DOM, I have no white space, and everything seems to be ok
This is the header code
<?php
session_start();
if(empty($_SESSION['user_id']))
{
header("Location: https://www.home.co.uk");
}
require '../dbcon/database.php';
$db = DB();
require '../lib/library.php';
$app = new WIDGET();
require '../lib/carbon.php';
$user = $app->UserDetails($_SESSION['user_id']);
?>
I have also tried
<?php
session_start();
require '../dbcon/database.php';
$db = DB();
require '../lib/library.php';
$app = new WIDGET();
require '../lib/carbon.php';
$user = $app->UserDetails($_SESSION['user_id']);
?>
Which has made no difference.
And this is the code in line 9510 of Mdpf.php
case Destination::INLINE:
if (headers_sent($filename, $line)) {
throw new \Mpdf\MpdfException(
sprintf('Data has already been sent to output (%s at line %s), unable to output PDF file', $filename, $line)
);
}
Like mentioned all I am getting is a blank page, if I output to an iframe it works ok.
Works fine on 127.0.0.1
Any help much appreciated and thanks in advance for any help
Update:
Sorry forgot to mention or show the code calling the pdf.
I have already tried ob_clean() and still no go
require_once 'pdf/vendor/autoload.php';
if(ob_get_length() > 0) {
ob_clean();
}
$mpdf = new \Mpdf\Mpdf();
$html ='<html>
<body>
<h2 style="font-size: 120px; text-align:center;">SOLD</h2>
<h2 style="font-size: 50px; text-transform: uppercase; text-align:center;">'. $row->widget_model .'</h2>
<h2 style="font-size: 50px; text-transform: uppercase; text-align:center;">'. $row->widget_serial .'</h2><br>
<h2 style="font-size: 30px; text-transform: uppercase; text-align:center;">Delivery Date: '. (new DateTime($_POST['expdelivery']))->format('d-m-Y') .'</h2>
<br>
<h2 style="font-size: 20px; text-align:center;">Contact: '. $_POST['name'] .'</h2>
</body>
</html>';
$mpdf->WriteHTML($html);
$mpdf->Output('sold.pdf','I');
ob_end_flush();
}
EDIT 2:-
Looking at the network Tab it is just showing all the js calls and favicon.ico
It also shows this page updatewidget.php?id=2178
<?php
$page_title = "Widget Tracker - Amend Widget";
$page = '';
include "includes/header.php";
include "includes/sidebar.php";
$id = isset($_GET['id']) ? $_GET['id'] : NULL;
$sth = $db->prepare("SELECT * FROM `widgettracker` WHERE `id` = :id");
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->setFetchMode(PDO::FETCH_OBJ);
$sth->execute();
$row = $sth->fetch();
?>
<section class="content">
<div class="container-fluid">
<?php
if($row->widgetstatus == "Sold")
{include "sold.php";}
else
{include "widgetdetails.php";}
?>
</div>
</section>
<?php
include "includes/footer.php";
include "includes/customjs.php";
?>
And the page that has the Mpdf on is "widgetdetails.php" I need to insert a Sold date and update and generate the pdf. Then the error
You might have a blank line or newline after the closing ?> in your header file. Try removing the closing ?> from your php files that are not actually outputting anything.
You will want to check the network tab in your developer console. You should be able to see the raw data returned by the server, which should show you whatever data it is sending before it reaches the mpdf code.
Related
Screenshot of issue
I am creating a messaging application and want to make emoji images viewable rather than showing their codes in the messages.
I have used an emoji picker js file for entering them in the text area but in sent messages the emoji icon is not showing.
I use the following function :
function loadMessages($token){
// this function is loads all the messages from the database
$db = connect();
$me = $_SESSION['id'];
$query = $db->prepare("SELECT * FROM messages WHERE (fromm=:fromm1 AND too=:too1) OR (fromm=:too2 AND too=:fromm2) ");
$query->bindParam(':fromm1',$me);
$query->bindParam(':too1',$token);
$query->bindParam(':too2',$token);
$query->bindParam(':fromm2',$me);
$query->execute();
$found = $query->rowCount();
if($found){
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$from = $row['fromm'];
$to = $row['fromm'];
$message = $row['message'];
$text = $row['message'];
$html = preg_replace("/\\\\u([0-9A-F]{2,5})/i", "&#x$1;", $message);
if($from == $me){
$realMessage = "<div class='me'> $html <br /><br /></div>";
} else {
$realMessage = "<div><div class='you'>$html<br /><br /></div></div>";
}
echo $realMessage;
}
The main index.php includes this:
<div class="display-message" style="position: absolute; width: 100%; padding: 10px; background: inherit; bottom: 0;">
</div>
Hi and welcome to Stack Overflow.
There is an encoding issue. You are seeing the Latin output of the characters rather than Unicode (is your database encoding set to something other than UTF-8?).
Get PHP to output the following header before any other output:
header('Content-type: text/html; charset=utf-8');
Add the following meta tag in your <head> section:
<meta charset="utf-8">
And in your loop:
...
while($row=$query->fetch(PDO::FETCH_ASSOC)){
$from=$row['fromm'];
$to=$row['fromm'];
$message=preg_replace("/\\\u([0-9A-F]{2,5})/i","&#x$1;",$row['message']);
$message=mb_convert_encoding($message,'UTF-16','HTML-ENTITIES');
$html=mb_convert_encoding($message,'UTF-8','UTF-16');
...
I'm using wkhtmltopdf to generate my pdf files. I've left the generation alone for some time and for whatever reason it's not generating the header and footer anymore.
Things I've tried so far (will update this when more answers come in):
Having the doctype, html, head and body tags ion the header and footer
Chaning the paths to the header and footer to absolute paths (Even using complete absolute paths from the drive forward C:/xampp... does not work.) It might be worth pointing out that changing the filename to something that doesn't exist does not throw an error. So I don't know if it finds the files. Maybe someone can tell me a good way to test this?
This is my header file:
<!DOCTYPE html>
<html>
<head>
<title>PDF header</title>
<style>
html {
display: block;
}
body {
font-family: Calibri, "Segoe Ui Regular", sans-serif;
letter-spacing: 0px;
}
</style>
</head>
<body style="padding-top: 30px">
<img src="../../images/logo_extra.jpg" style="width: 100%;"/>
</body>
</html>
This is my main file:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
// header('Content-Disposition: attachment; filename="offerte.pdf"');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
// I know there is a 'cover' function in WKHTMLTOPDF
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
And as always, please:
Give me an explanation and maybe an example but not just a bunch of working code!
PS: If you see any other mistakes, please let me know.
wkhtmltopdf has an issue with header/cover/footer. I didn't dig into it very deep as adding margins did solve it for me:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
//just set margins
$pdf->setOption('margin-top', 20);
$pdf->setOption('margin-bottom', 15);
$pdf->setOption('margin-left', '0');
$pdf->setOption('margin-right', '0');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
Description
Second problem is weird - nonexisting filename should throw an error. Comment out header and try then with false filename, snappys AbstractGenerator should say something...
I have strange problem, I make for example 3 files main.html style.css index.php,
then when I open index.php which read main.html and print to output I get different appearance than when I opening main.html, do you know what I making wrong ?
HTML:
<!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>aa</title>
<link rel="stylesheet" type="text/css" href="style2.css"/>
</head>
<body>
<div id="container">
a
</div>
</body></html>
CSS:
body
{
width: 100%;
padding:0;
margin: 0;
background: #f6f6f6;
}
#container
{
margin: 0 ;
padding: 0;
background: #f6f6f6;
}
PHP:
<?php
$output = file_get_contents("main2.html");
echo $output;
?>
This main2.html
This is index.php
Where you have this:
<?php
$output = file_get_contents("main2.html");
echo $output;
?>
Just simply change to
<?php include ('main2.html');
Use the PHP Require Function.
//PHP
<?php
require 'main.html';
?>
Also you should set the width: 100% on the header not on the body.
http://php.net/manual/en/function.file-get-contents.php
file_get_contents — Reads entire file into a string
http://php.net/manual/en/function.include.php
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
So better use include or require. Note : I am writing from a mobile that's why it is not so formatted.
I agree with #jordan davis "Also you should set the width: 100% on the header not on the body."
I find solution, i must set php file encoding to ANSI or I set all files encoding UTF-8 without BOM + header('Content-Type: text/html; charset=utf-8'); to php file.
In my last template system I used this function:
private function read_file($filename){
$code = '';
if(file_exists($filename)){
$templatefile = fopen($filename, 'r');
while(!feof($templatefile)){
$code = $code.fgets($templatefile, 1024);
}
fclose($templatefile);
}
return $code;
}
That works perfectly for me. Insted of file_get_content im using fopen to open the File and then with fgets im reading the Content and finally it has to be closed with fclose. See the PHP.net for further Information about the function.
You can also add some Code for a 404 Message if your Template File doesn't exist.
Okay I want to apologize in advance because I'm having trouble articulating what the root cause of the problem is with this site that I'm attempting to build. So if I'm way off in outer space...=/
Anyways, the first issue I'm having is that when I attempted to fix an issue with "include" function in php I'm getting this error (below) the reason why I was tampering with it at all was because my navs aren't working. I keep getting a 404 whenever I attempt to leave the index.php page.
Warning: include(/home/content/22/10350022/html/../Setup.php) [function.include]: failed to open stream: No such file or directory in /home/content/22/10350022/html/index.php on line 6
Warning: include() [function.include]: Failed opening '/home/content/22/10350022/html/../Setup.php' for inclusion (include_path='.:/usr/local/php5_3/lib/php') in /home/content/22/10350022/html/index.php on line 6
Here is the code from index.php, setup, and navs:
Index.php ---below
> > <?php
// Setup Document:
//include (1) - forces the site to load the file one time but we're not going to include set up file more than once
define('APP_PATH', dirname(__FILE__) . "/../");
include(APP_PATH . "Setup.php");
if (isset($_GET['page']) && $_GET['page'] ==! ''){
$pg =$_GET['page'];
} else {
$pg = 'Home';
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="Css/styles.css">
<style type="text/css">
body {
background-color: #039;
}
</style>
</head>
<body>
<div class ="header">
<?php include('Template/header.php');?>
</div>
<div class ="main_Nav">
<?php include('Template/main_Nav.php');?>
</div>
<div class ="main_Content">
<?php include('Content/'.$pg.'.php');?>
<?php #$Home = "SELECT body FROM Pages WHERE Id=1";
#$d = '$Home, $conn';
#if ($d == false) {
#echo "FAILURE".mysql_error($conn);
#}
?>
<div class ="main_Footer">
<?php include('Template/footer.php');?>
</div>
</body>
</html>
Some stuff is commented out on purpose so I could work on this issue.
Navs below --
<?php
## Main Navigation Document
include('/Config/setup.php');
?>
<ul>
<li>Home</li>
<li>Services</li>
<li>Free Stuff</li>
<li>Blog</li>
<li>About Us</li>
</ul>
Finally - setup -- some info is blacked out obviously and some stuff is commented out on purpose
<?php
## Setup Document
//Host - location of database on server etc
//username
//pswd
//database name
//connection variables
$host = "";
$user = "test";
$pwd = "";
$db = "test";
//connection info
$conn = mysqli_connect ($host, $user, $pwd, $db);
if ($conn ==false) {
echo "connection has failed";
}
//fetching the title of pages
#$sql = "SELECT title FROM pages WHERE Id=2";
//da warnings
#$r = '$sql,$conn';
# echo $r;
# echo "sucess!";
// return
#if ($r == false) {
# echo "FAILURE".mysql_error($conn);
#}
?>
Originally I was just using include('/Config/setup.php'); which works. But whenever I attempt to navigate to content featured in my navs bar...i get a 404 error. Now this should only change the content box -- the rest of the page should remain static. <?php include('Content/'.$pg.'.php');?> Which is what I think I'm doing here. Or at least I hope it is lol. $pg is the string 4(Home) and unless page is otherwise defined it will be defaulted to home. At least that is what I THINK I wrote above.
my file structure just really fast is this:
...root folder on FTP
-content
-config
-images
-Css
-templates
-Stats
-Roodyinfo
Now this path it keeps asking for...does that have to do with my hosting service? Cause it seems like its saying I have to define an ABSOLUTE path???
All help is welcome and appreciated. Thanks!
Just put all the files you need in the same directory and use require('filename'); with no paths.
Hello everyone I wonder if someone could help me with this issue I have been using the Twitter API class for over a year now fine. Last week I moved to a grid hosting plan from shared hosting now everytime I use the API get the following errors.
Uncaught exception 'EpiOAuthException' in /home/content/98/3412598/html/twitter/EpiOAuth.php:434 Stack trace:
#0 /home/content/98/3412598/html/twitter/EpiOAuth.php(404): EpiOAuthException::raise(Object(EpiCurlManager), false)
#1 /home/content/98/3412598/html/twitter/EpiOAuth.php(45): EpiOAuthResponse->__get('oauth_token')
#2 /home/content/98/3412598/html/twitter/ufollowme.php(13): EpiOAuth->getAuthenticateUrl() #3 {main} thrown in /home/content/98/3412598/html/twitter/EpiOAuth.php on line 434
I am using this library http://code.google.com/p/php-twitter/downloads/detail?name=php-twitter-1.1.zip&can=2&q=
and here is the simpletest code which I have used all along.
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
$consumer_key = 'REMOVED';
$consumer_secret = 'REMOVED';
$token = 'REMOVED';
$secret= 'REMOVED';
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $token, $secret);
$twitterObjUnAuth = new EpiTwitter($consumer_key, $consumer_secret);
?>
<h1>Single test to verify everything works ok</h1>
<h2>View the source of this file</h2>
<div id="source" style="display:none; padding:5px; border: dotted 1px #bbb; background-color:#ddd;">
<?php highlight_file(__FILE__); ?>
</div>
<hr>
<h2>Generate the authorization link</h2>
<?php echo $twitterObjUnAuth->getAuthenticateUrl(); ?>
<hr>
<h2>Verify credentials</h2>
<?php
$creds = $twitterObj->get('/account/verify_credentials.json');
?>
<pre>
<?php print_r($creds->response); ?>
</pre>
<hr>
<h2>Post status</h2>
<?php
$status = $twitterObj->post('/statuses/update.json', array('status' => 'This a simple test from twitter-async at ' . date('m-d-Y h:i:s')));
?>
<pre>
<?php print_r($status->response); ?>
</pre>
<script> function viewSource() { document.getElementById('source').style.display=document.getElementById('source').style.display=='block'?'none':'block'; } </script>
Any help welcomed. Thank you
Found the fix guys if anyone else needs to answer its remove lines 171-172 on the EPIOAUTH.php file.
// if(isset($_SERVER ['SERVER_ADDR']) && !empty($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] != '127.0.0.1')
// curl_setopt($ch, CURLOPT_INTERFACE, $_SERVER ['SERVER_ADDR']);