Reputation: 468
I m trying to implement Knp SNappy Bundle to display an html page as a pdf from my controller. I'm stuck with this error :
The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
[==========> ] 18%
Warning: Failed to load file:///slick/slick.css (ignore)
Warning: Failed to load file:///slick/slick-theme.css (ignore)
Warning: Failed to load file:///clovis-app/style.css (ignore)
Warning: Failed to load file:///clovis-app/estimate.css (ignore)
Warning: Failed to load file:///clovis-app/res/clovis.png (ignore)
Warning: Failed to load file:///js/collection.js (ignore)
Warning: Failed to load file:///clovis-app/script.js (ignore)
Warning: Failed to load file:///clovis-app/estimate.js (ignore)
[============================================================] 100%
Counting pages (2/6)
[============================================================] Object 1 of 1
Resolving links (4/6)
[============================================================] Object 1 of 1
Loading headers and footers (5/6)
Printing pages (6/6)
[> ] Preparing
[===================> ] Page 1 of 3
[=======================================> ] Page 2 of 3
[============================================================] Page 3 of 3
Done
Exit with code 1 due to network error: ContentOperationNotPermittedError"
stdout: ""
command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" --lowquality "C:\Users\Razgort\AppData\Local\Temp\knp_snappy55ae6cc4ad0353.17775155.html" "C:\Users\Razgort\AppData\Local\Temp\knp_snappy55ae6cc4ad41d0.64896673.pdf".
Here is my configuration file :
knp_snappy:
pdf:
enabled: true
binary: "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""
options: []
image:
enabled: true
binary: "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\""
options: []
and here is how i render the view :
$html = $this->renderView('CompanyBundle:Estimate:edit.html.twig', array(
'estimate' => $estimate,
'estimateForm' => $estimateForm->createView(),
'articleForm' => $articleForm->createView(),
'articleSelectForm' => $articleSelectForm->createView(),
'manpowerForm' => $manpowerForm->createView(),
'manpowerSelectForm' => $manpowerSelectForm->createView(),
'workSelectForm' => $workSelectForm->createView(),
'workForm' => $workForm->createView(),
'bundleForm' => $bundleForm->createView(),
'bundleSelectForm' => $bundleSelectForm->createView(),
));
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
I eard there is problems on MAMP, i use wamp, didn't see any issue with it. I'll really appreciate some help on this =)
Edit :
I modified some things in order to generate a pdf first. And i did. But the pdf only contents my login page. Then i added the access to my session and it worked. But when i try to send it as a response like that :
$session = $this->get('session');
$session->save();
session_write_close();
$pageUrl = $this->generateUrl('estimate_preview', array('id' => $id), true);
return new Response(
$this->get('knp_snappy.pdf')->getOutput($pageUrl, array('cookie' => array($session->getName() => $session->getId()))),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
it doesn't seem to work either... Does someone have an idea why ?
Upvotes: 0
Views: 2268
Reputation: 31
in Symfony version 4.4 getOutput()
works fine ...
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Knp\Snappy\Pdf;
private $session;
private $pdf;
public function __construct(SessionInterface $session, Pdf $pdf)
{
$this->session = $session;
$this->pdf = $pdf;
}
/**
* @Route("/estimate/preview/pdf/{id}", name="estimate_preview_pdf")
*/
public function estimate_preview_pdf($id): Response
{
$this->session->save();
$pageUrl = $this->generateUrl('estimate_preview_html', array('id' => $id), false);
return new Response(
$this->pdf->getOutput($pageUrl,array('cookie' => array($this->session->getName() => $this->session->getId()))),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
}
Code $this->session->save()
is important... without it it gets stuck
Upvotes: 0
Reputation: 4215
Knp SNappy Bundle is a wrapper. In your symphony side you do all right.
But in your HTML-to-render you have relative URLs, that is not absolute to HTTP-server. So when wkhtmltopdf see in your HTML url like '/dsasdaasd/sdaasd.wtf', it does not know, that it's your site.
1st option. Render PDF by URL (pass some option like "use print styles"). 2nd: Use in your HTML absolute pathes to assets.
Anyway there is no absolute correct HTML, so you can meet in the future more errors like this -- even if in huge HTML there will be only one not-found-image. So try to find some options in snappy to ignore these errors as in the result pdf will be generated ok.
Upvotes: 0
Reputation: 1508
Try by using forward (getContent()) method instead render.
$html = $this->forward('CompanyBundle:Estimate:edit.html.twig', array(
'estimate' => $estimate,
'estimateForm' => $estimateForm->createView(),
'articleForm' => $articleForm->createView(),
'articleSelectForm' => $articleSelectForm->createView(),
'manpowerForm' => $manpowerForm->createView(),
'manpowerSelectForm' => $manpowerSelectForm->createView(),
'workSelectForm' => $workSelectForm->createView(),
'workForm' => $workForm->createView(),
'bundleForm' => $bundleForm->createView(),
'bundleSelectForm' => $bundleSelectForm->createView(),
))->getContent();
$pdf =$this->get('knp_snappy.pdf')->getOutputFromHtml($html);
First check by printing $pdf and then you can return it as a response.
Upvotes: 1
Reputation: 851
Do this for all your CSS files in your Twig:
<link rel="stylesheet" href="{{ app.request.scheme ~'://'~ app.request.httpHost ~ asset('css/my_custom_stylesheet.css') }}" />
The CSS folder must be directly under /web
Upvotes: 0