Skip to content

Html2Media is a Laravel package for converting HTML to PDFs with options to download or print. It supports configurable settings like file name, orientation, margins, and integrates with Filament actions for dynamic content rendering. Perfect for documents, invoices, and reports, it simplifies creating high-quality, customizable outputs.

License

Notifications You must be signed in to change notification settings

torgodly/Html2Media

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌟 Html2Media Filament Action Documentation πŸ“„

Html2Media is a powerful Laravel Filament package that allows you to generate PDFs, preview documents, and directly print content from your application. πŸš€


πŸ“Œ Overview

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.

✨ Features

  • 🎨 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.

πŸ”§ Installation

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.


βš™οΈ Configuration Methods

Here’s how you can customize your Html2MediaAction!

1. πŸ“‚ filename()

Set the name of the generated PDF file. ✍️

Usage:

Html2MediaAction::make('print')
    ->filename('my-custom-document')
  • 🏷️ Default: 'document.pdf'
  • πŸ”  Accepts: string or Closure

2. πŸ“„ 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.


3. πŸ”„ orientation()

Set the page orientation for the PDF, either portrait or landscape. πŸ–ΌοΈ

Usage:

Html2MediaAction::make('print')
    ->orientation('landscape')
  • 🏷️ Default: 'portrait'
  • πŸ”  Accepts: string ('portrait', 'landscape') or Closure

4. πŸ“ format()

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]), or Closure

5. πŸ”— enableLinks()

Enable or disable automatic hyperlink conversion in the PDF. πŸ”—

Usage:

Html2MediaAction::make('print')
    ->enableLinks()
  • 🏷️ Default: false
  • πŸ”  Accepts: bool or Closure

6. πŸ”§ scale()

Adjust the scaling factor for HTML to PDF conversion. πŸ”

Usage:

Html2MediaAction::make('print')
    ->scale(2)
  • 🏷️ Default: 2
  • πŸ”  Accepts: int or Closure

7. πŸ–¨οΈ print()

Enable or disable the print button in the modal. πŸ–¨οΈ

Usage:

Html2MediaAction::make('print')
    ->print(true)
  • 🏷️ Default: true
  • πŸ”  Accepts: bool or Closure

8. πŸ‘οΈ preview()

Enable a preview option for the document content before printing or saving. πŸ‘€

Usage:

Html2MediaAction::make('print')
    ->preview()
  • 🏷️ Default: false
  • πŸ”  Accepts: bool or Closure

9. πŸ’Ύ savePdf()

Enable the option to directly save the content as a PDF. πŸ’Ύ

Usage:

Html2MediaAction::make('print')
    ->savePdf()
  • 🏷️ Default: false
  • πŸ”  Accepts: bool or Closure

10. βœ… requiresConfirmation()

Show a confirmation modal before performing the action. πŸ›‘

Usage:

Html2MediaAction::make('print')
    ->requiresConfirmation()
  • 🏷️ Default: true
  • πŸ”  Accepts: bool or Closure

11. πŸ’» 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]))
  • πŸ”  Accepts: View, Htmlable, or Closure

🎨 Example Usage

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 and print 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.

πŸ“Š Filament Action or Table Action

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. 🌍


⚑ Quick Example: Direct Print or Save as PDF

  1. For direct printing:
Html2MediaAction::make('print')
    ->content(fn($record) => view('invoice', ['record' => $record]))

This will directly open the print dialog for the HTML content. πŸ–¨οΈ

  1. For saving as PDF:
Html2MediaAction::make('print')
    ->savePdf()
    ->content(fn($record) => view('invoice', ['record' => $record]))

This will save the HTML content as a PDF. πŸ’Ύ


🏁 Conclusion

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! πŸŽ‰

About

Html2Media is a Laravel package for converting HTML to PDFs with options to download or print. It supports configurable settings like file name, orientation, margins, and integrates with Filament actions for dynamic content rendering. Perfect for documents, invoices, and reports, it simplifies creating high-quality, customizable outputs.

Resources

License

Stars

Watchers

Forks

Packages

No packages published