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

使用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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算