【问题标题】:How to create a file in pdf extension and write contents into that file in php?如何以 pdf 扩展名创建文件并将内容写入 php 中的该文件?
【发布时间】:2017-01-11 10:36:35
【问题描述】:

我已经编写了一个用于生成 pdf 文件并将一些内容写入该特定 pdf 的 php 代码。代码如下所示:

 <?php

    session_start();


    include('../database.php');

    $id = $_GET['id'];

    $_SESSION["cusid"] = $id;


    $invname =  'invoice_'.(mt_rand(1,10000)).'i'.(mt_rand(1,10000));

    $phpFile = fopen('invoice/'.$invname.'.php', "w") or die("Could not open file.");

    $inv = $invname.'.pdf';


    $sql=mysqli_query($con,"update customer set file='$inv' where cusid='$id'");

    $phpContent = '<?php 

                      session_start(); 
                      include("../database.php"); 


                      $id = $inv;

                      echo $id;
     '; 

     fwrite($phpFile, $phpContent);
     fclose($phpFile);
?>

但是使用此代码,会创建 pdf 文件,但是当打开特定的 pdf 时,它会显示无法打开文件的消息。

如何解决这个错误?谁能建议如何解决这个错误?

【问题讨论】:

  • PDF 文件不仅仅是另一个文本文件。为什么还要将 PHP 脚本写入 pdf 文件?
  • 使用 fpdf 或其他库生成 pdf,如 stackoverflow.com/questions/2132015/…
  • 您需要使用任何符合您要求的pdf库
  • 您将需要图书馆的帮助,但我们不应该推荐图书馆。在“PHP PDF”上进行 Google 搜索
  • 您需要一个知道如何处理并能够与 pdf 文件通信的中间件(库)。

标签: php


【解决方案1】:

FPDF 是一个 PHP 类,可让您在不使用 PDFlib 库的情况下生成 PDF 文件。 你可以在这里下载 http://www.fpdf.org/en/download.php 然后提取它并上传到您的服务器。例如到目录public_html/fpdf/

<?php

require('./fpdf.php');

$pdf=new FPDF();

$pdf->AddPage();

$pdf->SetFont('<any_font>','B',16);

