I have to do CSV file export, and that file has to have ISO-8859-2 character encoding, and display properly foreign characters.
The controller looks like this:
public function exportAction(Request $request) {
$repository = $this->getDoctrine()
->getManager()
->getRepository('AdminBundle:ShopPayroll');
$request = $this->get('request');
$response = $this->render('AdminBundle:Payroll:csv.html.twig', [
'list' => $repository->getSomeData()
]);
$handle = fopen('php://memory', 'r+');
$header = array();
fputcsv($handle, (array)utf8_decode($response));
rewind($handle);
$content = stream_get_contents($handle);
fclose($handle);
$response->setCharset('ISO-8859-2');
$response->headers->set('Content-Type', 'text/csv; charset=ISO-8859-2');
$response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
$response->prepare($request);
return $response->send();
}
And the csv.html.twig file itself (the file is encoded as ISO-8859-2):
{% for payroll in list %}
{{ payroll.fvatName|slice(0,32)|lower|title|raw|convert_encoding('UTF-8', 'ISO-8859-2') }}
{% endfor %}
Ok, it does downloads the file in ISO-8859-2 encoding, BUT if the string variable contains foreign characters, it changes those characters into some weird symbols.
I tried using iconv inside fputcsv() function, I tried using it as a twig built-in function - none works.
How can I solve this problem?