我在PHP中使用FPDF库来创建PDF文档,它一切正常。但是,根据用户选择的选项,有时生成的PDF只是一个空白页面。有没有办法可以显示一条消息,说明"没有结果符合您的标准"而不只是显示一个空白页?
以下是我使用
的代码<?php
ini_set('max_execution_time', 600); //600 seconds = 10 minutes
//Get vars from POST
$getstart = $_POST['datestart'];
$getend = $_POST['dateend'];
$userstaff = $_POST['staff'];
$usertask = $_POST['task'];
$taskcheck = isset($_POST['taskcheck']) ? true : false;
function reverse_strdate ($date)
{
$d = substr($date,0,2);
$m = substr($date,3,2);
$y = substr($date,6,4);
$nd = $y.'-'.$m.'-'.$d;
return($nd);
}
function re_reverse_strdate ($date)
{
$y = substr($date,0,4);
$m = substr($date,5,2);
$d = substr($date,8,2);
$nd = $d.'-'.$m.'-'.$y;
return($nd);
}
$startdate = reverse_strdate($getstart);
$enddate = reverse_strdate($getend);
//SET UP PDF DOC
require('fpdf.php');
$pdf=new FPDF();
//set document properties
$pdf->SetAuthor('Me');
$pdf->SetTitle('Work Instruction');
$pdf->SetAutoPageBreak(false, 0);
//set font for the entire document
$pdf->SetFont('Helvetica','',20);
$pdf->SetTextColor(74,74,74);
$pdf->SetMargins(14,0,14);
//MYSQL QUERY TO RETRIEVE DATA
mysql_connect("localhost","root","");
mysql_select_db("wi_gen");
if ($userstaff == "All Staff" && $usertask == "All Tasks")
{
$query="SELECT * FROM tasks WHERE start BETWEEN '$startdate' AND '$enddate'";
}
else if ($userstaff != "All Staff" && $usertask != "All Tasks" && $taskcheck = true)
{
$query="SELECT * FROM tasks WHERE (start BETWEEN '$startdate' AND '$enddate') AND (task_staff_assName = '$userstaff') AND (title LIKE '%{$usertask}%')";
}
else if ($userstaff != "All Staff" && $usertask != "All Tasks")
{
$query="SELECT * FROM tasks WHERE (start BETWEEN '$startdate' AND '$enddate') AND (task_staff_assName = '$userstaff') AND (task_type = '$usertask')";
}
else if ($userstaff != "All Staff")
{
$query="SELECT * FROM tasks WHERE (start BETWEEN '$startdate' AND '$enddate') AND (task_staff_assName = '$userstaff')";
}
else if ($usertask != "All Tasks" && $taskcheck = true)
{
$query="SELECT * FROM tasks WHERE (start BETWEEN '$startdate' AND '$enddate') AND (title LIKE '%{$usertask}%')";
}
else if ($usertask != "All Tasks")
{
$query="SELECT * FROM tasks WHERE (start BETWEEN '$startdate' AND '$enddate') AND (task_type = '$usertask')";
}
$result=mysql_query($query);
$num=mysql_numrows($result);
//Loop through data and create pages
$i=0;
while ($i < $num) {
//GET DATA AND STORE TO VARIABLES
$project_ref=mysql_result($result,$i,"task_jobName");
$assigned_to=mysql_result($result,$i,"task_staff_assName");
//Start Time
$task_start=mysql_result($result,$i,"start");
$task_start= re_reverse_strdate($task_start);
//Finish Time
$task_end=mysql_result($result,$i,"end");
$task_end= re_reverse_strdate($task_end);
//Get Task Details and Title
$title=mysql_result($result,$i,"title");
$description=mysql_result($result,$i,"description");
//Get Task Id for API Request
$id=mysql_result($result,$i,"id");
//Get Custom Fields
$cust_tasks_url = file_get_contents("https://api.workflowmax.com/job.api/task/".$id."/customfield?apiKey=14C10292983D48CE86E1AA1FE0F8DDFE&accountKey=D7091F50FB6B4A5B858B2D54D7739F9C");
$xml = new SimpleXMLElement($cust_tasks_url);
foreach ($xml->CustomFields->CustomField as $field)
{
//Component Reference
if ($field->ID == "26696")
{
$component_ref = $field->Text;
}
//Work Istruction Ref
if ($field->ID == "26700")
{
$wi_ref = $field->Text;
}
//Machine
if ($field->ID == "27125")
{
$machine = $field->Text;
}
//Drawing 1 Ref
if ($field->ID == "26698")
{
$drawing_ref_1 = $field->Text;
}
//Drawing 2 Ref
if ($field->ID == "26699")
{
$drawing_ref_2 = $field->Text;
}
//Drawing 3 Ref
if ($field->ID == "26697")
{
$drawing_ref_3 = $field->Text;
}
}
//CREATE PAGES
$pdf->AddPage('P');
//display the title
$pdf->SetFontSize(26);
$pdf->Cell(67,45,'Work Instruction',0,0,'C',0);
//Add the logo
$pdf->image('logo.jpg',140,8);
//Project Name Label
$pdf->SetXY (14,40);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Project Ref:');
//Project Ref String
$pdf->SetXY (40,40);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,$project_ref);
//WI Label
$pdf->SetXY (120,40);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'WI Ref:');
//WI String
$pdf->SetXY (137,40);
$pdf->SetFontSize(9);
$pdf->SetFont('','');
$pdf->Write(5,$wi_ref);
//Component Ref Label
$pdf->SetXY (14,50);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Component Ref:');
//Component Ref String
$pdf->SetXY (49,50);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,$component_ref);
//Assigned To Label
$pdf->SetXY (120,50);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Assigned To:');
//Assigned To String
$pdf->SetXY (149,50);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,$assigned_to);
//Start Date Label
$pdf->SetXY (14,60);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Start Date:');
//Start Date String
$pdf->SetXY (37,60);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5 ,$task_start);
//Finish Date Label
$pdf->SetXY (120,60);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Finish Date:');
//Finish Date String
$pdf->SetXY (146,60);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5, $task_end);
//Drawing Ref Label
$pdf->SetXY (120,75);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Drawing Ref(s):');
//Drawing Ref String (1)
$pdf->SetXY (120,80);
$pdf->SetFontSize(10);
$pdf->SetFont('','');
$pdf->Write(5,$drawing_ref_1);
//Drawing Ref String (2)
$pdf->SetXY (120,85);
$pdf->SetFontSize(10);
$pdf->SetFont('','');
//$pdf->Write(5,$drawing_ref_2);
//Drawing Ref String (3)
$pdf->SetXY (120,90);
$pdf->SetFontSize(10);
$pdf->SetFont('','');
$pdf->Write(5,$drawing_ref_3);
//Machine Label
$pdf->SetXY (14,77);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Machine:');
//Machine String
$pdf->SetXY (34,77);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,$machine);
//Est. Hours Label
$pdf->SetXY (14,90);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Est. Hours:');
//Est. Hours String
$pdf->SetXY (39,90);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,'8');
//Description Seperation
$pdf->SetTextColor(200,200,200);
$pdf->SetXY (12,100);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'____________________________________________________________________________');
//Title Label
$pdf->SetTextColor(74,74,74);
$pdf->SetXY (14,110);
$pdf->SetFontSize(14);
$pdf->SetFont('','B');
$pdf->Write(5,$title);
//Description Label
$pdf->SetXY (14,117);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(6,$description);
//Actual Hours Label
$pdf->SetXY (146,200);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,'Actual Hours: ');
//Actual Hours Input
$pdf->Cell(18,6,'',1,0,'C',0);
//Completed Declaration
$pdf->SetTextColor(200,200,200);
$pdf->SetXY (12,207);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'____________________________________________________________________________');
//Completed Declaration
$pdf->SetTextColor(74,74,74);
$pdf->SetXY (14,217);
$pdf->SetFontSize(12);
$pdf->SetFont('','I');
$pdf->Write(5,'I have completed the above work to the required standard and in accordance with all relevant health and saftey regulations.');
//Completed Signed
$pdf->SetXY (14,234);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Signed:');
//Completed Signature Line
$pdf->SetXY (32,234);
$pdf->SetFontSize(12);
$pdf->SetFont('','U');
$pdf->Write(5,' ');
//Completed Signed Date
$pdf->SetXY (140,234);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Date:');
//Seperator
$pdf->SetTextColor(200,200,200);
$pdf->SetXY (14,243);
$pdf->SetFontSize(12);
$pdf->SetFont('','');
$pdf->Write(5,'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -');
//Supervisor Note
$pdf->SetTextColor(74,74,74);
$pdf->SetXY (14,250);
$pdf->SetFontSize(8);
$pdf->SetFont('','');
$pdf->Write(5,'TO BE COMPLETED BY SUPERVISOR');
//Checked Declaration
$pdf->SetXY (14,260);
$pdf->SetFontSize(12);
$pdf->SetFont('','I');
$pdf->Write(5,'I have checked that the above work has been completed to the required standard and in accordance with all relevant health and saftey regulations.');
//Checked Signed
$pdf->SetXY (14,277);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Signed:');
//Checked Signature Line
$pdf->SetXY (32,277);
$pdf->SetFontSize(12);
$pdf->SetFont('','U');
$pdf->Write(5,' ');
//Checked Signed Date
$pdf->SetXY (140,277);
$pdf->SetFontSize(12);
$pdf->SetFont('','B');
$pdf->Write(5,'Date:');
$i++;
}
mysql_close();
//Output the document
$file_start_date = re_reverse_strdate ($startdate);
$file_end_date = re_reverse_strdate ($enddate);
$pdf->Output('work_instructions - '.$file_start_date.' to '.$file_end_date.'.pdf','I');
?>