Installation
composer require aldaflux/doc-img-bundle
Ensure your composer.json meets the package’s PHP/Symfony version requirements (PHP ≥5.3.2, Symfony ≥3.0).
Bundle Registration
Add to config/bundles.php (Symfony 4+) or app/AppKernel.php (Symfony 3):
return [
// ...
Aldaflux\DocImgBundle\AldafluxDocImgBundle::class => ['all' => true],
];
Configuration
Define the web_dir in config/packages/aldaflux_doc_img.yaml:
aldaflux_doc_img:
web_dir: "%kernel.project_dir%/public"
First Use Case
Render a Twig template with embedded images (use double quotes for src attributes):
<img src="/bundles/aldafluxdocimg/images/inda.png">
<img src="https://upload.wikimedia.org/.../Logo_Microsoft_Word_2013.png">
Generate an MHT file in a controller:
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class DocController extends AbstractController {
public function generateDoc() {
$html = $this->renderView('path/to/your_template.html.twig');
$mht = $this->get('HtmlToMht')->GenereHtmlToMht($html);
$response = new Response($mht, 200, [
'Content-Type' => 'application/vnd.ms-word',
'Content-Disposition' => 'attachment; filename="document.doc"',
]);
return $response;
}
}
Template Design
templates/documents/invoice.html.twig with dynamic data:
<h1>{{ customer.name }}</h1>
<img src="{{ asset('images/logo.png') }}">
Controller Integration
$html = $this->renderView('documents/invoice.html.twig', [
'customer' => $customerData,
]);
$mht = $this->get('HtmlToMht')->GenereHtmlToMht($html);
return new Response($mht, 200, [
'Content-Disposition' => 'attachment; filename="invoice.doc"',
]);
Routing
# config/routes.yaml
app.doc_generate:
path: /generate-doc
controller: App\Controller\DocController::generateDoc
Local Image Handling
public/bundles/aldafluxdocimg/images/ or use Symfony’s asset() helper:
<img src="{{ asset('bundles/aldafluxdocimg/images/logo.png') }}">
Batch Processing Loop through records and generate multiple MHT files:
foreach ($invoices as $invoice) {
$html = $this->renderView('documents/invoice.html.twig', ['invoice' => $invoice]);
$mht = $this->get('HtmlToMht')->GenereHtmlToMht($html);
$this->saveMhtToDisk($mht, "invoice_{$invoice->id}.doc");
}
Custom Image Paths
Override the web_dir per environment (e.g., config/packages/dev/aldaflux_doc_img.yaml):
aldaflux_doc_img:
web_dir: "%kernel.project_dir%/public/dev-assets"
Testing
Use the built-in test route (/aldaflux/) to verify functionality:
php bin/console server:run
Visit http://localhost:8000/aldaflux/ to test the bundle’s default template.
Image Paths
public/bundles/aldafluxdocimg/images/ or referenced via asset().') in src attributes break the MHT generation. Use ".Symfony Version Mismatch
config/bundles.php is updated.Class 'Aldaflux\DocImgBundle\AldafluxDocImgBundle' not found → Verify bundle registration.MHT Output Issues
|e filter if needed).web_dir points to the correct public directory.Caching
Log HTML Before Conversion Dump the Twig-rendered HTML to debug:
file_put_contents('debug.html', $html);
Open debug.html in a browser to verify rendering.
Check MHT Generation Save the raw MHT output for inspection:
file_put_contents('debug.mht', $mht);
Open debug.mht in Word to diagnose issues.
Service Not Found
If $this->get('HtmlToMht') fails:
services.yaml for overrides).php bin/console cache:clear
Custom MHT Headers
Extend the Response headers for metadata (e.g., author, subject):
$response->headers->set('X-Author', 'Your Name');
$response->headers->set('Subject', 'Generated Document');
Post-Processing Hook into the MHT generation to modify content (e.g., add watermarks):
$mht = $this->get('HtmlToMht')->GenereHtmlToMht($html);
$mht = str_replace('<body>', '<body><div class="watermark">Draft</div>', $mht);
Image Optimization Pre-process images (resize/compress) before embedding them in the template:
<img src="{{ asset('images/optimized_' ~ image.name) }}">
Use libraries like intervention/image for dynamic resizing.
Event Listeners Listen for MHT generation events (if the bundle exposes them) to add custom logic:
# config/services.yaml
services:
App\EventListener\DocGenerationListener:
tags:
- { name: kernel.event_listener, event: aldaflux.doc_img.generate, method: onGenerate }
How can I help you explore Laravel packages today?