unable to print pdf (MPDF) - php

I am trying to print my content to PDF using Mpdf tool . The code is working in localhost , But when i tired same code in server its not working giving some eroor "mPDF error: Some data has already been output to browser, can't send PDF file".
My code is :
<?php
$address = "banglore rt nagar";
$template_data = " hello this is test ##ADDRESS## adress";
$template_data = str_replace('##ADDRESS##', $address , $template_data);
ob_end_clean();
include 'MPDF57/mpdf.php';
$mpdf=error_reporting(E_STRICT);
$mpdf=new mPDF('win-1252','A4','','',15,10,16,10,10,10);
$mpdf->Bookmark('Start of the document');
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($template_data);
$mpdf->Output();
exit();
?>
i have tried lot of solutions . but nothing works good . any help ?

Use ob_start(); after the <?php tags . hope it will helps you .
more ohttp://php.net/manual/en/function.ob-start.php

Related

How to fix iframe rendering of Dompdf output in WordPress admin?

I'm trying to render a custom pdf invoice with the help of the Dompdf library in my custom post type admin editing screen. I've setup a metabox to show the iframe with the pdf.
This is the code I'm using:
function invoice_metabox_callback() {
require plugin_dir_path( __FILE__ ) . '/dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf;
$dompdf->loadHtml('Hello world');
$dompdf->render();
$output = $dompdf->output();
//file_put_contents(plugin_dir_path( __FILE__ ) . '/pdf-test.pdf', $output);
echo '<iframe src="data:application/pdf;base64,' . $output . '"></iframe>';
}
The iframe is loading, but the pdf viewer is giving the following error: ERR_INVALID_URL
Any idea?
Thanks in advance, have a great day.

data being sent but not being included in pdf file

Im using mpdf to generate pdf which works great,
but when I send data from js that being sent perfectly but not included my pdf
tried to send the data in different ways and include it in php as
$name=$_post['name'];
and use the variable $name
or write it directly as $_post['name'].
ps.when I create a variable such as $name="namename"
it can be included
require_once __DIR__ . '../vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$style = '<style>'.file_get_contents('pdf_styles.css') . '</style>';
$name = $_POST['first_name'];
$name2="namename";
$firstpage = $mpdf->AddPage(); //cover page
//header of first page
$data .= '<div class="centered">'.'<h1>'. $name . '</h1>' . '</div>' . '<br/>' . '</br>';
$mpdf->WriteHTML($style);
$mpdf->WriteHTML($data);
....
no error msgs

Warning file_get_contents in DOM Parser

My case is, I want to scrap a website, which is success, and I'm using PHP cURL. The problem start when I want to use the DOM Parser to get the content I want. Here is the warning came out:
the error image is here
And the code I use is here. Before this code, I scrap a website using cURL, it's working, but just this part got error :
include 'simple_html_dom.php';
//Here is where I scraping, no need to show it
$fp = fopen(dirname(__FILE__) . '/airpaz.html', 'w');
//$html contain the page I scrap
fwrite($fp, $html);
fclose($fp);
$html_content = file_get_contents(dirname(__FILE__) . '/airpaz.html');
echo $html_content;
$html2 = new simple_html_dom();
$html2->load_file($html_content);
Hope you guys can help, thanks
It looks like you are trying to read a file 3 times:
$read_file = fread($fr, filesize(dirname(__FILE__) . '/airpaz.html'));
and:
$html_content = file_get_contents($read_file);
and:
$html2->load_file($html_content);
In the last two instances, instead of a file-name you pass html contents to the function so that will not work.
You should read the file only once and use string functions on the contents you receive. Or you open the url directly in $html2->load_file().
try this code
include 'simple_html_dom.php';
$html_content = file_get_html(dirname(__FILE__) . '/airpaz.html');
echo $html_content;
$html2 = new simple_html_dom();
$html2->load_file($html_content);

a href is not working for dynamic url in mpdf

I am getting problem in anchor tag while export to PDF.Anchor tag is working for static url like http:google.com but it is not working for dynamic url.I am using mpdf module for PDF.
$url = $fullBaseUrl.'/designers/attachment/time/'.$value['filetime'].'/uploadTab/imgattach';
// http://localhost/msme_latest/designers/attachment/time/1394432246/uploadTab/imgattach
$html= ''.$value['filename'].'';
// echo $html; die;
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
When i echo my code $html it is giving my link properly.But when i export this code in PDF it is not giving any kind of link PDF.
Any help will be appriciated.
For solve this problem you have to make just i change in mpdf.php
Actual Code is in mpdf.php on line no.20146
if(isset($vetor[1]) and $vetor[1] != '') //LINK
{
if (strpos($vetor[1],".") === false && strpos($vetor[1],"#") !== 0) //assuming every external link has a dot indicating extension (e.g: .html .txt .zip www.somewhere.com etc.)
{
//Repeated reference to same anchor?
/*
while(array_key_exists($vetor[1],$this->internallink)) $vetor[1]="#".$vetor[1];
$this->internallink[$vetor[1]] = $this->AddLink();
$vetor[1] = $this->internallink[$vetor[1]];
*/
}
$this->HREF = $vetor[1]; // HREF link style set here ******
}
and you have just comment line of code (line number:20151 to 20153)
/*
while(array_key_exists($vetor[1],$this->internallink)) $vetor[1]="#".$vetor[1];
$this->internallink[$vetor[1]] = $this->AddLink();
$vetor[1] = $this->internallink[$vetor[1]];
*/
and your pdf will accept all link include "localhost" and other external links.
use Ip address instead of localhost or use Live server url it is working for me
<?php
$fullBaseUrl = "http://127.0.0.1/meme_latest";
$url = $fullBaseUrl.'/designers/attachment/time/'.$value['filetime'].'/uploadTab/imgattach';
//(http://localhost/msme_latest/designers/attachment/time/1394432246/uploadTab/imgattach)
$html= 'Test link';
//echo $html; die;
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>

