📌  相关文章
📜  如何使用PHP从外部文本文件生成 PDF 文件?

📅  最后修改于: 2022-05-13 01:56:40.999000             🧑  作者: Mango

如何使用PHP从外部文本文件生成 PDF 文件?

在本文中,我们将学习如何使用PHP使用 FPDF 从外部文本文件生成 PDF 文件。它是一个免费的PHP类,包含许多用于创建和修改 PDF 的函数。 FPDF 类包括许多功能,例如页面格式、页眉、页脚、自动分页符、换行符、图像支持、颜色、链接等等。

方法:

  • 您需要从 FPDF 网站下载 FPDF 类并将其包含在您的PHP脚本中。
    require('fpdf/fpdf.php');
  • 根据您的需要实例化和使用 FPDF 类,如以下示例所示。
    $pdf=new FPDF();

示例:以下代码根据外部文本文件生成 PDF 文件,即“cIntro.txt”、“cStandard.txt”和“cPrototype.txt”。请参阅代码中的注释,以便根据应用程序的需要更好地理解和定制。

PHP
SetFont('Arial', 'B', 15);
        
        // Calculate string length 
        $w = $this->GetStringWidth($title) + 6;
        $this->SetX((210 - $w) / 2);
        
        // Set drawing color
        $this->SetDrawColor(0, 80, 180);
        
        // It defines the grey color for filling
        $this->SetFillColor(105, 105, 105);
        
        // Sets the text color
        $this->SetTextColor(255, 0, 0);
        
        // Set the line width to 1 mm)
        $this->SetLineWidth(1);
        
        // Prints a cell Title
        $this->Cell($w, 9, $title, 1, 1, 'C', 1);
        
        // Line break
        $this->Ln(10);
    }
    
    // Function to set the document footer
    function Footer() {
  
        // Set Y Position from bottom
        $this->SetY(-20);
        
        // Sets font to Arial italic 10
        $this->SetFont('Arial', 'I', 10);
        
        // Sets the Text color in gray
        $this->SetTextColor(128);
        
        // Prints a cell with Page number
        $this->Cell(0, 10, 'Page ' 
            . $this->PageNo(), 0, 0, 'C');
    }
    
    // Function to set the title for a tutorial
    function tutorialTitle($num, $label) {
  
        // Sets font to Arial 12
        $this->SetFont('Arial', '', 12);
        
        // Sets to fill Background color with Light grey 
        $this->SetFillColor(211, 211, 211);
        
        // Prints a cell with Title for tutorial
        $this->Cell(0, 6, "Chapter $num : $label", 0, 1, 'L', 1);
        
        // Line break
        $this->Ln(4);
    }
    
    // Function to set the content from a external file 
    function tutorialContent($file) {
  
        // Read text file
        $f   = fopen($file, 'r');
        $txt = fread($f, filesize($file));
        fclose($f);
        
        // Sets the font to Times 12
        $this->SetFont('Times', '', 12);
        
        // It prints texts with line breaks
        $this->MultiCell(0, 5, $txt);
        
        //Puts a Line break
        $this->Ln();
        
        // Set font in italics
        $this->SetFont('', 'I');
        
        // Prints a cell
        $this->Cell(0, 5, '(end of content)');
    }
      
    function showTutorial($num, $title, $file) {
  
        // Add a new page
        $this->AddPage();
        $this->tutorialTitle($num, $title);
        $this->tutorialContent($file);
    }
}
  
// Initiate a PDF object
$pdf   = new PDF();
$title = 'C Programming Language';
  
// Sets the document title
$pdf->SetTitle($title);
  
// Sets the document author name
$pdf->SetAuthor('gfg author name');
  
$pdf->showTutorial(
    1, 
    'C Language Introduction', 
    'cIntro.txt'
);
  
$pdf->showTutorial(
    2, 
    'C Programming Language Standard', 
    'cStandard.txt'
);
  
$pdf->showTutorial(
    3, 
    'Importance of function prototype in C', 
    'cPrototype.txt'
);
  
$pdf->Output();
?>


“cIntro.txt”文件:

“cStandard.txt”文件:

“cPrototype.txt”文件:

输出: