Installation:
composer require aspose/cloud-bundle:~0.3
Update AppKernel.php to register the bundle:
new Aspose\Bundle\CloudBundle\AsposeCloudBundle(),
Configuration:
Add credentials to config.yml:
aspose_cloud:
url: http://api.aspose.com/v1.1
app:
sid: your_client_id
key: your_client_secret
outputLocation: "%kernel.cache_dir%/aspose_cloud/"
First Use Case: Convert a Word document to PDF in a controller:
use Symfony\Component\HttpFoundation\Response;
public function convertDocumentAction()
{
$app = $this->get('aspose.app');
$wordConverter = $this->get('aspose.wordsconverter');
$wordConverter->setFilename('/path/to/input.docx')
->setFormat('pdf')
->convert();
return new Response('Conversion complete!');
}
File Conversion:
Use service IDs like aspose.wordsconverter, aspose.slidesconverter, or aspose.spreadsheetconverter for format conversions.
$converter = $this->get('aspose.wordsconverter');
$converter->setFilename('/input.docx')
->setFormat('pdf')
->setOutputPath('/output.pdf')
->convert();
Batch Processing: Loop through files and convert them sequentially:
$files = ['file1.docx', 'file2.docx'];
foreach ($files as $file) {
$converter = $this->get('aspose.wordsconverter');
$converter->setFilename($file)
->setFormat('pdf')
->convert();
}
Integration with Forms: Handle file uploads and conversions in a Symfony form:
// Controller
public function uploadAndConvertAction(Request $request)
{
$file = $request->files->get('document');
$converter = $this->get('aspose.wordsconverter');
$converter->setFilename($file->getPathname())
->setFormat('pdf')
->convert();
return $this->render('success.html.twig');
}
Dependency Injection: Inject services directly into controllers or services:
class DocumentService
{
private $wordConverter;
public function __construct($wordConverter)
{
$this->wordConverter = $wordConverter;
}
public function processDocument($filePath)
{
$this->wordConverter->setFilename($filePath)
->setFormat('pdf')
->convert();
}
}
Authentication Errors:
sid and key in config.yml are correct and match your Aspose Cloud credentials.aspose_cloud.url) is accurate (e.g., http://api.aspose.com/v1.1 for production).Output Location:
outputLocation is not set or invalid, conversions may fail silently. Defaults to %kernel.cache_dir%/aspose_cloud/.File Paths:
setFilename() and setOutputPath() to avoid issues with relative paths.Rate Limits:
Aspose\Cloud\Sdk\Exception\ApiException gracefully:
try {
$converter->convert();
} catch (ApiException $e) {
$this->addFlash('error', 'Conversion failed: ' . $e->getMessage());
}
Enable Verbose Logging: Configure the SDK to log requests/responses:
aspose_cloud:
debug: true
Logs will appear in var/log/dev.log (Symfony default).
Check Response Status: Inspect the response object for errors:
$response = $converter->convert();
if (!$response->isSuccess()) {
throw new \RuntimeException($response->getErrorMessage());
}
Test with Small Files: Start with small files (e.g., <1MB) to rule out network or timeout issues.
Custom Services: Extend the bundle to add new converters or modify existing ones:
// services.yml
services:
aspose.custom_converter:
class: AppBundle\Service\CustomConverter
arguments: ['@aspose.app']
Event Listeners: Trigger actions post-conversion (e.g., send email notifications):
// src/AppBundle/EventListener/ConversionListener.php
class ConversionListener
{
public function onConversionSuccess(ConversionEvent $event)
{
// Send email or log success
}
}
Override Configuration: Dynamically override settings in runtime:
$app = $this->get('aspose.app');
$app->setOutputLocation('/custom/path/');
How can I help you explore Laravel packages today?