繁体   English   中英

FPDF:输出由pdf文件插入的php文件

[英]FPDF : Outputs php file insted of pdf file

我正在使用FPDF将表单数据输出到PDF file。但是当我单击Submit按钮以将数据发布到generatepdf.php文件时,它将输出generatepdf.php文件而不是正确的PDF文件。以下是我的代码。

的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>

生成pdf.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'); 
      }
    }
 }

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

因此,您必须像这样编辑代码的Output部分:

$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

这可以解决您的问题(也许您需要稍微调整一下代码)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM