首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FPDF,Laravel 5.2

FPDF,Laravel 5.2
EN

Stack Overflow用户
提问于 2016-08-18 06:45:08
回答 1查看 11.2K关注 0票数 1

我试着用Laravel5.2生成pdf文件。我已经成功地将fpdf安装到我的laravel上,因为它可以生成一个简单的pdf。代码如下:

代码语言:javascript
复制
<?php
namespace App\Helpers;

use Fpdf;

class ReportHelper{
    public static $elementPerPage = 10;

    public static function generatePDF()
    {
        Fpdf::AddPage();
        Fpdf::SetFont('Arial','B',16);
        Fpdf::Cell(40,10,'Hello World!');
        Fpdf::Output();
        exit;
}
}

?>

然后,我将学习教程2中的http://www.fpdf.org/示例。如果我们想要创建一个新的页眉或页脚,我们需要编写如下代码:

代码语言:javascript
复制
<?php
require('fpdf.php');

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(30,10,'Title',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().'/{nb}',0,0,'C');
}
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
?>

因此,我在reportHelper文件{ ReportHelper}下面创建了一个新类,如下所示:

代码语言:javascript
复制
class PDF extends Fpdf
{
     //Header
    function Header()
    {
        //Logo
        $this->Image('mangekyo2.JPG',18,6,25,20,'JPG'); //posisi (x,y), image size
        // Arial bold 15
        $this->SetFont('Arial','B',15);
        // Move to the right
        $this->Cell(70); //title position
        // Title
        $this->Cell(60,10,'Hisoka PDF Title',1,0,'C');//box size
        // 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().'/{nb}',0,0,'C');
    }
}

然后在generatePDF方法{ ReportHelper类}中,我修改了代码如下:

代码语言:javascript
复制
class ReportHelper{
    public static $elementPerPage = 10;

    public static function generatePDF()
    {

        // Fpdf::AddPage();
        // Fpdf::SetFont('Arial','B',16);
        // Fpdf::Cell(40,10,'Hello World!');
        // Fpdf::Output();
        // exit;

        //Instanciation of inherited class
        $pdf = new PDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Times','',12);
        for($i=1;$i<=60;$i++){
            $pdf->Cell(0,10,'Printing line number '.$i,0,1);
            if($i == 10 || $i== 20 || $i == 30) {
                $pdf->Cell(0,10,'Hisokaline... '.$i,0,1);
                $pdf->Cell(0,10,'X : '.$pdf->GetX().', Y : '.$pdf->GetY(),0,1);
                $pdf->Image('mangekyo2.JPG',48,$pdf->GetY(),25,20,'JPG');

            }
        }
        $pdf->Output();
}
}

当我浏览/运行这个页面时,它会出现一个错误

代码语言:javascript
复制
Call to undefined method App\Helpers\PDF::AliasNbPages()

我知道AliasNbPages()是Fpdf中的一个方法,但是为什么不能识别它呢?它不仅是AliasNbPages(),而且AddPages和SetFont也没有定义,有方法在laravel .中调用此方法吗?我试过了

代码语言:javascript
复制
public static function generatePDF()
    {
        Fpdf::Header()
        {
            $this->SetFont('Arial','B',15);
            // Move to the right
            $this->Cell(70); //title position
            // Title
            $this->Cell(60,10,'Hisoka PDF Title',1,0,'C');//box size
            // Line break
            $this->Ln(20);
        };
}

不太管用..。

EN

回答 1

Stack Overflow用户

发布于 2017-01-09 06:24:04

首先,使用以下编写器命令安装库Anouar

composer require anouar/fpdf

配置

然后打开文件/config/app.php,添加以下行:

代码语言:javascript
复制
providers' => [
/*
* Laravel Framework Service Providers...
*/
…….
Anouar\Fpdf\FpdfServiceProvider::class,<br>
**and**


'aliases' => [
…
'Fpdf' => Anouar\Fpdf\Facades\Fpdf::class,

控制器

要导出pdf,请将此源代码添加到Controller :

代码语言:javascript
复制
public function exportpdf() {
 $pdf = new Fpdf();
 $pdf::AddPage();
 $pdf::SetFont('Arial','B',18);
 $pdf::Cell(0,10,"Title",0,"","C");
 $pdf::Ln();
 $pdf::Ln();
 $pdf::SetFont('Arial','B',12);
 $pdf::cell(25,8,"ID",1,"","C");
 $pdf::cell(45,8,"Name",1,"","L");
 $pdf::cell(35,8,"Address",1,"","L");
 $pdf::Ln();
 $pdf::SetFont("Arial","",10);
 $pdf::cell(25,8,"1",1,"","C");
 $pdf::cell(45,8,"John",1,"","L");
 $pdf::cell(35,8,"New York",1,"","L");
 $pdf::Ln();
 $pdf::Output();
 exit;
}

路由

不要忘记添加路由,在文件/app/Http/rouderes.php中添加以下一行:

代码语言:javascript
复制
Route::get('exportpdf', 'YourController@exportpdf');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39011542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档