doumeba0486 2014-05-29 09:06
浏览 37

如何从php文件中使用FPDF

i'm using FPDF to generate a pdf file from a php file :

<?php
require('fpdf.php');

class PDF extends FPDF
{
// Chargement des données
function LoadData($file)
{
    // Lecture des lignes du fichier
    $lines = file($file);
    $data = array();
    foreach($lines as $line)
        $data[] = explode(';',trim($line));
    return $data;
}

// Tableau simple
function BasicTable($header, $data)
{
    // En-tête
    foreach($header as $col)
        $this->Cell(40,7,$col,1);
    $this->Ln();
    // Données
    foreach($data as $row)
    {
        foreach($row as $col)
            $this->Cell(40,6,$col,1);
        $this->Ln();
    }
}

// Tableau amélioré
function ImprovedTable($header, $data)
{
    // Largeurs des colonnes
    $w = array(40, 35, 45, 40);
    // En-tête
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    $this->Ln();
    // Données
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR');
        $this->Cell($w[1],6,$row[1],'LR');
        $this->Cell($w[2],6,number_format($row[2],0,',',' '),'LR',0,'R');
        $this->Cell($w[3],6,number_format($row[3],0,',',' '),'LR',0,'R');
        $this->Ln();
    }
    // Trait de terminaison
    $this->Cell(array_sum($w),0,'','T');
}

// Tableau coloré
function FancyTable($header, $data)
{
    // Couleurs, épaisseur du trait et police grasse
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
    // En-tête
    $w = array(40, 35, 45, 40);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
    $this->Ln();
    // Restauration des couleurs et de la police
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    $this->SetFont('');
    // Données
    $fill = false;
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2],0,',',' '),'LR',0,'R',$fill);
        $this->Cell($w[3],6,number_format($row[3],0,',',' '),'LR',0,'R',$fill);
        $this->Ln();
        $fill = !$fill;
    }
    // Trait de terminaison
    $this->Cell(array_sum($w),0,'','T');
}
}

$pdf = new PDF();
// Titres des colonnes
$header = array('Pays', 'Capitale', 'Superficie (km²)', 'Pop. (milliers)');
// Chargement des données
$data = $pdf->LoadData('pays.txt');
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>

But when i execute it, (php file) those warnings appear :

Warning: file(pays.txt) [function.file]: failed to open stream: No such file or directory in C:\Program Files\EasyPHP-5.3.9\www\PDF\fpdf16\test_pdf.php on line 10

Warning: Invalid argument supplied for foreach() in C:\Program Files\EasyPHP-5.3.9\www\PDF\fpdf16\test_pdf.php on line 12 FPDF error: Some data has already been output, can't send PDF file

And i have really no idea how to fix it, any suggestions and thank you !

  • 写回答

1条回答 默认 最新

  • dpa55065 2014-05-29 09:12
    关注

    You get two warnings, the second is only a follow up problem, you can ignore it. The first one indicates your problem: the file you want to load cannot be found by your script:

    $data = $pdf->LoadData('pays.txt');
    

    The string 'pays.txt' you specify here is used as the path (location) of a file to be opened in that method LoadData($file) further up. That is where the problem occurs: from the view of the php process there is no such file under that path. So opening the file cannot succeed, that is an issue that provokes further problems along the line.

    Now most likely there is some file called "pays.txt" somewhere, since you coded those lines. Most likely the file simply is not at the location you (or the script) expect it to be. So you have these alternatives:

    1. instead of specifying just the name of the file you can specify the absolute path to the file. That makes opening (searching for) the file independent of the current process. An absolute path would be something like: /some/path/to/pays.txt . Or, in that strange MS-Windows notation: C:\\some\path\to\pays.txt
    2. you can specify a relative path to the file so that the process can find and thus open it. A relative path would be something like ../../folder/pays.txt, such a path is interpreted relative to the current working directory of the executing php script.
    3. you can move the file right into the current working directory of the executing php script process, so that it is directly found under its name only, without specifying a path. This might raise security issues, so I would advise against it generally. Try to keep files coding logic and data in separate separate locations.
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算