2012-12-06 41 views
0

可能重复:
Create a PDF file with PHPPDF下载

我需要help..i想一个PHP代码下载存储在MySQL数据库中的信息,并将其保存为PDF文件.. 到目前为止,下面是我有的代码,但它不是打印任何东西.. 请帮我修改它或给我另一个更好的代码。 谢谢..

<?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; 
?> 
+0

这不是你如何创建pdf文件 – 2012-12-06 08:16:46

+0

什么是'$ row [imgdata]'?这不是有效的PHP语法。如果你忘记引号,它将不起作用,因为你没有从表中选择一个名为'imgdata'的列。附:不要使用mysql_XXX函数,使用mysqli或PDO。 – Barmar

回答

1

您可以使用TCPDF来创建PDF文件。尝试使用以下格式的tcpdf。除此之外,你可以通过tcpdf文档来了解如何使用它。我分享的代码只是如何使用tcpdf创建pdf文件的一个例子。

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'); 

你可以把你在$pdf_content变量中获取的数据库信息。希望这对你有所帮助..