0

Possible Duplicate:
Create a PDF file with PHP

i need help..i want a php code to download information stored in the mysql database and save it in a pdf file.. so far, below is the code i have but it's not printing anything.. please help me to modify it or send me another better code. thanks..

<?php
$host = 'localhost';
$user = 'root';
$pass = 'hello';
$db = 'version';
$table = 'childinfo';
$file = 'export';
$csv_output;

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");


$gotten =mysql_query("select sex,childnumber from child_nfo");
$row =mysql_fetch_assoc($gotten);
$bytes = $row[imgdata];
header("Content-type: application/pdf");
header('Content-disposition: attachment; filename="thing.pdf"');
print $bytes;
?> 
Community
  • 1
  • 1
user1424183
  • 21
  • 1
  • 1
  • this is not how you create a pdf file –  Dec 06 '12 at 08:16
  • What is `$row[imgdata]`? That's not valid PHP syntax. If you just forgot the quotes, it won't work because you didn't select a column called `imgdata` from the table. P.S. Don't use mysql_XXX functions, use mysqli or PDO. – Barmar Dec 06 '12 at 08:41

2 Answers2

1

You can use tcpdf for creating pdf. try to use tcpdf in the following format. Besides you can have tcpdf documentation to see how to use it. I'm sharing the code which is only an example of how you can create pdf file using tcpdf.

require_once('tcpdf/tcpdf.php');

        error_reporting(0);
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('your author name');
$pdf->SetTitle('title');
$pdf->SetSubject('subject');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('dejavusans', '', 10);

// add a page
$pdf->AddPage();

// writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)

// create some HTML content
$html = $pdf_content;
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
//$pdf->Output('example_006.pdf', 'I');
$pdf_filename = time().".pdf";
$pdf->Output('destination_directory/'.$pdf_filename, 'F');

You can put your database information that you fetch in $pdf_content variable. Hope this help you..

Md. Mahbubul Haque
  • 800
  • 10
  • 18
0

I assume $byes is a bytestream of an image? You can't just print it out and expect it to be a formatted PDF file. These classes might be useful in generating PDFs:

FPDF: http://www.fpdf.org/

TCPDF: http://www.tcpdf.org/

mushroom
  • 1,909
  • 3
  • 16
  • 33