【问题标题】:how to generate pdf using blade view with tcpdf laravel 5?如何使用带有 tcpdf laravel 5 的刀片视图生成 pdf?
【发布时间】:2015-08-10 12:29:27
【问题描述】:

我正在尝试使用 laravel 5 和 https://github.com/elibyy/laravel-tcpdf 创建一个 pdf

当我的客户填写表格并单击提交时,我有一个 20 页的表格在 laravel Blade 中书写,我想为他生成 pdf 并保存

我尝试在我的控制器中这样做

public function createPDF()
{
    $pdf = Input::get('pdf',null);
    $company = Input::get('company',null);
    $branch = Input::get('branch',null);
    $sub_branch = Input::get('sub_branch',null);
    $form_type = Input::get('form_type',null);
    $form_name = Input::get('form_name',null);
    $form_heb_name = Input::get('form_heb_name',null);
    $sig_path=FormsController::getSignature_file();

    $data=compact('company','branch','sub_branch','form_type','form_name','form_heb_name','sig_path');
    Input::flash();
    if ($pdf) 
        { 
            $pdf = new TCPDF();
            $pdf->SetPrintHeader(false);
            $pdf->SetPrintFooter(false);
            $pdf->AddPage();
            $pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
            $filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
            $pdf->output($filename, 'I');
            return redirect('forms');
        }
     return view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name , $data);
}

bat 它不起作用,它创建了 2 个所有字段相互重叠的 pdf 页面

如何解决?

另外,我想以无法编辑的方式保存 pdf 文件,我该怎么做?

谢谢。

【问题讨论】:

  • 您找到解决方案了吗?
  • 不是我使用的 tcpdf link

标签: php laravel-5 tcpdf


【解决方案1】:

我会尽量给你一步一步的答案(针对 laravel 5.1 测试):

使用前,请确保您正确安装了TCPDF服务提供者:

安装 TCPDF 服务提供者

在 composer.json 中,添加这个包:

"require": {
    "elibyy/laravel-tcpdf": "0.*"
}

运行作曲家更新

在 config/app.php 中添加这个服务提供者

'providers' => [
    //..
    Elibyy\TCPDF\ServiceProvider::class,
]

运行 php artisan vendor:publish ,这将在你的文件中生成 config/laravel-tcpdf.php

在控制器中生成 pdf

public function createPDF()
{
    [...]
    //get default settings from config/laravel-tcpdf.php
    $pdf_settings = \Config::get('laravel-tcpdf');

   $pdf = new \Elibyy\TCPDF\TCPdf($pdf_settings['page_orientation'], $pdf_settings['page_units'], $pdf_settings['page_format'], true, 'UTF-8', false);

   $pdf->SetPrintHeader(false);
        $pdf->SetPrintFooter(false);
        $pdf->AddPage();
        $pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
        $filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
        $pdf->output($filename, 'I');
        return redirect('forms');
    
}

【讨论】:

  • 您好,我在下载时遇到了 tcpdf 文件名的问题,例如 'Héllo oumaya.pdf' 我有 'Hllo_oumaya.pdf' 第一个特殊字符为空,第二个空格被替换为 ''经过 '_' ???请问有什么帮助吗? :'( :'(
猜你喜欢
  • 2015-08-09
  • 2018-05-21
  • 1970-01-01
  • 2016-12-30
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 2017-05-23
相关资源
最近更新 更多