1

I am working on a Symfony 2 app that should create a CSV File for download:

sep=;
{% for header in headers %}{{ header|trans({}, 'app') }};{% endfor %}

{% for row in data %}
    {% for column in row %}{{ column }};{% endfor %}
{% endfor %}

And the Controller:

...
$response = new Response();
$response->headers->set('Content-Type', 'text/csv; charset=UTF-16LE');
$response->headers->set('Content-Disposition', 'attachment;filename="PaymentList.csv"');

$variables = array(
    'headers' => $headers,
    'data' => $data,
);

// Use UTF-16LE with BOM to be compatible with Excel on Win and Mac
$content = $this->renderView('AppBundle:Shop:export.csv.twig', $variables);
$content = chr(255) . chr(254) . mb_convert_encoding($content, 'UTF-16LE', 'UTF-8');
$response->setContent($content);

return $response;

This works very well and the CSV file can be used in Excel both on Windows and Mac OS X. However, the file should also be used in a quite old accounting tool that expects \r\n line breaks, while the Twig produces \n line breaks.

Is there any way to tell Twig to use \r\n instead?

Of course I could simply search/replace the line breaks in PHP after rendering the view. Another solution would be, to create the CSV Data completely in PHP without involving Twig at all.

Both solution would work, but I form my point of view that would not be very clean. I would like to control the output, that is created by Twig. Is this possible?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
  • 2
    saving your template with "line ending" -> windows instead of unix in your editor doesn't work? – Federkun Apr 05 '16 at 10:56
  • You need use library for generate csv file. No tamplate. For example look this question http://stackoverflow.com/questions/4249432/export-to-csv-via-php – Naumov Apr 05 '16 at 11:04

2 Answers2

3

Twig normalises line endings to \n, see the Github issue here: https://github.com/twigphp/Twig/issues/1481

As discussed there, if you want to use Twig to generate this output, you'll need to extend the default lexer, or manually replace the endings in the output.

iainn
  • 16,826
  • 9
  • 33
  • 40
1

Line-ending is template-line-ending dependent. But if templates are "stored" in VCS, it may be converted - without your direct control.

On: https://www.designopsy.com/symfony2-export-csv/ I've found something like this:

  $response = new StreamedResponse();
  $response->setCallback(function(){

      $handle = fopen('php://output', 'w+');

      // Add the header of the CSV file
      fputcsv($handle, array('Name', 'Surname', 'Age', 'Sex'),';');
      // Query data from database
      $results = $this->connection->query( "Replace this with your query" );
      // Add the data queried from database
      while( $row = $results->fetch() )
      {
          fputcsv(
              $handle, // The file pointer
              array($row['name'], $row['surname'], $row['age'], $row['sex']), // The fields
          ';' // The delimiter
           );
      }

      fclose($handle);
  });
eRIZ
  • 1,477
  • 16
  • 32