Laravel 5.1 活泼的 PDF 覆盖
Laravel 5.1 Snappy PDF overwrite
是否有覆盖现有 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();
}
问题是,当我 运行 第二次运行该功能时,我得到一个文件已经存在的错误。
如果假设您使用 barryvdh/laravel-snappy 和 laravel 5.1
您用于保存的函数 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();
}
希望这对您有所帮助,
K
我所做的是将覆盖函数添加到 save() 中,如下所示:
public function save($filename, $overwrite = true)
{
if ($this->html)
{
$this->snappy->generateFromHtml($this->html, $filename, $this->options, $overwrite);
}
elseif ($this->file)
{
$this->snappy->generate($this->file, $filename, $this->options, $overwrite);
}
return $this;
}
我已经为此提出了拉取请求:https://github.com/barryvdh/laravel-snappy/pull/76,但这使用 $overwrite = false 作为默认值
我今天在 laravel 遇到了同样的问题。
由于网络错误退出并返回代码 1:ContentNotFoundError
我试图用 url() 函数包装我所有的 css 和脚本 link。
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>
并且我的代码可以正常工作。现在一切都很好。 :)
是否有覆盖现有 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();
}
问题是,当我 运行 第二次运行该功能时,我得到一个文件已经存在的错误。
如果假设您使用 barryvdh/laravel-snappy 和 laravel 5.1
您用于保存的函数 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();
}
希望这对您有所帮助,
K
我所做的是将覆盖函数添加到 save() 中,如下所示:
public function save($filename, $overwrite = true)
{
if ($this->html)
{
$this->snappy->generateFromHtml($this->html, $filename, $this->options, $overwrite);
}
elseif ($this->file)
{
$this->snappy->generate($this->file, $filename, $this->options, $overwrite);
}
return $this;
}
我已经为此提出了拉取请求:https://github.com/barryvdh/laravel-snappy/pull/76,但这使用 $overwrite = false 作为默认值
我今天在 laravel 遇到了同样的问题。
由于网络错误退出并返回代码 1:ContentNotFoundError
我试图用 url() 函数包装我所有的 css 和脚本 link。
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>
并且我的代码可以正常工作。现在一切都很好。 :)