-2

I am using the following code to generate pdf in php but when I open file it shows corrupted or damaged file error.

$FileName = date("d-m-y") . '.pdf';
$Content = "";

# Titlte of the CSV
$Content = "Name,Address,Age,Phone \n";

# fill data in the CSV
$Content .= "\"John Doe\",\"New York, USA\",15,65465464 \n";

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $FileName . '"');
echo $Content;
exit();
Adam Brown
  • 2,812
  • 4
  • 28
  • 39
user2185041
  • 99
  • 5
  • 12
  • What do you use for generating PDF file? Any library? As far as I see if this is all the code - you have to render the PDF file with the use of library like **UTCPDF** (good UTF support). – Rolice Apr 24 '13 at 08:21
  • 1
    Use mpdf, its best, I have used it. http://www.mpdf1.com/mpdf/download – Maulik Vora Apr 24 '13 at 08:22
  • 2
    tcpdf is better than those said before this comment. fact. – STT LCU Apr 24 '13 at 08:23
  • try to write the file hosting server and see in pdf is created properly.usually when i face this problem, its either the header function or output buffer thant causes the problem – Deepak Srinivasan Apr 24 '13 at 08:28
  • 2
    Do you _seriously_ think that if you just _label_ any kind of text data (CSV format here) as “this is a PDF”, it will magically become one …? – CBroe Apr 24 '13 at 08:43

3 Answers3

3

PDF is a binary file, you can't do it this way. I'd suggest to use some PDF framework, like mPDF. PHP itself has also libraries for PDF, but it's still much better (and less effort) to use a framework.

mikiqex
  • 5,023
  • 2
  • 24
  • 22
  • Please avoid fpdf. Its best days are long over. I've been using it for five years now, and wishing I could switch. – Mchl Apr 24 '13 at 08:58
1

I have used http://www.tcpdf.org/ in the past, works very well and is very complete and FLOSS.

smarques
  • 708
  • 7
  • 22
0

Try Using MPDF library http://www.mpdf1.com/mpdf/download

this library is robust and can convert html to pdf directly