我是PHP的新手,我一直在尝试使用数据库中的图像创建一个PDF文件。我所看到的所有教程只在Header中放入一个图像(而不是从数据库中),只从数据库中提取文本放入PDF.i我正在使用FPDF来创建我的PDF文件。任何指导或帮助实现这一点将不胜感激。
答案 0 :(得分:1)
我们假设您有一个名为images
的表格,其中图片网址存储在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');