使用FPDF将数据库中的图像转换为PDF


Image from database into PDF using FPDF

我有一个从iPad应用程序发送到SQL数据库的图像。我可以使用以下php:检索此图像并在网页中显示

$img = base64_encode($row['photoData']);
echo "<img src='"data:image/jpg;charset=utf8;base64, $img'"/>";

这显示良好。我现在想做的是使用FPDF将此图像放入PDF文档中,但我很难做到这一点。

此:

$img = base64_encode($row['photoData']);
$pdf->Image($img);

给出以下错误:

FPDF错误:图像文件没有扩展名,也没有指定类型:

所以我尝试了这个(尽管我意识到我必须考虑如何对图像的大小进行排序):

$pdf->Image($img, 20, 20, 20, 20 'JPG');

给我的是:

FPDF错误:缺少或不正确的图像文件:

正确的方法是什么?

或者,将图像临时保存到服务器,然后将保存的图像放入PDF文档会更容易吗?

如以上评论中所述,这可以通过使用流("data url")将图像数据移交给fpdf库来实现,而无需将物理文件写入磁盘:

<?php
// load the 'fpdf' extension
require('fpdf.php');
// just for demonstration purpose, the OP gets the content from a database instead
$h_img = fopen('img.jpg', "rb");
$img = fread($h_img, filesize('img.jpg'));
fclose($h_img);
// prepare a base64 encoded "data url"
$pic = 'data://text/plain;base64,' . base64_encode($img);
// extract dimensions from image
$info = getimagesize($pic);
// create a simple pdf document to prove this is very well possible: 
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello Image!');
$pdf->Image($pic, 10, 30, $info[0], $info[1], 'jpg');
$pdf->Output();

如果这是一个好的建议,那就是另一个问题,这只是为了证明这是可能的。。。

根据文档,FPDF::Image接受文件名作为第一个参数,而不是二进制blob。

如果要专门使用FPDF,请先将图像保存到临时文件中,然后将其传递给FPDF::Image

要做到这一点,像这样的东西应该起作用:

$tmpFile = tempnam(sys_get_temp_dir(), 'fpdfimg');
if (file_put_contents($tmpFile, $row['photoData'])) {
    $fpdf->Image($tmpFile);
    // save/display image
    unlink($tmpFile);
}

或者,如果你只想将图像作为PDF(没有其他内容)提供,你可以使用Imagick:

$im = new 'Imagick();
$im->readImageBlob($row['photoData']);
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;

由于FPDF无法使用base64数据在PDF上生成图像,我建议将文件永久保存到磁盘,而不是为每个PDF操作编写临时文件。

这将为您节省大量I/O开销。


假设您的表有唯一的photo_idphoto_name作为photoData,那么您可以使用类似的东西来创建图像并在FPDF中使用它们。

我还假设您有一个last_updatephoto_extension列。

<?php
$path = '/path/to/fpdf/images/';
$filename = $row['photo_id'].'.'.$row['photo_extension'];
$filepath = $path.$filename;
// If a physical file is not available then create it
// If the DB data is fresher than the file then make a new file
if(!is_file($filepath) || strtotime($row['last_update']) > filemtime($filepath))
{
    $result = file_put_contents($filepath, $row['photoData']);
    if($result === FALSE)
    {
        die(__FILE__.'<br>Error - Line #'.__LINE__.': Could not create '.$filepath);
    }
}
$pdf->Image($filepath);

如果您计划更新存储在数据库中的photoData,则必须确保也有一个时间戳列,并将该时间戳与磁盘上映像的filemtime($filepath)进行比较。

另一个解决方案;)

通过复制和粘贴(fpdf的一段代码经过编辑)来创建一个新的php:

require('fpdf.php');
class DATAIMAGE extends FPDF
{
protected function _parsedata($file)
{
    // Extract info from a JPEG file
    $a = getimagesizefromstring($file);
    if(!$a)
        $this->Error('Missing or incorrect image file: '.$file);
    if($a[2]!=2)
        $this->Error('Not a JPEG file: '.$file);
    if(!isset($a['channels']) || $a['channels']==3)
        $colspace = 'DeviceRGB';
    elseif($a['channels']==4)
        $colspace = 'DeviceCMYK';
    else
        $colspace = 'DeviceGray';
    $bpc = isset($a['bits']) ? $a['bits'] : 8;
    return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$file);
}
}

然后在主php中调用这个php而不是fpdf.php。

您现在可以通过在功能末尾添加"数据"来显示图像:

$pdf->Image($mysqlrow["blob"],0,0,40,0,'data');