2013-07-24 54 views
-2

我是PHP新手,我一直在尝试使用数据库中的图像创建一个PDF文件。我看过的所有教程只将图像放在标题中(而不是从数据库),并且只从数据库中提取文本以放入PDF中。我正在使用FPDF创建我的PDF文件。任何指导或帮助实现此目的将不胜感激。使用来自mysql数据库的图像生成PDF文件

回答

1

比方说,您有一个名为images的表格,其中图像URL存储在列url的列下。您可以使用FPDF和MySQL适配器构建一个PDF的所有这样的图像的:

require 'fpdf/fpdf.php'; 

// DB parameters 
$host = "localhost"; 
$user = "username"; 
$pass = "password"; 
$db = "db"; 

// Create fpdf object 
$pdf = new FPDF('P', 'pt', 'Letter'); 

// Add a new page to the document 
$pdf->addPage(); 

// Try to connect to DB 
$r = mysql_connect($host, $user, $pass); 
if (!$r) { 
    echo "Could not connect to server\n"; 
    trigger_error(mysql_error(), E_USER_ERROR); 
} else { 
    echo "Connection established\n"; 
} 

// Try to select the database 
$r2 = mysql_select_db($db); 
if (!$r2) { 
    echo "Cannot select database\n"; 
    trigger_error(mysql_error(), E_USER_ERROR); 
} else { 
    echo "Database selected\n"; 
} 

// Try to execute the query 
$query = "SELECT * FROM images"; 
$rs = mysql_query($query); 
if (!$rs) { 
    echo "Could not execute query: $query"; 
    trigger_error(mysql_error(), E_USER_ERROR); 
} else { 
    echo "Query: $query executed\n"; 
} 

while ($row = mysql_fetch_assoc($rs)) { 
    // Get the image from each row 
    $url = $row['url']; 

    // Place the image in the pdf document 
    $pdf->Image($url); 
} 

// Close the db connection 
mysql_close(); 

// Close the document and save to the filesystem with the name images.pdf 
$pdf->Output('images.pdf','F'); 

参考

+0

其实我没找到正确答案我的问题 '$ query =“SELECT imageField FROM yyy WHERE ...”; $ result = mysql_query($ query); $ row = mysql_fetch_assoc($ result); $ image = $ row ['imageField']; $ pdf-> MemImage($ image,50,30);' 但是要使用MemImage,你必须从这个链接下载mem_image.php http://www.fpdf.org/en/script/script45.php –

+0

如果我在数据库中有很多图像,并且我想要检索所有图像,因为此解决方案仅显示我在表格中的第一个图像 –

相关问题