Laravel 5.1 Snappy PDF覆盖

时间:2015-10-03 17:06:48

标签: php pdf laravel-5.1

是否有覆盖现有PDF文件的选项?

我有这个功能:

public function quote($id){

    $quote          = Quotes::find($id);
    $last_quote     = sprintf("%'.04d\n", $quote->quote_number);
    $customer       = Customer::find($quote->customer_id);
    $items          = $quote->Items()->get();
    $fullinfo       = json_decode($quote->fullinfo);
    $token          = $quote->Tokens()->first();

    $data = [
        'quote'         => $quote,
        'last_quote'    => $last_quote,
        'customer'      => $customer,
        'items'         => $items,
        'fullinfo'      => $fullinfo,
        'token'         => $token,
    ];

    $pdf = App::make('snappy.pdf.wrapper');

    $pdf->LoadView('quotes.quote_print', $data)
        ->setOption('page-size', 'A4');
    $path = storage_path() . "/quotes/{$customer->name}/cotizacion.pdf";
    $pdf->save($path);
    return $pdf->stream();


}

问题是,当我第二次运行该功能时,我得到一个文件已存在错误。

3 个答案:

答案 0 :(得分:0)

如果假设您使用 laravel 5.1 barryvdh / laravel-snappy

您用来保存的函数save()不支持覆盖标志,它在内部调用generateFromHtml,它支持覆盖标志,第四个参数。

请参阅\vendor\barryvdh\laravel-snappy\src\PdfWrapper.php

中的保存方法
public function save($filename)
{

    if ($this->html)
    {
        $this->snappy->generateFromHtml($this->html, $filename, $this->options);
    }
    elseif ($this->file)
    {
        $this->snappy->generate($this->file, $filename, $this->options);
    }

    return $this;
}

我建议您使用以下方法将视图保存为pdf

public function quote($id){

    $quote          = Quotes::find($id);
    $last_quote     = sprintf("%'.04d\n", $quote->quote_number);
    $customer       = Customer::find($quote->customer_id);
    $items          = $quote->Items()->get();
    $fullinfo       = json_decode($quote->fullinfo);
    $token          = $quote->Tokens()->first();

    $data = [
        'quote'         => $quote,
        'last_quote'    => $last_quote,
        'customer'      => $customer,
        'items'         => $items,
        'fullinfo'      => $fullinfo,
        'token'         => $token,
    ];

    $pdf = App::make('snappy.pdf.wrapper');

    $html = view('quotes.quote_print', $data))->render();
    $path = storage_path() . "/quotes/{$customer->name}/cotizacion.pdf";

    //public function generateFromHtml($html, $output, array $options = array(), $overwrite = false)
    $pdf->generateFromHtml($html, $path,[],$overwrite = true);
    return $pdf->stream();


}

希望这有帮助,

ķ

答案 1 :(得分:0)

我所做的是将覆盖函数添加到save(),如下所示:

ng-app="CMS"

我已经发出了拉取请求:https://github.com/barryvdh/laravel-snappy/pull/76,但这使用$ overwrite = false作为默认值

答案 2 :(得分:0)

我今天在laravel面对同样的问题。

由于网络错误,

退出代码1:ContentNotFoundError

我尝试用url()函数包装我的所有css和脚本链接。

Example:
Replace
        <link href="/css/bootstrap.min.css" rel="stylesheet">
to         
    <link href="{{url('/css/bootstrap.min.css')}}" rel="stylesheet">
And Replace
    <script src="{{url('/js/jquery-2.1.1.js')}}"></script>
To
    <script src="{{url('/js/jquery-2.1.1.js')}}"></script>

我的代码正常工作。现在一切都很好。 :)