是否有覆盖现有 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();
}
问题是,当我第二次运行该函数时,我收到文件已存在错误。
我所做的就是在 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 作为默认值
如果假设您正在使用 barryvdh/laravel-snappy 和 laravel 5.1
您用来保存的函数
save()
不支持覆盖标志,它内部调用支持覆盖标志的generateFromHtml
,即第四个参数。
参见\vendor\barryvdh\laravel-snappy\src\PdfWrapper.php
中的save方法
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
我今天在 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>
我的代码可以工作。现在一切都很好。 :)
我的解决办法是删除要覆盖的文件。
版本
knplabs/knp-snappy v1.3.1 knplabs/knp-snappy-bundle v1.8.0
PDF 创建逻辑(在 Ibexa 社区版/Symfony 下运行)
private function _makePdf(ContentView $view) {
$pdfFilename = $this->baseDir . $this->pdfUrl . '.pdf';
unlink($pdfFilename);
$html = $this->render($view->getTemplateIdentifier(), $view->getParameters());
$this->knpSnappyPdf->generateFromHtml(
$html->getContent(),
$pdfFilename
);
}