Html2Media is a powerful Laravel Filament package that allows you to generate PDFs, preview documents, and directly print content from your application. 🚀
The Html2MediaAction provides a set of flexible actions for your Filament resources, enabling:
To install the package, simply run the following command:
composer require torgodly/html2media
Once installed, the Html2MediaAction can be used within your Filament resources or tables.
Here’s how you can customize your Html2MediaAction!
filename()
Set the name of the generated PDF file. ✍️
Usage:
Html2MediaAction::make('print') ->filename('my-custom-document')
'document.pdf'
string
or Closure
pagebreak()
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 Closure
format()
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 Closure
enableLinks()
Enable or disable automatic hyperlink conversion in the PDF. 🔗
Usage:
Html2MediaAction::make('print') ->enableLinks()
false
bool
or Closure
scale()
Adjust the scaling factor for HTML to PDF conversion. 🔍
Usage:
Html2MediaAction::make('print') ->scale(2)
2
int
or Closure
print()
Enable or disable the print button in the modal. 🖨️
Usage:
Html2MediaAction::make('print') ->print(true)
true
bool
or Closure
preview()
Enable a preview option for the document content before printing or saving. 👀
Usage:
Html2MediaAction::make('print') ->preview()
false
bool
or Closure
savePdf()
Enable the option to directly save the content as a PDF. 💾
Usage:
Html2MediaAction::make('print') ->savePdf()
false
bool
or Closure
requiresConfirmation()
Show a confirmation modal before performing the action. 🛑
Usage:
Html2MediaAction::make('print') ->requiresConfirmation()
true
bool
or Closure
content()
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 Closure
Here’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 Html2MediaAction in the same way, whether it's in a Filament table action or a regular action. Simply import the appropriate class:
use Torgodly\Html2Media\Actions\Html2MediaAction;use Torgodly\Html2Media\Tables\Actions\Html2MediaAction;
This makes the action flexible and usable in various contexts. 🌍
Html2MediaAction::make('print') ->content(fn($record) => view('invoice', ['record' => $record]))
This will directly open the print dialog for the HTML content. 🖨️
Html2MediaAction::make('print') ->savePdf() ->content(fn($record) => view('invoice', ['record' => $record]))
This will save the HTML content as a PDF. 💾
The Html2Media package for Filament 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! 🎉