duanhuayong6687
2013-07-24 06:21
浏览 88
已采纳

使用mysql数据库中的图像生成PDF文件[关闭]

I am new to PHP and i have been trying to create a PDF file with images from the database.All the tutorial i have seen only put an image in the Header(not from the database) and only pull text from the database to put in the PDF.i am using FPDF to create my PDF files.Any guidelines or help to achieve this will be appreciated.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dopq87915 2013-07-24 06:40
    最佳回答

    Let's say that you have a table called images, where the image URLs are stored under the column url. You can use FPDF and the MySQL adapter to construct a PDF out of all of the images like this:

    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
    ";
        trigger_error(mysql_error(), E_USER_ERROR);
    } else {
        echo "Connection established
    "; 
    }
    
    // Try to select the database
    $r2 = mysql_select_db($db);
    if (!$r2) {
        echo "Cannot select database
    ";
        trigger_error(mysql_error(), E_USER_ERROR); 
    } else {
        echo "Database selected
    ";
    }
    
    // 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
    ";
    } 
    
    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');
    

    References

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题