Php 从mysql数据库生成带有图像的PDF文件
我是PHP新手,我一直在尝试用数据库中的图像创建一个PDF文件。我看过的所有教程都只在标题中放了一个图像(不是来自数据库)并且只从数据库中提取文本以放入PDF。我正在使用FPDF创建我的PDF文件。任何实现这一点的指导或帮助都将不胜感激。假设您有一个名为Php 从mysql数据库生成带有图像的PDF文件,php,Php,我是PHP新手,我一直在尝试用数据库中的图像创建一个PDF文件。我看过的所有教程都只在标题中放了一个图像(不是来自数据库)并且只从数据库中提取文本以放入PDF。我正在使用FPDF创建我的PDF文件。任何实现这一点的指导或帮助都将不胜感激。假设您有一个名为images的表,其中图像url存储在url列下。您可以使用FPDF和MySQL适配器从所有图像中构造PDF,如下所示: require 'fpdf/fpdf.php'; // DB parameters $host = "localhost"
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');
工具书类
$query=“SELECT imageField FROM yyy WHERE…”$结果=mysql\u查询($query)$row=mysql\u fetch\u assoc($result)$image=$row['imageField']$pdf->MemImage($image,50,30)
但是要使用MemImage,你必须从这个链接下载mem_image.php,如果我在数据库中有很多图像,我想检索它们的所有图像,因为这个解决方案只显示我表中的第一个图像,该怎么办