'Symfony - how to create a pdf with some data selected by checkbox
I have a page with a list of products and the detail. In the detail I have a button to generate a PDF file (works with KnpSnappy Bundle) of the product.
Now I want on the list page, to select the products with checkbox and create one pdf with all products selected but I don't really know how can I do that
I started with the integration of checkbox with the id of the product as the value and I made a button to send theses values to my controller with a new route
In my controller, I don't see how can I configure my new route and what could be my next step to generate the pdf with all of theses data
there is some code :
list view :
{{ form(form) }}
{% for annonce in annonces %}
{{ annonce._source.titre }}
{{ annonce._source.reference }}
<input type="checkbox" name="checkbox[]" value="{{annonce._id}}" multiple/>
{% endfor %}
<input type="submit" value="export" name="test" href="{{ path('annonce_pdf_multiple') }}">
{{ form_end(form) }}
Controller :
/**
* @Route("/", name="annonce_search", methods="GET|POST")
* @Route("/", name="annonce_pdf_multiple", methods="POST") (it's my new route, should I add ids ?)
*/
public function search(Request $request)
{
$select = $request->get('checkbox');
}
if it can help there is the part code of how I generate one PDF to the show page :
show :
{{ annonce.ville.nom }}
{{ annonce.ville.codePostal }}
{{ SomeOtherData }}
//link to generate the pdf
<a href="{{ path('annonce_pdf', {'id': annonce.id}) }}">
controller :
/**
* @Route("/annonce/{id}/pdf", name="annonce_pdf", methods="GET")
* @Route("/annonce/{id}/pdf/contact", name="annonce_pdf_contact", methods="GET", defaults={"contact": true})
*/
public function pdf(Security $security, Annonce $annonce, bool $contact = false): Response
{
$filePath = $this->pdfService->pdfAnnonce($annonce, $contact);
$response = new BinaryFileResponse($filePath);
// $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;
}
}
The function pdfAnnonce() :
public function pdfAnnonce(Annonce $annonce, bool $contact = false) {
$filepath = $this->root.$this->directory.md5($annonce->getId()).'.pdf';
$this->pdf->generateFromHtml(
$this->templating->render('@themes/'.$annonce->getWebsite()->toElasticaIndex().'/front/annonce/show.pdf.twig', [
'annonce' => $annonce,
'root' => $this->root,
'contact' => $contact
]),
$filepath,
$options,
true
);
return $filepath;
}
thanks you in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
