Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在应用程序中使用FPDF_Php_Pdf_Fpdf - Fatal编程技术网

Php 在应用程序中使用FPDF

Php 在应用程序中使用FPDF,php,pdf,fpdf,Php,Pdf,Fpdf,我一直在用FPDF进行测试,在制作PDF方面进展顺利 在我的应用程序中,我计划有一个“转换为PDF”按钮,当用户单击它时,FPDF将使用给定变量在新窗口中创建PDF 生成PDF的代码如下所示: require_once("fpdf.php"); $author = "Test"; $title = "Candidate profile"; $width = 10; $height = 10; $text = "Test"; $font_family = 'Arial'; $font_size

我一直在用FPDF进行测试,在制作PDF方面进展顺利

在我的应用程序中,我计划有一个“转换为PDF”按钮,当用户单击它时,FPDF将使用给定变量在新窗口中创建PDF

生成PDF的代码如下所示:

require_once("fpdf.php");

$author = "Test";
$title = "Candidate profile";
$width = 10;
$height = 10;
$text = "Test";
$font_family = 'Arial';
$font_size = 11;
$file_name = "candidate-profile-jesse-orange";
$extension = ".pdf";
$output = "I";
$heading_size = 16;
$subheading_size = 13;

class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo
        $this->Image('logo.png',10,10,30);
        // Arial bold 15
        $this->SetFont('Arial','B',16);
        // Move to the right
        $this->Cell(50);
        // Title
        $this->Cell(100,8,'Candidate profile for: Jesse Orange',1,0,'C');
        // Line break
        $this->Ln(20);
    }

    // Page footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'R');
    }
}

// Create a new instance
$pdf = new PDF("P", "mm", "A4");

// Set some default options
$pdf->setAuthor($author);
$pdf->setTitle($title);
$pdf->AddPage();
$pdf->SetDisplayMode("real",'default');
$pdf->SetFont($font_family, '', $font_size);

// Field
$pdf->SetFont($font_family, 'B', $font_size);
$pdf->Cell(40, $height, "Position applied for:", 0, 0);

// Value
$pdf->SetFont($font_family, '', $font_size);
$pdf->Cell(60, $height, "Innovation Advisor (Job ID: 221)", 0, 0);

// Field
$pdf->SetFont($font_family, 'B', $font_size);
$pdf->Cell(40, $height, "Date of application:", 0, 0);

// Value
$pdf->SetFont($font_family, '', $font_size);
$pdf->Cell(50, $height, "17th July 2017", 0, 1);

$pdf->Output($file_name.$extension, $output);
在单独的页面上,您可以看到数据库查询的输出和每个字段的变量,例如:

  • $name=“John”
  • $name=“Smith”
或者更准确地说

$name=$row['name']

我的问题是:我应该将已经存储的变量发布到一个单独的脚本来生成PDF,还是应该在同一页上

理论上,按下按钮可以调用
$pdf->Output()

补充:

页面上显示的变量用于在浏览器中查看,然后当用户单击convert to PDF时,只需获取这些变量并将其与FPDF一起使用

类似的例子是Google Analytics如何允许您以PDF格式下载报告。

鉴于此流程:

  • 数据库值将显示给最终用户
  • 用户查看值并可选择生成PDF文件
  • 。。。如果不允许用户编辑这些值(即它们不是默认值,而是最终值),则将它们提交到服务器是没有意义的。服务器已经可以直接访问这些值,如果您使它们来自外部源,它们将成为不受信任的输入,您将被迫验证它们

    只要确保你有一个可重用的设计,这样你就不需要实现读取值代码两次


    事实上,这是我在野外偶尔看到的一种反模式。在最坏的情况下,是“修改我的个人资料”页面从URL获取用户ID(而不是从会话中获取当前已验证用户的ID)。计算风险;-)

    为什么首先需要两个脚本,因为数据来自数据库?您是否先显示变量以获得用户批准?这些变量显示在网页上以供查看,但如果您在外部提交值(如POST表单),则可以选择以PDFIf的形式进行查看,然后打开更改的可能性。这是可取的吗?我有一个想法,所以数据必须可以在给定的页面上查看。。。如果我只发布一个唯一的ID,然后在生成PDF文件的页面中进行查询,这不是更实际吗?