如何在 LARAVEL 4 上使用 TCPDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20417664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:44:13  来源:igfitidea点击:

How to use TCPDF on LARAVEL 4

phphtmllaravellaravel-4tcpdf

提问by melvnberd

Good Day,

再会,

Can anyone help me how to use TCPDF in Laravel 4, I mean from installing on via composer update to generating a pdf from the view or through the controller. Ive tried to search on Google but I cant find a comprehensive tutorial on how to use it on Laravel 4. Sorry for this noobish question :) .

任何人都可以帮助我如何在 Laravel 4 中使用 TCPDF,我的意思是从通过 Composer 更新安装到从视图或通过控制器生成 pdf。我试过在谷歌上搜索,但我找不到关于如何在 Laravel 4 上使用它的综合教程。抱歉这个菜鸟问题:)。

Thanks for the help. I will really appreciate it. Have a good day!

谢谢您的帮助。我会很感激的。祝你有美好的一天!

回答by JamesG

The definitive version of TCPDF can be added to your Laravel application by adding the tecnick.com/tcpdf package to your composer.json file as follows:

通过将 tecnick.com/tcpdf 包添加到您的 composer.json 文件中,可以将 TCPPDF 的最终版本添加到您的 Laravel 应用程序中,如下所示:

"require": {
    "laravel/framework": "4.0.*",
    "tecnick.com/tcpdf": "6.0.*", // This is the line to add

Running composer updatewill add the library to your project.

运行composer update会将库添加到您的项目中。

You can then instantiate instances of TCPDFand work with it as per the documentation.

然后,您可以TCPDF根据文档实例化它的实例并使用它。

Here's an example of a method that you could add to a controller that would construct a very simple PDF file and send it to the browser for download:

这是一个方法示例,您可以将其添加到控制器中,该控制器将构建一个非常简单的 PDF 文件并将其发送到浏览器以供下载:

public function getPdftest()
{
    $pdf = new TCPDF();

    $pdf->SetPrintHeader(false);
    $pdf->SetPrintFooter(false);
    $pdf->AddPage();
    $pdf->Text(90, 140, 'This is a test');
    $filename = storage_path() . '/test.pdf';
    $pdf->output($filename, 'F');

    return Response::download($filename);
}

回答by devo

To install TCPDF, add this in your composer.json file require block,

要安装 TCPDF,请将其添加到您的 composer.json 文件 require 块中,

"laurentbrieu/tcpdf": "dev-master"

Now it looks like,

现在看起来,

....

"require": {
        "laravel/framework": "4.0.*",
        "laurentbrieu/tcpdf": "dev-master"
    },
......

Then update using composer, run the following in your project root.

然后使用 Composer 更新,在您的项目根目录中运行以下命令。

composer update

Now you can call TCPDF functions in your laravel project by adding alias.

现在你可以通过添加别名在你的 Laravel 项目中调用 TCPDF 函数。

See this for more details - https://packagist.org/packages/laurentbrieu/tcpdf

有关更多详细信息,请参阅此内容 - https://packagist.org/packages/laurentbrieu/tcpdf