MPDF No Output (Blank Page)

I'd installed the MPDF utility in order to convert HTML&CSS to PDF reports.
So far things have been working just fine, until I've tried converting certain page to PDF ,and there's no output.
I have to mention that i'm able to display the page regularly through browser - the problem only comes up when i'm trying to convert it to PDF - then I receive blank page. Moreover, there are no encoding problems (part of the output is written in Hebrew, but I've already overcame this obstacle)
Here's part of the code :
if($customer!=$tempCustomer)
{
if($tempCustomer!="")
{
$html.=("</table>");
$html.=("</BR>סהכ".$sumTotal."</BR>");
$html.=("</BR>משטחים".$sumPallets."</BR>");
}
$sumTotal=0; //RESET SUM OF EACH CUSTOMER
$sumPallets=0; //RESET PALLETS COUNT
$html.=("</div>");
$html.=("<div class='subTable'>");
// $html.=("לקוח: ".$customerName."</br>");
$sumTotal=0;
$sumPallets=0;
$tempCustomer=$customer;
$html.=("<table border='3'
<tr><td>מגדל</td><td>תאריך</td><td>תעודה</td><td>פריט</td><td>סוג</td><td>גודל</td><td>כמות</td><td>משקל</td><td>מחיר
מכירה</td><td>סכום</td><td>משטחים</td></tr>");
$html.=("<tr>");
$html.=("<td>".$grower."</td>");
$html.=("<td>".$date."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$item."</td>");
$html.=("<td>".$type."</td>");
$html.=("<td>".$size."</td>");
$html.=("<td>".$quantity."</td>");
$html.=("<td>".$weight."</td>");
$html.=("<td>".$price."</td>");
$html.=("<td>".$total."</td>");
$html.=("<td>".$pallet."</td>");
$html.=("</tr>");
$sumTotal+=$total;
$sumPallets+=$pallet;
}
else
{
$html.=("<tr>");
$html.=("<td>".$grower."</td>");
$html.=("<td>".$date."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$item."</td>");
$html.=("<td>".$type."</td>");
$html.=("<td>".$size."</td>");
$html.=("<td>".$quantity."</td>");
$html.=("<td>".$weight."</td>");
$html.=("<td>".$price."</td>");
$html.=("<td>".$total."</td>");
$html.=("<td>".$pallet."</td>");
$html.=("</tr>");
$sumTotal+=$total;
$sumPallets+=$pallet;
}
/*
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("</tr>");
*/
}
$html2='אבדרכדכגכגכגכג';
$html3='אבדרכדכגכגכגכג';
//==============================================================
//MPDF SETTINGS - CONTINUE
$mpdf->SetAutoFont();
$mpdf->autoFontGroupSize = 1;
$mpdf->SetDirectionality('rtl');
$mpdf->useLang = true;
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
Any suggestions?
Thanks in advance
Have you tried debugging it? Per mpdf's site: If you get nothing but a blank screen on your browser, it may be because there is a script error. Turn on debugging at the start of your script.
<?php
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->debug = true;
$mpdf->WriteHTML("Hallo World");
$mpdf->Output();
?>
If the above works, then it's something with your code. Sometime, even a single space before any html output can throw off MPDF
First of all, If you get nothing but a blank screen on your browser, it may be because there is a script error. Turn on debugging at the start of your script.
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$mpdf->WriteHTML("Hello World");
$mpdf->Output();
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception
// name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
After that if there is any error like
Data has already been sent to output, unable to output PDF file
This means before creating pdf with mPDF some data is stored in the buffer which is sended to the browser. Therefore it is unable to create PDF.
Just do this..
Add this below php built-in function at the first line of your page were you are preparing data for pdf.
op_start();
And add this php built-in function before mPDF code (before where you are calling mpdf)
ob_end_flush();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();
So that it will clear all buffer output before processing mPDF.
Make sure if you use any functions then keep it in the same page.
<?php
include_once("mpdf-master/mpdf.php");
include_once('../../../../wp-load.php');
$htm = get_template_directory_uri().'/admin/_invoice.php?id='.$_GET['id'];
$html = file_get_contents("$htm");
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
?>
You can try link this.but you will get seconde page blank but in first page you will get the detail.
Thanks
Sanket.

Categories