FPDF:输出php文件而不是pdf文件

时间:2014-03-19 10:24:18

标签: php image pdf

我正在使用FPDF将表格数据输出到PDF文件中。但是当我点击提交按钮将数据发布到generatepdf.php文件时,它输出generatepdf.php文件而不是右边的PDF文件。下面是我的代码。< / p>

HTML

<form action="generatepdf.php" method="post" enctype="multipart/form-data" onsubmit="return validation();" >

<input type="text" name="business_name" id="business_name" /></br>


<input type="file" name="logo" id="logo" accept="image/*" ></br>
<input type="submit" value="generate" name="submit" />
</form>

generatepdf.php

require('fpdf/fpdf.php');

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["logo"]["name"]);
$extension = end($temp);
if ((($_FILES["logo"]["type"] == "image/gif")|| ($_FILES["logo"]["type"] == "image/jpeg")|| ($_FILES["logo"]["type"] == "image/pjpeg")|| ($_FILES["logo"]["type"] == "image/x-png")
|| ($_FILES["logo"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["logo"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["logo"]["error"] . "<br>";
    }
  else
    {
      if (file_exists("upload/" . $_FILES["logo"]["name"]))
      {
      echo $_FILES["logo"]["name"] . " already exists. ";
      }
      else
      {
        $logo = date('dmYHis').$_FILES["logo"]["name"];
        move_uploaded_file($_FILES["logo"]["tmp_name"], "images/" . $logo);
        $pdf=new FPDF();

        //set document properties
        $pdf->SetAuthor('Suraj Ahir');
        $pdf->SetTitle('Test');

        //set font for the entire document
        $pdf->SetFont('Helvetica','B',20);
        $pdf->SetTextColor(50,60,100);

        //set up a page
        $pdf->AddPage('P');


        //insert an image and make it a link
        $image = 'D:\wamp\www\exportpdf\1.jpg';
        $pdf->Image($image,10,20,33,0,' ','http://www.fpdf.org/');


        $logoimage = "D:\wamp\www\\exportpdf\images\\".$logo;
        $pdf->Image($logoimage,10,220,25,25);

        //Output the document
        $name = date('dmYHis');
        $pdf->Output($name.'.pdf','I'); 
      }
    }
 }

1 个答案:

答案 0 :(得分:0)

您必须将pdf文件保存在服务器本地,然后将用户重定向到该文件。

所以你必须编辑代码的输出部分,如下所示:

$pdf->Output($name.'.pdf','F'); // F is saving the file to the given filename
header('Location: ' . $name . '.pdf'); // redirect the user to the generated pdf file

这可能是你问题的解决方案(也许你必须稍微调整一下代码。)