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'));
}
}
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;
}
}