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:
- π PDF Generation: Convert HTML to a PDF and download it.
- π¨οΈ Direct Printing: Print HTML content directly from the application.
- π Document Preview: Preview the content in a modal before printing or exporting.
- π¨ Customizable File Naming: Define a custom name for the generated PDF.
- π Preview & Print Options: Preview the content before printing or saving as a PDF.
- π Page Configuration: Adjust page orientation, size, margins, and scaling.
- π οΈ Advanced PDF Options: Control page breaks, hyperlink inclusion, and more.
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!
Set the name of the generated PDF file. βοΈ
Usage:
Html2MediaAction::make('print')
->filename('my-custom-document')
- π·οΈ Default:
'document.pdf'
- π Accepts:
string
orClosure
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.
Set the page orientation for the PDF, either portrait or landscape. πΌοΈ
Usage:
Html2MediaAction::make('print')
->orientation('landscape')
- π·οΈ Default:
'portrait'
- π Accepts:
string
('portrait'
,'landscape'
) orClosure
Define the format of the PDF, including standard sizes like A4 or custom dimensions. π
Usage:
Html2MediaAction::make('print')
->format('letter', 'in')
- π·οΈ Default:
'a4'
- π Accepts:
string
,array
(e.g.,[width, height]
), orClosure
Enable or disable automatic hyperlink conversion in the PDF. π
Usage:
Html2MediaAction::make('print')
->enableLinks()
- π·οΈ Default:
false
- π Accepts:
bool
orClosure
Adjust the scaling factor for HTML to PDF conversion. π
Usage:
Html2MediaAction::make('print')
->scale(2)
- π·οΈ Default:
2
- π Accepts:
int
orClosure
Enable or disable the print button in the modal. π¨οΈ
Usage:
Html2MediaAction::make('print')
->print(true)
- π·οΈ Default:
true
- π Accepts:
bool
orClosure
Enable a preview option for the document content before printing or saving. π
Usage:
Html2MediaAction::make('print')
->preview()
- π·οΈ Default:
false
- π Accepts:
bool
orClosure
Enable the option to directly save the content as a PDF. πΎ
Usage:
Html2MediaAction::make('print')
->savePdf()
- π·οΈ Default:
false
- π Accepts:
bool
orClosure
Show a confirmation modal before performing the action. π
Usage:
Html2MediaAction::make('print')
->requiresConfirmation()
- π·οΈ Default:
true
- π Accepts:
bool
orClosure
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]))
- π Accepts:
View
,Htmlable
, orClosure
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:
- π Generate a PDF from the
invoice
Blade view. - π¨οΈ Allow users to
preview
andprint
the document. - πΎ Enable
saving as PDF
and show a confirmation modal before executing. - π Set A4 format with portrait orientation.
- π Enable links and set custom margins.
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. π
- For direct printing:
Html2MediaAction::make('print')
->content(fn($record) => view('invoice', ['record' => $record]))
This will directly open the print dialog for the HTML content. π¨οΈ
- For saving as PDF:
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! π