PHP code example of xslain / html2media

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

    

xslain / html2media example snippets


// In your Livewire component or controller
public function render()
{
    return view('your-view', [
        'record' => $this->record
    ]);
}



namespace App\Http\Livewire;

use Livewire\Component;
use Xslain\Html2Media\Traits\HasHtml2MediaBase;

class InvoicePdfGenerator extends Component
{
    use HasHtml2MediaBase;

    public $invoice;

    public function mount($invoice)
    {
        $this->invoice = $invoice;
        
        // Configure the PDF settings
        $this->content(fn() => view('invoices.pdf', ['invoice' => $this->invoice]))
             ->filename('invoice-' . $this->invoice->id)
             ->orientation('portrait')
             ->format('a4')
             ->savePdf(true)
             ->print(true);
    }

    public function render()
    {
        return view('livewire.invoice-pdf-generator');
    }

    public function getElementId(): string
    {
        return 'invoice-' . $this->invoice->id;
    }

    public function downloadPdf()
    {
        $this->dispatch('triggerPrint', ...$this->getDispatchOptions('savePdf'));
    }

    public function printInvoice()
    {
        $this->dispatch('triggerPrint', ...$this->getDispatchOptions('print'));
    }
}

$this->filename('my-custom-document')

$this->pagebreak('section', ['css', 'legacy'])

$this->orientation('landscape')

$this->format('a4', 'mm')
// or custom dimensions
$this->format([210, 297], 'mm') // A4 dimensions

$this->scale(2)

$this->margin([10, 15, 10, 15]) // [top, right, bottom, left]
// or uniform margin
$this->margin(10)

$this->enableLinks(true)

$this->content(fn() => view('invoice', ['record' => $this->record]))



namespace App\Http\Livewire;

use Livewire\Component;
use Xslain\Html2Media\Traits\HasHtml2MediaBase;

class ReportGenerator extends Component
{
    use HasHtml2MediaBase;

    public $report;

    public function mount($report)
    {
        $this->report = $report;
        
        // Configure all PDF settings
        $this->content(fn() => view('reports.pdf-template', ['report' => $this->report]))
             ->filename('report-' . $this->report->id)
             ->orientation('landscape') // Landscape for wide reports
             ->format('a4', 'mm') // A4 format with mm units
             ->scale(2) // High quality
             ->margin([10, 15, 10, 15]) // Custom margins
             ->enableLinks(true) // Enable clickable links
             ->pagebreak('section', ['css', 'legacy']) // Page breaks after sections
             ->savePdf(true) // Enable save PDF button
             ->print(true); // Enable print button
    }

    public function render()
    {
        return view('livewire.report-generator');
    }

    public function getElementId(): string
    {
        return 'report-' . $this->report->id;
    }
}

Html2MediaAction::make('print')
    ->pagebreak('section', ['css', 'legacy'])

Html2MediaAction::make('print')
    ->orientation('landscape')

Html2MediaAction::make('print')
    ->format('letter', 'in')

Html2MediaAction::make('print')
    ->enableLinks()

Html2MediaAction::make('print')
    ->scale(2)

Html2MediaAction::make('print')
    ->print(true)

Html2MediaAction::make('print')
    ->preview()

Html2MediaAction::make('print')
    ->savePdf()

Html2MediaAction::make('print')
    ->

Html2MediaAction::make('print')
    ->content(fn($record) => view('invoice', ['record' => $record]))

Html2MediaAction::make('print')
    ->scale(2)
    ->print() // Enable print option
    ->preview() // Enable preview option
    ->filename('invoice') // Custom file name
    ->savePdf() // Enable save as PDF option
    ->]) // Set custom margins
    ->content(fn($record) => view('invoice', ['record' => $record])) // Set content

// Use the full-featured component
@livewire('html2media', [
    'record' => $record,
    'content' => view('invoice', ['record' => $record])
])

// Or use the simple button component
@livewire('html2media-button', [
    'record' => $record,
    'label' => 'Download PDF',
    'content' => view('invoice', ['record' => $record])
])
bash
php artisan vendor:publish --tag=html2media-assets