Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Html Converter Bundle Laravel Package

bicpi/html-converter-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install via Composer
    composer require bicpi/html-converter-bundle
    
  2. Enable the Bundle Add to config/bundles.php:
    return [
        // ...
        Bicpi\HtmlConverterBundle\BicpiHtmlConverterBundle::class => ['all' => true],
    ];
    
  3. First Use Case Convert HTML to plain text in a controller:
    use Bicpi\HtmlConverterBundle\Converter\HtmlConverter;
    
    class MyController extends AbstractController
    {
        public function convertAction(HtmlConverter $converter)
        {
            $html = '<h1>Hello</h1><p>World!</p>';
            $text = $converter->convert($html);
            // Returns: "Hello\n\nWorld!"
            return new Response($text);
        }
    }
    

Key Files

  • Configuration: config/packages/bicpi_html_converter.yaml (if present).
  • Documentation: Resources/doc/index.md (for advanced usage).

Implementation Patterns

Common Workflows

  1. Service Injection Use dependency injection for reusable conversions:

    public function __construct(private HtmlConverter $converter) {}
    
    public function processEmail(string $htmlBody): string
    {
        return $this->converter->convert($htmlBody);
    }
    
  2. Twig Integration Add a Twig extension for frontend conversions:

    // src/Twig/AppExtension.php
    class AppExtension extends \Twig\Extension\AbstractExtension
    {
        public function __construct(private HtmlConverter $converter) {}
    
        public function getFunctions(): array
        {
            return [
                new \Twig\TwigFunction('html_to_text', [$this, 'convertHtml']),
            ];
        }
    
        public function convertHtml(string $html): string
        {
            return $this->converter->convert($html);
        }
    }
    
  3. Batch Processing Loop through HTML strings (e.g., emails or CMS content):

    $converter = $container->get(HtmlConverter::class);
    $texts = array_map([$converter, 'convert'], $htmlArray);
    

Integration Tips

  • Symfony Forms: Use for sanitizing user-submitted HTML before storage.
  • API Responses: Convert rich-text fields to plain text for email notifications.
  • Caching: Cache converted results if the same HTML is processed repeatedly.

Gotchas and Tips

Pitfalls

  1. Deprecated Package

    • Last release in 2016; verify compatibility with Symfony 5/6/7.
    • Check for forks (e.g., this one) if issues arise.
  2. Configuration Overrides

    • The bundle may not support modern Symfony config formats (e.g., services.yaml overrides).
    • Default settings are hardcoded; inspect src/DependencyInjection/BicpiHtmlConverterExtension.php for options.
  3. Edge Cases

    • Tables: The converter may flatten tables unpredictably. Workaround: Pre-process HTML with DOMDocument for critical cases.
    • Unicode: Non-Latin scripts (e.g., CJK) might render as ????. Tip: Test with your target locale early.

Debugging

  • Log Raw Output Compare input/output to debug conversions:
    $converter->convert($html, ['debug' => true]); // If supported
    
  • Fallback Logic Wrap conversions in a try-catch for graceful degradation:
    try {
        return $converter->convert($html);
    } catch (\Exception $e) {
        return fallbackToPlainText($html);
    }
    

Extension Points

  1. Custom Rules Extend the underlying HtmlConverter library (if source is available) to add:

    • Preserved formatting (e.g., keep bold/italics as *text*).
    • Custom tag handling (e.g., <code> blocks).
  2. Event Listeners Hook into Symfony events (e.g., kernel.response) to auto-convert HTML in responses:

    // src/EventListener/HtmlConverterListener.php
    class HtmlConverterListener
    {
        public function __construct(private HtmlConverter $converter) {}
    
        public function onKernelResponse(GetResponseEvent $event)
        {
            $response = $event->getResponse();
            if ($response->headers->contains('Content-Type', 'text/html')) {
                $body = $this->converter->convert($response->getContent());
                $response->setContent($body);
            }
        }
    }
    
  3. Alternative Libraries If the bundle is limiting, consider:

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php