PHP code example of santwer / exporter

1. Go to this page and download the library: Download santwer/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/ */

    

santwer / exporter example snippets


use WordExporter\Facades\WordExporter;

// Download as a Word file
WordExporter::download(new MyExportClass(), 'final-word.docx');

// Store the exported file
WordExporter::store(new MyExportClass(), 'path/to/save/export.docx');

// Store the exported file with an certain filename
WordExporter::storeAs(new MyExportClass(), 'path/to/save/', 'export.docx');

// Store the exported file with an certain filename as a batch
WordExporter::batchStore(
    new Exportable(new MyExportClass(), 'path/to/save/', 'export.docx'),
    new Exportable(new MyExportClass1(), 'path/to/save/', 'export1.docx'),
    new Exportable(new MyExportClass2(), 'path/to/save/', 'export2.pdf'),
    );

// Queue it for later processing
WordExporter::queue(new MyExportClass(), 'path/to/save/export.docx');

namespace App\Http\Controllers;

use App\Http\Export\FirstExport;
use Santwer\Exporter\Facade\WordExport;

class HomeController extends Controller
{
    public function index()
    {
        return WordExport::download(new FirstExport(), 'myExport.docx');
    }
}

namespace App\Http\Export;

use Santwer\Exporter\Concerns\FromWordTemplate;
use Santwer\Exporter\Concerns\GlobalTokens;
use Santwer\Exporter\Concerns\TokensFromCollection;
use Illuminate\Support\Collection;

class FirstExport implements FromWordTemplate, TokensFromCollection, GlobalTokens
{
	public function items(): Collection
	{
		return collect([
			[
				'name' => 'Jane Smith',
				'email' => '[email protected]',
				'deliveryAddress' => [
					'street' => 'Main Street',
					'city' => 'Metropolis',
					'postcode' => '543210',
				],
			],
			[
				'name' => 'Alice Johnson',
				'email' => '[email protected]',
				'deliveryAddress' => [
					'street' => 'Elm Street',
					'city' => 'Springfield',
					'postcode' => '987654',
				],
			],
			[
				'name' => 'Bob Williams',
				'email' => '[email protected]',
				'deliveryAddress' => [
					'street' => 'Oak Avenue',
					'city' => 'Townsville',
					'postcode' => '135792',
				],
			],
		]);
	}

	public function blockName():string
	{
		return 'customer';
	}

	public function values(): array
	{
		return [
			'TownDateFormat' => 'Townsville, '. now()->format('Y-m-d'),
		];
	}

	public function itemTokens($item) : array
	{
		return $item;
	}

	public function wordTemplateFile(): string
	{
		return 'uploads/myDocFile.docx';
	}
}

namespace App\Http\Export;
use Santwer\Exporter\Concerns\FromWordTemplate;
use Santwer\Exporter\Concerns\GlobalTokens;
use Santwer\Exporter\Concerns\WithCharts;
use Santwer\Exporter\Concerns\TokensFromCollection;
use Illuminate\Support\Collection;

class FirstExport implements FromWordTemplate, TokensFromCollection, GlobalTokens, WithCharts
{
    public function charts(): array
	{

		return [
			'radar' => function () {
				$categories = array('A', 'B', 'C', 'D', 'E');
				$series1 = [1, 3, 2, 5, 4];

				$chart = new Chart('radar', $categories, $series1,
					[
						'width' => 1000000*5,
						'height' => 1000000*5,
						'showLegend' => true

					],'Series 1');
				$chart->addSeries($categories, [3, 4, 5, 1, 2], 'Series 2');
				return $chart;
			},
		];
	}
	
	public function items(): Collection
	{
		return collect([
            
		]);
	}

	...
}


namespace App\Http\Export;

use Santwer\Exporter\Concerns\FromWordTemplate;
use Santwer\Exporter\Concerns\GlobalTokens;
use Santwer\Exporter\Concerns\WithImages;
use Santwer\Exporter\Concerns\TokensFromCollection;
use Illuminate\Support\Collection;

class FirstExport implements FromWordTemplate, TokensFromCollection, GlobalTokens, WithImages
{
    public function images(): array
    {
        return [
            'CompanyLogo' => public_path('images/logo.jpg'),
            'UserLogo' => 'path' => public_path('images/logo.jpg'), 'width' => 100, 'height' => 100, 'ratio' => false,
            'image1' => function () {
                return [
                    'path' => public_path('images/image1.jpg'),
                    'width' => 100,
                    'height' => 100,
                    'ratio' => false,
                ];
            },
        ];
    }
    
