I'm using mpdf to convert and display html to PDF
when i run it on my local pc/server i see the pdf, but when i run it on my site server i'm getting this error:
mPDF error: Some data has already been output to browser, can't send PDF file
$url = "http://".SITE_DOMAIN."/itinPage-printVer.php?itinID=".$_GET['itinID'];
$htmlStr = file_get_contents($url);
include('html2pdf/mpdf.php');
//ob_end_clean();
$mpdf=new mPDF('utf-8');
$mpdf->WriteHTML( $htmlStr);
$mpdf->Output();
exit;
Could it be folder permissions?
I addressed the problem successfully by changing the encoding of the .php file which contains the mpdf code from UTF-8 to ANSI!
Note: The file encoding does not affect the contents of your $htmlStr variable, so the output can be in any character set you wish (utf-8, etc).
Related
IM trying to save a PDF file, generated from HTML, into the user's local using MPDF. Here is the part of the code responsable for this:
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('gestao/relatorios/relatorio_cargo_cidades', $data, true);
$mpdf->WriteHTML($html);
$mpdf->SetFooter(relatorio_footer());
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
At first I was trying to show the PDF using the "I" param from the output function, than the user could just see the PDF and choose to download it or not. But when I tried to submit a file into another website, it says that the file is not a PDF. Than I used my linux to see if the file was actually a PDF, here's what I got:
As you can see, the file is being saved as "data" for some reason. I've already tried to use the 'F' param, also from the output function, and than it worked, I saved as PDF. But the F param only save the file inside the code folder, so it was not very usefull for me.
Can anyone tell me how could I save the file as an actually PDF using MPDF?
EDIT
I think that the problem is being caused by CODEIGNITER, not by the MPDF. When I'm setting the last param from the load->view as TRUE, the returned HTML is in a data (string) form, and MPDF is not converting this properly.
Adding ob_clean() might solve your problem:
ob_clean();
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
You can simply use the D parameter for downloading:
$mpdf -> Output ('FILENAME.pdf', 'D');
or generate your own header and then output the content as string:
header ("Content-type: application/pdf");
header ("Content-Disposition: inline; filename=FILENAME.pdf");
$mpdf -> Output ('', 'S');
I tried to use MPDF library for generating PDF.
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
// Other code
$mpdf->Output("1.pdf", 'D');
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
And I get this error message.
Data has already been sent to output, unable to output PDF file
I used ob_end_clean() but not working.
I used all answers in this question but nothing works for me.
TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file
I have got the same error.
Data has already been sent to output, unable to output PDF file
This means before creating a PDF with mPDF some data is stored in the buffer which is sent to the browser. Therefore it is unable to create PDF.
To rectify this, add this below php built-in function at the first line of your page were you are preparing data for pdf.
ob_start();
And add this below 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();
This will clear the buffer output before processing mPDF.
Make sure if you use any functions to keep them on the same page, don't include a functions page where you have kept your all functions.
Read more about PHP Buffering
I just had this error and the correct solution for it is the following.
if when you type <?php in your script as the very first word, make sure there are no space characters or any characters before <?php whatsoever. This also happens with anything else giving the error Data has already been sent to output. Space characters like space and TAB don't appear to you and it will definitely deceive you.
This won't appear easily to anyone.
For me, it helped to insert <?php instead of <?.
I am new to PHP report generation. I am trying to use MPDF 5.7. When I try create sample pdf using simple php page, it is created successfully. But when I put it in to codeigniter chrome browser says "Failed to load PDF document.". But It can open using firefox. But if i downloaded it, it is not support to open using adobe reader. But still my sample pfd is working well anyway.
This is how I create sample pdf.
<?php
$html = '
<h1>mPDF</h1>
<h2>Basic Example Using CSS Styles</h2>
<p class="breadcrumb">Chapter » Topic</p>
<h3 style="color:red; background-color:gray; margin-left:100px;">Heading 3</h3>';
include("themes/MPDF57/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyleA4.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
When I put it into codeigniter cannot open pdf. my php file is created in 'view' and mpdf library also put in same place in theme folder.
Your PDF file contains some direct PHP output from your script which makes it corrupt.
It can be a whitespace from after the ?> PHP closing tag, it can be PHP notice printed during course of generating the file, etc.
Be sure that you disable all layouting your framework provides as it produces the exact same output that could cause this.
To troubleshoot further, save your PDF file to disc ($mpdf->Output('<file path>', "D");) and open it in a text editor. You will see a bunch of weird characters.
If the file does not start with %PDF-1.4, look for the reason of the output before calling $mpdf->Output();.
If there is some readable text at the end of the document, look after calling the Output method.
I'd suggest you to remove your ?> PHP closing tag anyway for a good measure.
See also https://mpdf.github.io/troubleshooting/error-messages.html
The reason this happens is that there are some PHP errors/warnings that show up before the PDF gets written, these make it a non-valid PDF therefore not readable by the Browser.
You can see it by downloading and opening the PDF file with a TextEditor and you'll see PHP errors instead of the standard %PDF-1.4 value (which should be the first value of the file).
If you suppress warnings error_reporting(E_ERROR | E_PARSE); you should be good to go.
Try this
$mpdf->Output('', "I");
so then file should be displayed inline in browser instead of downloading it.
if you want to download use "D" insted of "I"
I had the same problem, I didn't notice that header and footer (header was the problem) are still included in my index file. When I called my generate_pdf file it was pulling header and that created error. After I removed header pdf was generated successfully.
I want to generate a PDF file in my web app. I am using php and I tried fpdf library
I tried to create pdf by using following code:
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Привет мир!');
$pdf->Output();
?>
It works when we use english characters inside file, but file encoding is ANSI and inside writen russian characters I get that error:
if I change file encoding to utf-8 get error:
FPDF error: Some data has already been output, can't send PDF file (output started at Z:\home\fpdf\www\tutorial\tuto1.php:1)
Who knows how to resolve the problem, please help me !
$pdf->Cell(40,10, iconv('UTF-8', 'CP-1251', $str) );
Make sure you're saving your document as UTF8
I'm using mPDF library to provide pdf files.
Assume that I have a file named facture.php that contains :
an image in the header
a table in the body
an image in the footer of the file
And another file named convert_HTML2PDF.php that will provide the pdf output of the first file.
I would like to get the output of the facture file that is interpreted into a variable and output it as a pdf file without visualising in the browser.
How can I do that and use the output inside the convert_HTML2PDF.php?
$pdf = $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN)
will output the PDF into a string - no need to create temp file.
Use file_get_contents() to get html content of your facture.php :
$output = file_get_contents('http://www.example.com/facture.php');
$mpdf->Output('filename.pdf', 'F');
will send the output to a file on your server instead of a browser.
See:
http://mpdf1.com/manual/index.php?tid=125
For more output variations.