PHP code example of vegas-cmf / exporter

1. Go to this page and download the library: Download vegas-cmf/exporter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vegas-cmf / exporter example snippets


use Phalcon\DiInterface;
use Vegas\DI\ServiceProviderInterface;

/**
 * Class ExporterServiceProvider
 */
class ExporterServiceProvider implements ServiceProviderInterface
{
    const SERVICE_NAME = 'exporter';

    /**
     * {@inheritdoc}
     */
    public function register(DiInterface $di)
    {
        $di->set(self::SERVICE_NAME, function() use ($di) {
            $exporter = new \Vegas\Exporter\Exporter;
            return $exporter->setDI($di);
        }, true);
    }

    public function getDependencies()
    {
        return [];
    }
}

$exportData = [
    ["John", "Malkovic", "52"],
    ["Kenny", "Smith", "36"],
    ["Sam", "Stevenson", "18"],
];

$columns = ["Firstname", "Lastname", "age"];

$extraSettings = [  // CSV-only settings, default values below
    'separator'     => ',',
    'lineSeparator' => PHP_EOL,
    'skipHeaders'   => false,   // skip printing headers in first row?
    'quoteFields'   => false    // enclose output fields in ""
];

$config = (new \Vegas\Exporter\ExportSettings)
            ->setFilename('my_export_file')
            ->setOutputDir('/tmp')
            ->setHeaders($columns)
            ->setHeaderKeysAsParams(true)
            ->setData($exportData)
            ->setExtraSettings($extraSettings);
            
/** @var \Phalcon\DiInterface $di */
$exporter = $di->get('exporter');

$exporter->setConfig($config);
$exporter->saveCsv();

$exportData = [
    ["John", "Malkovic", "52"],
    ["Kenny", "Smith", "36"],
    ["Sam", "Stevenson", "18"],
];

$columns = ["Firstname", "Lastname", "age"];

$extraSettings = [  // PDF-only settings, default values below
    'pageOrientation'       => 'Portrait',
    'pageSize'              => 'A4',
    'fontSize'              => 0,
    'fontFamily'            => ''
];

$config = (new \Vegas\Exporter\ExportSettings)
            ->setTemplate('template_name')
            ->setTitle('My first PDF export')
            ->setFilename('my_export_file')
            ->setHeaders($columns)
            ->setData($exportData)
            ->setExtraSettings($extraSettings);
            
/** @var \Phalcon\DiInterface $di */
$exporter = $di->get('exporter');

$di->get('view')->disable();    // prevent default view rendering
$exporter->setConfig($config);
$exporter->downloadPdf();

$john = new \stdClass;
$john->Firstname = 'John';
$john->Lastname = 'Malkovic';
$john->age = 52;

$exportData = [
    $john,
    // ...
];

$columns = ["Firstname", "Lastname", "age"];

$extraSettings = [  // XML-only settings, default values below
    'rootName'  => 'root',  // document tree tag name
    'nodeName'  => 'item'   // each node tag name
];

$config = (new \Vegas\Exporter\ExportSettings)
            ->setHeaders($columns)
            ->setHeaderKeysAsParams(false)
            ->setData($exportData)
            ->setExtraSettings($extraSettings);
            
/** @var \Phalcon\DiInterface $di */
$exporter = $di->get('exporter');
            
$exporter->setConfig($config);
$result = $exporter->printXml();