FPDF保存并查看保存的PDF

时间:2018-02-11 00:39:44

标签: php fpdf

我有一个使用FPDF转换为PDF的表单

我正在寻找一种方法将PDF存储在网站上的一个文件夹中,并有一个页面,它按名称显示在列表中。

我确实试过谷歌,但一无所获。

我的FPDF代码:

<?php
//set the question values
$questions     = array(
'name' => "Name: ",
'email' => "Email: ",
'fax' => "Fax Number: ",
'privateaddress' => "Address (Private): ",
'companyaddress' => "Address (Company): ",
'contactprivate' => "Contact Number (Private): ",
'contactcompany' => "Contact Number (Company): ",
'Q1' => "Question 1: ",
'Q2' => "Question 2: ",
'Q3' => "Question 3: ",
'Q4' => "Question 4: ",
'Q5' => "Question 5: ",
'Q6' => "Question 6: ",
'Q7' => "Question 7: ",
'Q8' => "Question 8: ",
'Q9' => "Question 9: ",
'Q10' => "Question 10: ",
'Q11' => "Question 11: ",

);
 //set the question answers
$name          = $_POST['name'];
$email         = $_POST['email'];
$fax           = $_POST['fax'];
$privateaddress= $_POST['privateaddress'];
$companyaddress= $_POST['companyaddress'];
$contactprivate= $_POST['contactprivate'];
$contactcompany= $_POST['contactcompany'];
$question1= $_POST['Q1'];
$question2= $_POST['Q2'];
$question3= $_POST['Q3'];
$question4= $_POST['Q4'];
$question5= $_POST['Q5'];
$question6= $_POST['Q6'];
$question7= $_POST['Q7'];
$question8= $_POST['Q8'];
$question9= $_POST['Q9'];
$question10= $_POST['Q10'];
$question11= $_POST['Q11'];
//set the question names
$Name  = $questions['name'];
$Email  = $questions['email'];
$Fax  = $questions['fax'];
$Privateaddress  = $questions['privateaddress'];
$Companyaddress  = $questions['companyaddress'];
$Contactprivate  = $questions['contactprivate'];
$Contactcompany  = $questions['contactcompany'];
$q1  = $questions['Q1'];
$q2  = $questions['Q2'];
$q3  = $questions['Q3'];
$q4  = $questions['Q4'];
$q5  = $questions['Q5'];
$q6  = $questions['Q6'];
$q7  = $questions['Q7'];
$q8  = $questions['Q8'];
$q9  = $questions['Q9'];
$q10  = $questions['Q10'];
$q11  = $questions['Q11'];

require('fpdf.php');

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('images/logo.png', 10, 6, 30);

    $this->SetFont('Arial', 'B', 15);

    $this->Cell(50);


    $this->Cell(90, 10, 'Claim Form', 'C');
    // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{

    $this->SetY(-15);

    $this->SetFont('Arial', 'I', 8);

    $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
}
}


$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('Arial', 'B', 10);
//insert questions and answers
$pdf->MultiCell(150, 10, sprintf("%s %s", $Name, $name));

$pdf->MultiCell(150,10,sprintf("%s %s", $Email, $email));

$pdf->MultiCell(150,10,sprintf("%s %s", $Fax, $fax));

$pdf->MultiCell(150,10,sprintf("%s %s", $Privateaddress, $privateaddress));

$pdf->MultiCell(150,10,sprintf("%s %s", $Contactprivate, $contactprivate));

$pdf->MultiCell(150,10,sprintf("%s %s", $Companyaddress, $ccompanyaddress));

$pdf->MultiCell(150,10,sprintf("%s %s", $Contactcompany, $contactcompany));

$pdf->MultiCell(150,10,sprintf("%s %s", $q1, $question1));

$pdf->MultiCell(50,10,sprintf("%s %s", $q2, $question2));

$pdf->MultiCell(150,10,sprintf("%s %s", $q3, $question3));

$pdf->MultiCell(50,10,sprintf("%s %s", $q4, $question4));

$pdf->MultiCell(150,10,sprintf("%s %s", $q5, $question5));

$pdf->MultiCell(50,10,sprintf("%s %s", $q6, $question6));

$pdf->MultiCell(150,10,sprintf("%s %s", $q7, $question7));

$pdf->MultiCell(50,10,sprintf("%s %s", $q8, $question8));

$pdf->MultiCell(150,10,sprintf("%s %s", $q9, $question9));

$pdf->MultiCell(50,10,sprintf("%s %s", $q10, $question10));

$pdf->MultiCell(150,10,sprintf("%s %s", $q11, $question11));
//display pdf
$fileName = 'New Claim By:' . $_POST['name'] . '.pdf';
$pdf->Output($fileName, 'D');

1 个答案:

答案 0 :(得分:0)

我猜你提供的代码会给你一个pdf文档,但不是将它保存在服务器上,而是在浏览器中显示它。 另请注意,我强烈建议您查看正确转发您正在从$_POST变量中检索的数据,因为您当前的代码可能会因任意数量的注入攻击而受到影响,因为例如,通过将../../foldername/添加到文件名输入来修改存储文档的文件夹,或者甚至更糟糕(取决于您是否还将数据存储到数据库中,......)。

从FPDF的文档中,您可以找到要传递给$pdf->Output()方法的参数,将文件保存到硬盘驱动器而不是将其提供给浏览器。所以你应该改变这一行: $pdf->Output('F', $fileName);

  

输出

     

说明将文档发送到给定目标:浏览器,文件或   串。在浏览器的情况下,可以使用PDF查看器或a   下载可能会被迫。如果需要,该方法首先调用Close()   终止文件。参数

     

DEST       目的地在哪里发送文件。它可以是以下之一:

    I: send the file inline to the browser. The PDF viewer is used if available.
    D: send to the browser and force a file download with the name given by name.
    F: save to a local file with the name given by name (may include a path).
    S: return the document as a string.

The default value is I.  name
The name of the file. It is ignored in case of destination S.
The default value is doc.pdf.string Output([string dest [, string name [, boolean isUTF8]]])

然后您将有另一个脚本使用php的scandir函数列出目录中的文件。更多信息here 这只是一个更简单的选择,还有很多其他选择。