| Install | |
|---|---|
composer require xslain/html2media |
|
| Latest Version: | 1.0.0 |
| PHP: | ^8.1|^8.2|^8.3 |
Html2Media is a powerful Laravel Livewire package that allows you to generate PDFs, preview documents, and directly print content from your application. 🚀
The Html2Media package provides flexible Livewire components for your Laravel applications, enabling:
To install the package, simply run the following command:
composer require xslain/html2media
After installation, publish the assets:
php artisan vendor:publish --tag=html2media-assets
The Html2Media components will be automatically registered with Livewire.
You can use the Html2Media Livewire component directly in your Blade templates:
// In your Livewire component or controller
public function render()
{
return view('your-view', [
'record' => $this->record
]);
}
<!-- In your Blade template -->
@livewire('html2media', [
'record' => $record,
'content' => view('your-pdf-template', ['record' => $record])
])
For a simple button that triggers PDF generation:
@livewire('html2media-button', [
'record' => $record,
'label' => 'Download Invoice',
'content' => view('invoice-template', ['record' => $record])
])
Create your own Livewire component that uses the Html2Media trait:
<?php
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'));
}
}
Here's how you can customize your Html2Media components!
filename()Set the name of the generated PDF file. ✍️
Usage:
$this->filename('my-custom-document')
'document.pdf'string or Closurepagebreak()Define page break behavior. Customize how and where page breaks occur within the document. 🛑
Usage:
$this->pagebreak('section', ['css', 'legacy'])
🔄 Default: ['mode' => ['css', 'legacy'], 'after' => 'section']
🛠️ Accepts:
mode: Array of strings (['avoid-all', 'css', 'legacy'])after: Element ID, class, tag, or * for all elements.avoid: (Optional) Element ID, class, or tag to avoid page breaks.📖 More info on page breaks: here.
orientation()Set the page orientation for the PDF, either portrait or landscape. 🖼️
Usage:
$this->orientation('landscape')
'portrait'string ('portrait', 'landscape') or Closureformat()Define the format of the PDF, including standard sizes like A4 or custom dimensions. 📏
Usage:
$this->format('a4', 'mm')
// or custom dimensions
$this->format([210, 297], 'mm') // A4 dimensions
'a4'string (e.g., 'a4', 'letter') or array for custom dimensions, plus unit parameterscale()Control the rendering quality of the PDF by adjusting the scale factor. 📊
Usage:
$this->scale(2)
2int or Closuremargin()Set the margins for the PDF document. 📐
Usage:
$this->margin([10, 15, 10, 15]) // [top, right, bottom, left]
// or uniform margin
$this->margin(10)
0int (uniform margin) or array ([top, right, bottom, left]) or ClosureenableLinks()Enable or disable PDF hyperlinks. When enabled, hyperlinks are automatically added on top of all anchor tags. 🌐
Usage:
$this->enableLinks(true)
falsebool or Closurecontent()Set the content for the document. Typically, you'll pass a Blade view for the content. 📝
Usage:
$this->content(fn() => view('invoice', ['record' => $this->record]))
View, Htmlable, or ClosureHere's a complete example of a custom Livewire component with Html2Media:
<?php
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;
}
}
Corresponding Blade template (livewire/report-generator.blade.php):
<div>
<div class="mb-4">
<h3 class="text-lg font-semibold">{{ $report->title }}</h3>
<p class="text-gray-600">Generate PDF or print this report</p>
</div>
<!-- Hidden content for PDF generation -->
<div style="display: none">
<main id="print-smart-content-{{ $this->getElementId() }}" style="color: black;" wire:ignore>
{!! $this->getContent()?->toHtml() !!}
<iframe style="display: none" id="print-smart-iframe-{{ $this->getElementId() }}"></iframe>
</main>
</div>
<!-- Action buttons -->
<div class="flex space-x-3">
@if($this->isSavePdf())
<button
wire:click="$dispatch('triggerPrint', {{ json_encode($this->getDispatchOptions('savePdf')) }})"
type="button"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
📄 Download PDF
</button>
@endif
@if($this->isPrint())
<button
wire:click="$dispatch('triggerPrint', {{ json_encode($this->getDispatchOptions('print')) }})"
type="button"
class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"
>
🖨️ Print Report
</button>
@endif
</div>
</div>
@push('scripts')
<script src="{{ asset('vendor/html2media/js/es6-promise.min.js') }}"></script>
<script src="{{ asset('vendor/html2media/js/jspdf.umd.min.js') }}"></script>
<script src="{{ asset('vendor/html2media/js/html2canvas.min.js') }}"></script>
<script src="{{ asset('vendor/html2media/js/html2pdf.min.js') }}"></script>
<script src="{{ asset('vendor/html2media/js/html2media.js') }}"></script>
@endpush
@livewire('html2media-button', [
'label' => 'Download Invoice',
'content' => view('invoices.pdf', ['invoice' => $invoice])
])
@livewire('html2media', [
'record' => $user,
'content' => view('users.profile-pdf', ['user' => $user])
])
The Html2Media package for Livewire makes it easy to generate PDFs, preview documents, and print content directly from your Laravel app. With flexible configuration options and seamless Livewire integration, you can tailor it to your specific needs, ensuring smooth document handling. ✨
We hope this documentation helps you get started quickly. 🚀 Happy coding! 🎉
'document.pdf'string or Closurepagebreak()Define page break behavior. Customize how and where page breaks occur within the document. 🛑
Usage:
Html2MediaAction::make('print')
->pagebreak('section', ['css', 'legacy'])
🔄 Default: ['mode' => ['css', 'legacy'], 'after' => 'section']
🛠️ Accepts:
mode: Array of strings (['avoid-all', 'css', 'legacy'])after: Element ID, class, tag, or * for all elements.avoid: (Optional) Element ID, class, or tag to avoid page breaks.📖 More info on page breaks: here.
orientation()Set the page orientation for the PDF, either portrait or landscape. 🖼️
Usage:
Html2MediaAction::make('print')
->orientation('landscape')
'portrait'string ('portrait', 'landscape') or Closureformat()Define the format of the PDF, including standard sizes like A4 or custom dimensions. 📏
Usage:
Html2MediaAction::make('print')
->format('letter', 'in')
'a4'string, array (e.g., [width, height]), or ClosureenableLinks()Enable or disable automatic hyperlink conversion in the PDF. 🔗
Usage:
Html2MediaAction::make('print')
->enableLinks()
falsebool or Closurescale()Adjust the scaling factor for HTML to PDF conversion. 🔍
Usage:
Html2MediaAction::make('print')
->scale(2)
2int or Closureprint()Enable or disable the print button in the modal. 🖨️
Usage:
Html2MediaAction::make('print')
->print(true)
truebool or Closurepreview()Enable a preview option for the document content before printing or saving. 👀
Usage:
Html2MediaAction::make('print')
->preview()
falsebool or ClosuresavePdf()Enable the option to directly save the content as a PDF. 💾
Usage:
Html2MediaAction::make('print')
->savePdf()
falsebool or ClosurerequiresConfirmation()Show a confirmation modal before performing the action. 🛑
Usage:
Html2MediaAction::make('print')
->requiresConfirmation()
truebool or Closurecontent()Set the content for the document. Typically, you’ll pass a Blade view for the content. 📝
Usage:
Html2MediaAction::make('print')
->content(fn($record) => view('invoice', ['record' => $record]))
View, Htmlable, or ClosureHere’s a complete example of configuring the Html2MediaAction:
Html2MediaAction::make('print')
->scale(2)
->print() // Enable print option
->preview() // Enable preview option
->filename('invoice') // Custom file name
->savePdf() // Enable save as PDF option
->requiresConfirmation() // Show confirmation modal
->pagebreak('section', ['css', 'legacy'])
->orientation('portrait') // Portrait orientation
->format('a4', 'mm') // A4 format with mm units
->enableLinks() // Enable links in PDF
->margin([0, 50, 0, 50]) // Set custom margins
->content(fn($record) => view('invoice', ['record' => $record])) // Set content
This configuration will:
invoice Blade view.preview and print the document.saving as PDF and show a confirmation modal before executing.You can use the Html2Media components in various ways throughout your Livewire application. Here are the available components:
// 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])
])
This makes the package flexible and usable in various contexts throughout your Laravel application. 🌍
@livewire('html2media-button', [
'record' => $record,
'label' => 'Print Invoice',
'content' => view('invoice', ['record' => $record])
])
This will directly open the print dialog for the HTML content. 🖨️
@livewire('html2media-button', [
'record' => $record,
'label' => 'Download PDF',
'content' => view('invoice', ['record' => $record])
])
This will save the HTML content as a PDF. 💾
The Html2Media package for Livewire makes it easy to generate PDFs, preview documents, and print content directly from your Laravel app. With flexible configuration options, you can tailor it to your specific needs, ensuring smooth document handling. ✨
We hope this documentation helps you get started quickly. 🚀 Happy coding! 🎉