$pdf->Cell(40,10,'Your string..'');

$pdf->Output();

?>

以上代码足以创建一个 pdf 文件。将此输入到一个 php 文件中并将其保存在同一目录中。在执行时,您将生成一个 pdf。

【讨论】:

    【解决方案2】:

    你应该试试FPDF。它将帮助您使用 php 制作 pdf 文件。

    这里是一些使用 php 的示例 pdf 设计

    <?php
    require('fpdf.php');//download fpdf library and include here
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(0,10,"Welcome ",3,0,'L');
    $pdf->SetFont('Arial', '', 15);
    $pdf->SetXY(160 , 30);
    $pdf->Write(10, "May 8, 2016");
    $pdf->Line(18, 45, 210-20, 45);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetXY(10 , 54);
    $pdf->Write(10, "PAY");
    $pdf->SetFont('Arial', '', 11);
    $pdf->SetXY(18.5 , 54);
    $pdf->Write(10, "ONE THOUSAND TWO HUNDRED THIRTY");
    $pdf->SetXY(18 , 75);
    $pdf->Cell(35,10,"Pay to the order of ",1,0,'C');
    $pdf->SetXY(55, 79);
    $pdf->MultiCell(95,6,"Zain Farooq",'BB','L');
    $pdf->Ln();
    $pdf->SetFont('Arial','',10);
    $pdf->setFillColor(0,0,0);
    $pdf->SetTextColor(255,255,255);
    $pdf->Cell(80, 6, 'COMPANY INFORMATION', 1,0,'C',1);
    $pdf->SetFont('Arial','',18);
    $pdf->SetTextColor(0,0,0);
    $pdf->setFillColor(230,230,230);
    $pdf->Cell(80, 23, 'Earnings statement', 1 ,0,'C',1);
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(10 , 97);
    $pdf->SetTextColor(0,0,0);
    $pdf->MultiCell(80, 17, 'Extra statements', 'BL' ,'L');
    $pdf->SetXY(10 , 103);
    $pdf->MultiCell(90,11,"",'BL','R');
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(10 , 114);
    $pdf->setFillColor(0,0,0);
    $pdf->SetTextColor(255,255,255);
    $pdf->Cell(80, 6, 'Employee Information', 1,0,'C',1);
    $pdf->Cell(40, 6, 'Social Security', 1,0,'C',1);
    $pdf->Cell(40, 6, 'Employee ID', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->SetTextColor(0,0,0);
    $pdf->SetXY(10 , 114);
    $pdf->Cell(80, 18, '4323', 1);
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(90 , 120);
    $pdf->setFillColor(255,255,255);
    $pdf->Cell(40, 4, 'XXX-XX-123', 1,0,'C',1);
    $pdf->Cell(40, 4, '123456', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(90 , 124);
    $pdf->setFillColor(0,0,0);
    $pdf->SetTextColor(255,255,255);
    $pdf->Cell(20, 4, 'Start Date', 1,0,'C',1);
    $pdf->Cell(20, 4, 'End Date', 1, 0, 'C',1);
    $pdf->Cell(40, 4, 'Check Date', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(90 , 128);
    $pdf->setFillColor(255,255,255);
    $pdf->SetTextColor(0,0,0);
    $pdf->Cell(20, 4, 'Start Date', 1,0,'C',1);
    $pdf->Cell(20, 4, 'End Date', 1, 0, 'C',1);
    $pdf->Cell(40, 4, 'Check Date', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(10 , 132.3);
    $pdf->setFillColor(0,0,0);
    $pdf->SetTextColor(255,255,255);
    $pdf->Cell(20, 6, 'Earnings', 1,0,'C',1);
    $pdf->Cell(20,6, 'Rate', 1, 0, 'C',1);
    $pdf->Cell(20,6, 'Hours', 1, 0, 'C',1);
    $pdf->Cell(20, 6, 'Current', 1,0,'C',1);
    $pdf->Cell(20, 6, 'Year to date', 1, 0, 'C',1);
    $pdf->Cell(20, 6, 'Deductions', 1, 0, 'C',1);
    $pdf->Cell(20, 6, 'Current', 1,0,'C',1);
    $pdf->Cell(20, 6, 'Year to date', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(10 , 137.3);
    $pdf->setFillColor(255,255,255);
    $pdf->SetTextColor(0,0,0);
    $pdf->Cell(20, 50, 'Earnings', 1,0,'T',1);
    $pdf->Cell(20,50, 'Rate', 1, 0, 'C',1);
    $pdf->Cell(20,50, 'Hours', 1, 0, 'C',1);
    $pdf->Cell(20, 50, 'Current', 1,0,'C',1);
    $pdf->Cell(20, 50, 'Year to date', 1, 0, 'C',1);
    $pdf->Cell(20, 50, 'Deductions', 1, 0, 'C',1);
    $pdf->Cell(20, 50, 'Current', 1,0,'C',1);
    $pdf->Cell(20, 50, 'Year to date', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','B',8);
    $pdf->SetXY(10 , 187.3);
    $pdf->setFillColor(230,230,230);
    $pdf->SetTextColor(0,0,0);
    $pdf->Cell(60, 6, 'Earnings', 1,0,'R',1);
    $pdf->SetFont('Arial','',8);
    $pdf->Cell(20, 6, '1600.00', 1,0,'C',1);
    $pdf->Cell(20, 6, '14,400.00', 1, 0, 'C',1);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(20, 6, 'Deductions', 1, 0, 'C',1);
    $pdf->SetFont('Arial','',8);
    $pdf->Cell(20, 6, '370.00', 1,0,'C',1);
    $pdf->Cell(20, 6, '3,300.00', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(10 , 193.3);
    $pdf->setFillColor(255,255,255);
    $pdf->SetTextColor(0,0,0);
    $pdf->Cell(26.66667, 6, 'Check Number', 1,0,'R',1);
    $pdf->Cell(26.66667, 6, '#6059', 1,0,'C',1);
    $pdf->Cell(26.66667, 6, 'Net Pay', 1, 0, 'C',1);
    $pdf->Cell(26.66667, 6, '$1,230', 1, 0, 'C',1);
    $pdf->Cell(26.66667, 6, 'YTD NET PAY', 1,0,'C',1);
    $pdf->Cell(26.66667, 6, '$11,070', 1, 0, 'C',1);
    $pdf->Ln();
    $pdf->Ln();
    $pdf->SetFont('Arial','',8);
    $pdf->SetXY(10 , 199.5);
    $pdf->setFillColor(0,0,0);
    $pdf->Cell(160, 5, '', 1, 0, '',1);
    $pdf->Ln();
    $pdf->Output('filename.pdf','D');//once design is completed the file will be downloaded into your browser
    ?>
    

    它只是一个示例。你可以自己设计。希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多