2

i'm trying to export a twig view into csv, but i'm stuck, can someone help me out with detailled solution ? peace

/**
 *  
 * @Route("/export", name="export_csv")
 */
public function exportAction() {

    $entity = new Invite();
    $form = $this->createForm(new ManifSearchType(), $entity);
    $request = $this->get('request');

    $em = $this->getDoctrine()->getManager();

    $view = $this->render('PrifProtocoleBundle:Invite:index.html.twig', array(
        'form' => $form->createView()));

    $handle = fopen('php://memory', 'r+');
    $header = array();

    fputcsv($handle, $view);

    rewind($handle);

    $content = stream_get_contents($handle);
    fclose($handle);

    return new Response($content, 200, array(
        'Content-Type' => 'application/force-download',
        'Content-Disposition' => 'attachment; filename="export.csv"'
    ));
} 

error message:

Warning: fputcsv() expects parameter 2 to be array, object given in C:\wamp\www\protocole\src\Prif\ProtocoleBundle\Controller\InviteController.php line 56 
2
  • is your index.html.twig actually html formatted? If it is csv formatted, there is a better way to output a csv file. Commented Dec 18, 2013 at 18:01
  • hi, thanks for the reply. i didn't explain my problem well, what i want actually is to export the html form that is displayed through index.html.twig into a csv file. Commented Dec 18, 2013 at 23:49

3 Answers 3

6

The thing is, you are not trying to convert just a twig to a CSV file. You are converting HTML to a CSV file. Now that seems like a strange conversion.

What you should do is have your twig generate the CSV content. Like this:

$response = $this->render('PrifProtocoleBundle:Invite:export.csv.twig',array('data' => $data));
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
return $response;

And your twig should render in the CSV format.

Sign up to request clarification or add additional context in comments.

6 Comments

hi, thanks for the reply. i didn't explain my problem well, what i want actually is to export the html form that is displayed through index.html.twig into a csv file, is it feasable?
In the code I gave you, the $data you pass would be $form->getData(). You should not render an HTML to convert to a CSV, that doesn't make sense. You have your data already. Create a CSV from there. When someone submits the form, you handle the request, check to see if the form is valid, then you render the CSV with the form's data. So you have an HTML twig to display the form, so the user enters the data. And once the submitted data is valid, you have a CSV twig to render the CSV that you want to export. You should have 2 twig files. Doesn't that make sense?
hi, thanks for this precision, i understand now. it works perfectly
hi, i have another question, i created a form with a select box containing 10 values. After $form->getData, i want to return first a response in a html table (result.html.twig) and then being able to export the csv file (export.csv.twig) by a link in this html file. In my controller i did something like: $response1 that renders the html file, and $response2 that renders the csv file. but i only return $response1, so i'm stuck. do you think it's the right way to do this, or do you have a better solution? thanks
You should return your HTML response first, with the data you get, and with the link to export to CSV. In the link to generate the CSV, you could add the parameters (your 10 fields) to generate the CSV file. That would be a long link. If that is NOT acceptable for you, then you should have a way to cache the data (with APC for example). So when you render the HTML table with your 10 fields, you cache the data as well. And when you click on "export CSV", in your action, you fetch the data in the cache, and render the CSV from there. That's what I would do.
|
1

Try this:

fputcsv($handle, (array)$view);

2 Comments

hi, your solution works. it opens a file export.csv on excel. that's good. but the html form that i want is not in the file. Actually my index.html.twig renders a form at localhost/protocole/web/app_dev.php/protocole/invite/export and i want this html form to be in this csv file. do you know how i can change my code to do this? thanks
If you need to get plain HTML content, try renderView instead of render.
0

Old question but I came here when I searching a way to save rendered csv view in a file. With Symfony 3.3 (current version today 05/09/2017) a new filesystem method was added:

$fs->appendToFile('my.csv', $view);

Complete example could be:

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

$fs = new Filesystem();

try {
    $fs->appendToFile('my.csv', $view);
} catch (IOExceptionInterface $e) {
    echo "An error occurred while adding contents ".$e->getPath();
}

I have not tested yet, but it should works.

Source doc : https://symfony.com/doc/current/components/filesystem.html#appendtofile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.