Reputation: 1209
Is there an option to overwrite the existing PDF file?
I have this function:
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();
}
The problem is that when i run that function for the second time i get a file already exist error.
Upvotes: 3
Views: 5528
Reputation: 9782
My solution is to delete the file to be overwritten.
Versions
knplabs/knp-snappy v1.3.1 knplabs/knp-snappy-bundle v1.8.0
PDF creation logic (running under Ibexa community edition/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
);
}
Upvotes: 0
Reputation: 1716
I faced the same issue today in laravel.
Exit with code 1 due to network error: ContentNotFoundError
I tried to wrap my all css and script link with url() function.
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>
and my code working. Now every thing is fine. :)
Upvotes: 0
Reputation: 774
What i did was to add the overwrite function to save(), like this:
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;
}
I have made a pull request for this: https://github.com/barryvdh/laravel-snappy/pull/76 , but this uses $overwrite = false as default
Upvotes: 3
Reputation: 2243
If assume you are using barryvdh/laravel-snappy with laravel 5.1
The function save()
you are using to save doesn't support overwrite flag, it internally calls generateFromHtml
that does support overwrite flag, the fourth argument.
see the save method in \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;
}
I would recommend you to use the following method of saving the view to 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();
}
Hope this helps,
K
Upvotes: 0