【问题标题】:PHP to PDF using FPDF failing to open my pdf file(broken)使用 FPDF 的 PHP 到 PDF 无法打开我的 pdf 文件(损坏)
【发布时间】:2013-09-11 03:31:34
【问题描述】:

只是想问一下为什么我的 pdf 文件没有在 nitro pdf 中打开。我正在使用 fpdf。我只是想在我的 pd 中有一个花哨的表,但似乎有一个错误。谁能看出来是哪个?这是代码:

<?php

mysql_select_db($database_iexam, $iexam);
$query_getstudent = sprintf("SELECT * FROM exam_students WHERE stud_subject_id = %s", GetSQLValueString($colname_getstudent, "int"));
$getstudent = mysql_query($query_getstudent, $iexam) or die(mysql_error());
$row_getstudent = mysql_fetch_assoc($getstudent);
$totalRows_getstudent = mysql_num_rows($getstudent);

//defining objects
  $subject_id = $_SESSION['subject_id'];
  $getsub = mysql_query("SELECT subject_description FROM exam_subjects WHERE subject_id = '$subject_id'")or die(mysql_error());
  while($gotit = mysql_fetch_array($getsub)){
  $subj = $gotit['subject_description'];
  }

require_once("fpdf/fpdf.php");

class PDF extends FPDF
{

function FancyTable()
{
    // Colors, line width and bold font
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    // Header
    $w = array(40, 35, 40, 45);
        $this->Cell($w,7,$header["Name"],1,0,'C',true);
        $this->Cell($w,7,$header["ID Number"],1,0,'C',true);
        $this->Cell($w,7,$header["Passwords"],1,0,'C',true);

    $this->Ln();
    // Color and font restoration
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    // Data
    $fill = false;

    do {
  $shanks = $row_getstudent['stud_valid_nos'];
    $mugiwara = mysql_query("SELECT * FROM exam_sections WHERE sec_id_num = '$shanks'")or die(mysql_error());
    while($ben = mysql_fetch_array($mugiwara)){
    $beckman = $ben['sec_firstname']; 
    $usopp = $ben['sec_lastname'];
    }

    $trafalgar = $row_getstudent['stud_id'];
  $vergo = mysql_query("SELECT pass_password FROM exam_passwords WHERE pass_user_id = '$trafalgar'")or die(mysql_error());
    while($caesar = mysql_fetch_array($vergo)){
    $smoker = $caesar['pass_password'];
    }

        $this->Cell($w[0],6,"$beckman $usopp",'LR',0,'L',$fill);
        $this->Cell($w[1],6,"Shanks",'LR',0,'R',$fill);
        $this->Cell($w[2],6,"$smoker",'LR',0,'R',$fill);
        $this->Ln();
        $fill = !$fill;


} while ($row_getstudent = mysql_fetch_assoc($getstudent));


    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}
}
$pdf = new PDF();
$pdf->AddPage();
$pdf->FancyTable();
$pdf->Output();
$pdf->SetAutoPageBreak(true, 20);
$pdf->SetMargins(20, 20, 20);
$pdf->Cell(40,10,'PDF for '.$subj);
$pdf->Ln(10);
$pdf->Output();?>

【问题讨论】:

  • 用txt编辑器打开文件,看看里面有没有php错误提示

标签: php mysql pdf fpdf


【解决方案1】:

$header 在这些行的第三个参数中未定义:

$this->Cell($w,7,$header["Name"],1,0,'C',true);
$this->Cell($w,7,$header["ID Number"],1,0,'C',true);
$this->Cell($w,7,$header["Passwords"],1,0,'C',true);

【讨论】:

  • 好的。我的pdf刚刚工作。呵呵。但我的数据库中的记录没有显示
  • 我也没有在任何地方看到 $getstudent 定义,如果我正确地遵循您的代码,这就是数据的来源。
  • 获取学生是一个记录集。等我把它贴在那里。
  • 它在 FancyTable 函数中不可用,因此您不会看到数据库中的任何数据。
【解决方案2】:

在输出之前像上面的 ob_start() 和 ob_clean() 那样做

ob_start();

$pdf = new PDF();
$pdf->AddPage();
$pdf->FancyTable();
$pdf->Output();
$pdf->SetAutoPageBreak(true, 20);
$pdf->SetMargins(20, 20, 20);
$pdf->Cell(40,10,'PDF for '.$subj);
$pdf->Ln(10);

ob_clean();

$pdf->Output();

在输出之前像上面的 ob_start() 和 ob_clean() 那样做

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    相关资源
    最近更新 更多