bytes/docraptor
Lightweight PHP 5 DocRaptor API client to convert HTML into PDF or Excel. Create a document, set HTML content, and send it via cURL with your DocRaptor API key. Supports test mode and exception handling.
Installation Add the package via Composer:
composer require bytes/docraptor:dev-master
Ensure your PHP environment meets the requirements (PHP >= 5.3.2 with cURL).
First Use Case: Convert HTML to PDF
use Bytes\Docraptor\Document\PdfDocument;
use Bytes\Docraptor\Http\Client as HttpClient;
use Bytes\Docraptor\Client;
$document = new PdfDocument('invoice');
$document->setContent('<h1>Hello, DocRaptor!</h1><p>This is a test.</p>');
$httpClient = new HttpClient();
$client = new Client($httpClient, env('DOCRAPTOR_API_KEY'));
$pdf = $client->setTestMode(true)->convert($document);
file_put_contents('output.pdf', $pdf->getContent());
Where to Look First
Document Classes: Explore PdfDocument and ExcelDocument in src/Document/ for supported formats.Client class in src/Client.php for API key, test mode, and timeout settings.HttpClient in src/Http/ for proxy or authentication needs.Document Conversion
$document = new PdfDocument('report');
$document->setContent($htmlContent);
$client->convert($document); // Returns a `Document` object with binary content.
return response($pdf->getContent(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="report.pdf"',
]);
Configuration Management
$client->setTestMode(true);
$httpClient = new HttpClient(['timeout' => 30]);
Integration with Laravel
$this->app->singleton(Client::class, function ($app) {
$httpClient = new HttpClient();
return new Client($httpClient, config('services.docraptor.key'));
});
// app/Facades/DocRaptor.php
public static function convertPdf($html, $name) {
return app(Client::class)->convert(new PdfDocument($name)->setContent($html));
}
Batch Processing
Dispatch(new ConvertDocument($document, $userId))->onQueue('docraptor');
API Key Management
.env:
DOCRAPTOR_API_KEY=your_key_here
Error Handling
DocraptorException for API errors (e.g., invalid HTML, rate limits).
try {
$client->convert($document);
} catch (DocraptorException $e) {
Log::error('DocRaptor failed: ' . $e->getMessage());
return back()->withError('Failed to generate document.');
}
HttpClient calls to handle network issues:
try {
$response = $httpClient->post($url, $data);
} catch (Exception $e) {
// Retry logic or fallback
}
Content Validation
use Illuminate\Support\Str;
$cleanHtml = Str::of($userInput)->markdown()->toHtml();
$document->setContent($cleanHtml);
Rate Limits
// Production
$client->setTestMode(false);
Custom HTTP Client
HttpClient to add headers or logging:
class CustomHttpClient extends HttpClient {
public function __construct() {
parent::__construct(['headers' => ['X-Custom-Header' => 'value']]);
}
}
Document Metadata
setOptions():
$document->setOptions([
'margin_top' => '0.5in',
'margin_bottom' => '0.5in',
]);
Excel-Specific Features
ExcelDocument for spreadsheets:
$excel = new ExcelDocument('spreadsheet');
$excel->setContent('<table><tr><td>Data</td></tr></table>');
$client->convert($excel);
Debugging
HttpClient:
$httpClient = new HttpClient(['verbose' => true]);
Performance
$pdf = Cache::remember("doc_{$documentId}", 3600, function () use ($client, $document) {
return $client->convert($document);
});
Extensions
Storage::disk('s3')->put('documents/report.pdf', $pdf->getContent());
How can I help you explore Laravel packages today?