kratos
kratos

Reputation: 2495

CSV Export showing HTML along with data. Why?

Here is my code and I am using https://github.com/sonata-project/exporter

$data = array(
                    0 => array(
                        'name' => 'Jack'
                    ),
                    1 => array(
                        'name' => 'Jill'
                    )
                );

    $format = 'csv';

                    // Filename
                    $filename = 'referral.csv';

                    // Set Content-Type
                    $content_type = 'text/csv';

                    // Location to Export this to
                    $export_to = 'php://output';

                    // Data to export
                    $exporter_source = new \Exporter\Source\ArraySourceIterator($data);

                    // Get an Instance of the Writer
                    $exporter_writer = '\Exporter\Writer\\' . ucfirst($format) . 'Writer';

                    $exporter_writer = new $exporter_writer($export_to);

                    // Set the right headers
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Content-Description: File Transfer');
                    header('Content-type: ' . $content_type);
                    header('Content-Disposition: attachment; filename=' . $filename . ';');
                    header('Expires: 0');
                    header('Pragma: public');

                    // Export to the format
                    \Exporter\Handler::create($exporter_source, $exporter_writer)->export();

When I execute the following code, it downloads a CSV file that has the array data BUT also has HTML for the page. How do I make it so that the export only has the elements of the data array?

Upvotes: 1

Views: 1116

Answers (1)

1ed
1ed

Reputation: 3668

This code is from SonataCoreBundle and it streams the respose to the client so it's good for memory consumption:

use Exporter\Source\SourceIteratorInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;

use Exporter\Writer\XlsWriter;
use Exporter\Writer\XmlWriter;
use Exporter\Writer\JsonWriter;
use Exporter\Writer\CsvWriter;

// ...

public function exportAction()
{
    $data = array(array('name' => 'Jack'), array('name' => 'Jill'));
    $source = new \Exporter\Source\ArraySourceIterator($data);
    $filename = 'file.csv'
    $format = 'csv';

    switch ($format) {
        case 'xls':
            $writer      = new XlsWriter('php://output');
            $contentType = 'application/vnd.ms-excel';
            break;
        case 'xml':
            $writer      = new XmlWriter('php://output');
            $contentType = 'text/xml';
            break;
        case 'json':
            $writer      = new JsonWriter('php://output');
            $contentType = 'application/json';
            break;
        case 'csv':
            $writer      = new CsvWriter('php://output', ',', '"', "", true, true);
            $contentType = 'text/csv';
            break;
        default:
            throw new \RuntimeException('Invalid format');
    }

    $callback = function() use ($source, $writer) {
        $handler = \Exporter\Handler::create($source, $writer);
        $handler->export();
    };

    return new StreamedResponse($callback, 200, array(
        'Content-Type'        => $contentType,
        'Content-Disposition' => sprintf('attachment; filename=%s', $filename)
    ));
}

Upvotes: 2

Related Questions