PHP code example of cleaniquecoders / dokufy

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

    

cleaniquecoders / dokufy example snippets


return [
    // Default driver: gotenberg, libreoffice, chromium, phpword, fake
    'default' => env('DOKUFY_DRIVER', 'phpword'),

    'drivers' => [
        'gotenberg' => [
            'url' => env('DOKUFY_GOTENBERG_URL', 'http://gotenberg:3000'),
            'timeout' => env('DOKUFY_GOTENBERG_TIMEOUT', 120),
        ],

        'libreoffice' => [
            'binary' => env('DOKUFY_LIBREOFFICE_BINARY', 'libreoffice'),
            'timeout' => env('DOKUFY_LIBREOFFICE_TIMEOUT', 120),
        ],

        'chromium' => [
            'node_binary' => env('DOKUFY_NODE_BINARY'),
            'npm_binary' => env('DOKUFY_NPM_BINARY'),
            'timeout' => env('DOKUFY_CHROMIUM_TIMEOUT', 60),
        ],

        'phpword' => [
            'pdf_renderer' => env('DOKUFY_PDF_RENDERER', 'dompdf'),
        ],
    ],

    'pdf' => [
        'format' => env('DOKUFY_PDF_FORMAT', 'A4'),
        'orientation' => env('DOKUFY_PDF_ORIENTATION', 'portrait'),
        'margin_top' => env('DOKUFY_PDF_MARGIN_TOP', '1in'),
        'margin_bottom' => env('DOKUFY_PDF_MARGIN_BOTTOM', '1in'),
        'margin_left' => env('DOKUFY_PDF_MARGIN_LEFT', '0.5in'),
        'margin_right' => env('DOKUFY_PDF_MARGIN_RIGHT', '0.5in'),
    ],

    'templates' => [
        'path' => env('DOKUFY_TEMPLATES_PATH', resource_path('templates')),
    ],
];

use CleaniqueCoders\Dokufy\Facades\Dokufy;

// Simple HTML string
Dokufy::html('<h1>Hello World</h1>')
    ->toPdf(storage_path('documents/hello.pdf'));

// With placeholder data
Dokufy::html('<h1>Hello {{ name }}</h1>')
    ->data(['name' => 'Ahmad'])
    ->toPdf(storage_path('documents/greeting.pdf'));

use CleaniqueCoders\Dokufy\Facades\Dokufy;

// DOCX template with placeholders
Dokufy::template(resource_path('templates/offer-letter.docx'))
    ->data([
        'name' => 'Ahmad bin Ali',
        'position' => 'Senior Developer',
        'salary' => 'RM 8,500.00',
    ])
    ->toPdf(storage_path('documents/offer.pdf'));

// HTML template file
Dokufy::template(resource_path('templates/invoice.html'))
    ->data(['invoice_number' => 'INV-001', 'total' => '1,500.00'])
    ->toPdf(storage_path('invoices/inv-001.pdf'));

use CleaniqueCoders\Dokufy\Facades\Dokufy;

// Process placeholders and save as DOCX
Dokufy::template(resource_path('templates/contract.docx'))
    ->data(['client_name' => 'Acme Corp', 'date' => '15 January 2026'])
    ->toDocx(storage_path('contracts/acme-contract.docx'));

use CleaniqueCoders\Dokufy\Facades\Dokufy;

// In a controller - stream PDF inline (for preview)
public function preview(Invoice $invoice)
{
    return Dokufy::template(resource_path('templates/invoice.docx'))
        ->data($invoice->toArray())
        ->stream("invoice-{$invoice->number}.pdf");
}

// Download PDF as attachment
public function download(Invoice $invoice)
{
    return Dokufy::template(resource_path('templates/invoice.docx'))
        ->data($invoice->toArray())
        ->download("invoice-{$invoice->number}.pdf");
}

use CleaniqueCoders\Dokufy\Facades\Dokufy;

$html = view('documents.invoice', compact('invoice'))->render();

Dokufy::html($html)
    ->toPdf(storage_path("invoices/{$invoice->id}.pdf"));

use CleaniqueCoders\Dokufy\Facades\Dokufy;

// Force a specific driver
Dokufy::driver('gotenberg')
    ->html('<h1>Generated with Gotenberg</h1>')
    ->toPdf(storage_path('documents/gotenberg.pdf'));

// Create new instance with driver
Dokufy::make('chromium')
    ->html($htmlContent)
    ->toPdf($outputPath);

use CleaniqueCoders\Dokufy\Facades\Dokufy;
use CleaniqueCoders\Placeholdify\PlaceholderHandler;

$handler = (new PlaceholderHandler())
    ->useContext('employee', $employee, 'emp')
    ->addFormatted('salary', $employee->salary, 'currency', 'MYR')
    ->addDate('start_date', $employee->start_date, 'd F Y');

Dokufy::template(resource_path('templates/offer-letter.docx'))
    ->with($handler)
    ->toPdf(storage_path('documents/offer.pdf'));

use CleaniqueCoders\Dokufy\Facades\Dokufy;

it('generates offer letter PDF', function () {
    Dokufy::fake();

    $employee = Employee::factory()->create();

    // Your code that generates documents
    app(GenerateOfferLetter::class)->execute($employee);

    // Assertions
    Dokufy::assertPdfGenerated();
    Dokufy::assertGenerated(storage_path("documents/offer-{$employee->id}.pdf"));
});

it('generates contract DOCX', function () {
    Dokufy::fake();

    // Your code
    app(GenerateContract::class)->execute($client);

    Dokufy::assertDocxGenerated();
});

// Input methods
Dokufy::template(string $path): self      // Set template file (HTML or DOCX)
Dokufy::html(string $content): self       // Set HTML content directly
Dokufy::data(array $data): self           // Set placeholder data (can be chained)
Dokufy::with(object $handler): self       // Use Placeholdify handler

// Output methods
Dokufy::toPdf(string $outputPath): string              // Generate PDF file
Dokufy::toDocx(string $outputPath): string             // Generate DOCX file
Dokufy::stream(?string $filename = null): StreamedResponse    // Stream PDF inline
Dokufy::download(?string $filename = null): BinaryFileResponse // Download PDF

// Driver selection
Dokufy::driver(string $name): self        // Select specific driver
Dokufy::make(?string $driver = null): self // Create new instance

// Utilities
Dokufy::getAvailableDrivers(): array      // List available drivers
Dokufy::isDriverAvailable(string $driver): bool // Check driver availability
Dokufy::reset(): self                     // Reset instance state

// Testing
Dokufy::fake(): FakeDriver                // Use fake driver
Dokufy::assertGenerated(string $path): void    // Assert file generated
Dokufy::assertPdfGenerated(): void        // Assert PDF generated
Dokufy::assertDocxGenerated(): void       // Assert DOCX generated
bash
php artisan dokufy:install
bash
php artisan vendor:publish --tag="dokufy-config"
bash
php artisan dokufy:status