PHP code example of shoaib3375 / php-doc-exporter

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

    

shoaib3375 / php-doc-exporter example snippets


use Shoaib3375\PhpDocExporter\DocumentExporter;

$data = [
    ['name' => 'Shoaib', 'age' => 25, 'city' => 'Dhaka'],
    ['name' => 'মাইনুল', 'age' => 30, 'city' => 'Sylhet']
];

$exporter = new DocumentExporter();
$content = $exporter->export('pdf', $data);

// Save to file
file_put_contents('report.pdf', $content);

use Shoaib3375\PhpDocExporter\DocumentExporter;
use Illuminate\Http\Request;

public function export(Request $request)
{
    $data = [
        ['name' => 'Shoaib', 'age' => 25, 'city' => 'Dhaka'],
        ['name' => 'মাইনুল', 'age' => 30, 'city' => 'Sylhet']
    ];

    $exporter = new DocumentExporter();
    $format = $request->input('format', 'pdf'); // pdf, excel, word, csv
    
    $content = $exporter->export($format, $data);
    
    $extension = match($format) {
        'excel' => 'xlsx',
        'word' => 'docx',
        default => $format
    };
    
    return response($content)
        ->header('Content-Type', $this->getMimeType($format))
        ->header('Content-Disposition', 'attachment; filename="report.' . $extension . '"');
}

private function getMimeType($format) {
    return match($format) {
        'pdf' => 'application/pdf',
        'excel' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'word' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'csv' => 'text/csv',
    };
}

use Shoaib3375\PhpDocExporter\DocumentExporter;

public function exportInvoice(Request $request)
{
    $data = [
        'title' => 'Sales Invoice',
        'invoiceNumber' => 'INV-2024-001',
        'customerName' => 'মোহাম্মদ শোয়েব',
        'items' => [
            ['name' => 'Product A', 'quantity' => 2, 'price' => 500],
            ['name' => 'পণ্য বি', 'quantity' => 1, 'price' => 1000],
        ],
        'total' => 2000
    ];

    $exporter = new DocumentExporter();
    $content = $exporter->exportFromView('pdf', 'invoice', $data, [
        'paper' => 'A4',
        'orientation' => 'portrait'
    ]);

    return response($content)
        ->header('Content-Type', 'application/pdf')
        ->header('Content-Disposition', 'attachment; filename="invoice.pdf"');
}

Route::middleware('auth:sanctum')->post('/export/invoice', function (Request $request) {
    $token = $request->bearerToken();
    
    $data = ['title' => 'Invoice', 'items' => [...]];
    
    $exporter = new DocumentExporter();
    $content = $exporter->exportFromView('pdf', 'invoice', $data, [], $token);
    
    return response($content)
        ->header('Content-Type', 'application/pdf')
        ->header('Content-Disposition', 'attachment; filename="invoice.pdf"');
});

$exporter = new DocumentExporter();
$content = $exporter->exportFromView('pdf', 'invoice', [
    'title' => 'Sales Invoice',
    'invoiceNumber' => 'INV-001',
    'items' => [...],
    'total' => 2000
]);

$options = [
    'title' => 'Sales Report',
    'paper' => 'A4',           // A4, Letter, Legal
    'orientation' => 'landscape', // portrait or landscape
    'font' => 'NotoSansBengali' // Optional: override auto-detected font
];

$content = $exporter->export('pdf', $data, $options);

$options = [
    'title' => 'Monthly Report'
];

$content = $exporter->export('word', $data, $options);

$data = [
    ['name' => 'John', 'email' => '[email protected]'],
    ['name' => 'জন', 'email' => '[email protected]']
];

$exporter = new DocumentExporter();

// Export with token validation
$content = $exporter->export('pdf', $data, [], $apiToken);
// Throws InvalidArgumentException if token is invalid

use Shoaib3375\PhpDocExporter\Config;

$config = new Config();

// Validate tokens
if ($config->canAccessFullApi($token)) {
    // Full access: create, edit, update
}

if ($config->canAccessSafeApi($token)) {
    // Safe access: export only
}

$data = [
    ['নাম' => 'শোয়েব', 'বয়স' => '২৫', 'শহর' => 'ঢাকা'],
];
$pdf = $exporter->export('pdf', $data); // font is chosen automatically

$pdf = $exporter->export('pdf', $data, ['font' => 'Kalpurush']);
bash
composer 
bash
php artisan vendor:publish --provider="Shoaib3375\PhpDocExporter\PhpDocExporterServiceProvider"