Install the Bundle
Add to composer.json:
"require": {
"ddeboer/livedocx-bundle": "^1.0"
}
Run composer update.
Enable the Bundle
Add to app/AppKernel.php:
new Ddeboer\LiveDocxBundle\DdeboerLiveDocxBundle(),
Configure API Credentials
Add to app/config/config.yml:
livedocx:
api_key: "%env(LIVEDOCX_API_KEY)%"
api_secret: "%env(LIVEDOCX_API_SECRET)%"
First Use Case: Generate a Document Inject the service in a controller:
use Ddeboer\LiveDocxBundle\Service\LiveDocxService;
class DocumentController extends Controller
{
public function generateAction(LiveDocxService $liveDocx)
{
$templateId = 'YOUR_TEMPLATE_ID';
$data = ['name' => 'John Doe', 'date' => date('Y-m-d')];
$document = $liveDocx->generate($templateId, $data);
return new Response($document->getContent());
}
}
Template Management
liveDocx->getTemplates() to list available templates..docx template via the LiveDocx API and note its ID.Dynamic Document Generation
generate():
$liveDocx->generate('template_123', [
'client_name' => $user->name,
'contract_date' => $order->created_at->format('Y-m-d'),
]);
$response = new Response($document->getContent());
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$response->headers->set('Content-Disposition', 'attachment; filename="document.docx"');
Batch Processing
foreach ($users as $user) {
$document = $liveDocx->generate('invoice_template', [
'user' => $user->name,
'amount' => $user->balance,
]);
$this->saveDocument($document, $user->id);
}
Error Handling
try {
$document = $liveDocx->generate($templateId, $data);
} catch (\Zend\Service\LiveDocx\Exception\RuntimeException $e) {
$this->addFlash('error', 'Failed to generate document: ' . $e->getMessage());
}
API Key/Secret Exposure
config.yml violates security best practices..env with symfony/dotenv) and validate them in parameters.yml:
parameters:
livedocx.api_key: "%env(LIVEDOCX_API_KEY)%"
livedocx.api_secret: "%env(LIVEDOCX_API_SECRET)%"
Ensure .env is in .gitignore.Template ID Mismatches
404 errors.getTemplates() before use:
$templates = $liveDocx->getTemplates();
dump($templates); // Verify IDs match your records.
Large File Handling
Deprecated Zend\Service\LiveDocx
Zend\Service\LiveDocx is unmaintained. Monitor for breaking changes or fork the bundle if needed.Caching Templates
getTemplates()) to reduce API calls:
$templates = $this->get('cache')->get('livedocx_templates', function() use ($liveDocx) {
return $liveDocx->getTemplates();
});
Custom Document Naming
$filename = 'contract_' . $user->id . '_' . date('Ymd') . '.docx';
$response->headers->set('Content-Disposition', "attachment; filename=\"$filename\"");
Testing
LiveDocxService in PHPUnit:
$mock = $this->createMock(\Ddeboer\LiveDocxBundle\Service\LiveDocxService::class);
$mock->method('generate')->willReturn(new \Zend\Service\LiveDocx\Response\Document('fake_content'));
$this->container->set('ddeboer_livedocx.service', $mock);
Extension Points
// src/Ddeboer/LiveDocxBundle/Service/CustomLiveDocxService.php
class CustomLiveDocxService extends \Ddeboer\LiveDocxBundle\Service\LiveDocxService
{
public function generate($templateId, array $data)
{
$data['generated_at'] = now()->format('Y-m-d');
return parent::generate($templateId, $data);
}
}
Register it in services.yml:
services:
ddeboer_livedocx.service:
class: App\Service\CustomLiveDocxService
arguments: ['%livedocx.api_key%', '%livedocx.api_secret%']
Logging
# config/packages/monolog.yaml
handlers:
livedocx:
type: stream
path: "%kernel.logs_dir%/livedocx.log"
level: debug
channels: ["!event"]
Then log in the service:
$this->logger->debug('LiveDocx API call', ['template' => $templateId, 'data' => $data]);
How can I help you explore Laravel packages today?