    public function items(): Collection
    {
        return collect([
            
        ]);
    }

    ...
}


namespace App\Http\Export;

use Santwer\Exporter\Concerns\FromWordTemplate;
use Santwer\Exporter\Concerns\GlobalTokens;
use Santwer\Exporter\Concerns\WithTables;
use Santwer\Exporter\Concerns\TokensFromCollection;
use Illuminate\Support\Collection;

class FirstExport implements FromWordTemplate, TokensFromCollection, GlobalTokens, WithTables
{
    public function tables(): array
    {
        return [
            'table1' => function () {
                return [
                    'headers' => [['width' => 3000, 'text' => 'Name'], 'Email', 'Address'],
                    'rows' => [
                        ['Jane Smith', '[email protected]', 'Main Street'],
                        ['Alice Johnson', '[email protected]', 'Elm Street'],
                        ['Bob Williams', '[email protected]', 'Oak Avenue'],
                    ],
                    'style' => [
                        'borderSize' => 6,
                        'borderColor' => 'green',
                        'width' => 6000,
                    ],
                ];
            },
        ];
    }
    
    public function items(): Collection
    {
        return collect([
            
        ]);
    }

    ...
}
    

use Santwer\Exporter\Exportable;

class User {
    use Exportable;
    ...
}

use Santwer\Exporter\Exportable;
use Santwer\Exporter\Concerns\HasTokens;
...

class User implements HasTokens {
    use Exportable;
    ...
    public function exportTokens(): array
    {
        return [
            'name'  => $this->name,
            'email' => $this->email,
        ];
    }
}

use Santwer\Exporter\Exportable;
use Santwer\Exporter\Concerns\HasTemplate;
...

class User implements HasTemplate {
    use Exportable;
    ...
    public function exportTemplate(): string
    {
        return '/template.docx';
    }
}

use Santwer\Exporter\Exportable;
...

class User implements HasTemplate {
    use Exportable;
    ...
    protected $exportBlock = 'customer';
}

return User::where(...)
        ->template('templatefile.docx')
        ->export();

return User::where(...)
        ->export();

return User::where(...)
        ->template('templatefile.docx')
        ->store('storage/path');

return User::where(...)
        ->template('templatefile.docx')
        ->storeAs('storage/path', 'name.docx');

return User::where(...)
        ->first()
        ->export('filename.docx', ['template' =>' templatefile.docx']);

return User::find(1234)
        ->export('filename.docx', ['template' =>' templatefile.docx']);

return User::where(...)
        ->template('templatefile.docx')
        ->export(['format' => 'pdf']);

return User::where(...)
        ->exportPdf();

return User::where(...)
        ->exportFirstPdf();

return [
    'removeRelations' => false,
]

return User::with('posts')
        ->with('posts.comments')
        ->with('posts.comments.user')
        ->template('templatefile.docx')
        ->export();

return User::template('templatefile.docx')->export();

use Santwer\Exporter\Processor\GlobalVariables;
...
       GlobalVariables::setVariable('Date', now()->format('d.m.Y'));

use Santwer\Exporter\Processor\GlobalVariables;
...
      GlobalVariables::setVariables([
          'Time' =>  now()->format('H:i'),
          'Date' =>  now()->format('Y-m-d'),
      ]);

use Santwer\Exporter\Exportable;
...

class User implements HasTemplate {
    use Exportable;
    ...
    protected $exportBlock = 'customer';
}

use Santwer\Exporter\Exportable;
...

class User implements HasTemplate {
    use Exportable;
    ...
    protected $exportBlock = 'customer';
    
    public function deliveryAddress()
    {
        return $this->hasOne(Address::class);
    }

}

use Santwer\Exporter\Exportable;
...

class User implements HasTemplate {
    use Exportable;
    ...
    protected $exportBlock = 'customer';
    
    public function orders()
    {
        return $this->hasOne(Order::class);
    }
    
    public function deliveryAddress()
    {
        return $this->hasOne(Address::class);
    }

}
sh
    php artisan make:word {className}