Laravel
A Laravel SDK for the Docstron API, allowing you to easily generate PDFs, manage templates, applications, documents, and track usage statistics.
Installation
Section titled “Installation”You can install the package via composer:
composer require docstron/laravel-sdkConfiguration
Section titled “Configuration”Publish the configuration file:
php artisan vendor:publish --tag=docstron-configAdd your API key to your .env file:
DOCSTRON_API_KEY=your-api-keyDOCSTRON_BASE_URL=https://api.docstron.com/v1Applications
Section titled “Applications”use Docstron\Laravel\Facades\Docstron;
// Get all applications$apps = Docstron::getApplications();
// Get a specific application$app = Docstron::getApplication('app-7b4d78fb-820c-4ca9-84cc-46953f211234');Usage Statistics
Section titled “Usage Statistics”use Docstron\Laravel\Facades\Docstron;
// Get usage statistics and limits$usage = Docstron::getUsage();
// Response includes:// - applications (total, limit, usage_percentage)// - templates (total, limit, usage_percentage)// - documents (total, monthly, usage, monthly_limit, usage_percentage)// - subscription (plan_name, api_rate_limit, processing_priority, support_level)Templates
Section titled “Templates”use Docstron\Laravel\Facades\Docstron;
// Get all templates$templates = Docstron::getTemplates();
// Create a template$template = Docstron::createTemplate([ 'application_id' => 'app-7b4d78fb-820c-4ca9-84cc-46953f211234', 'name' => 'Invoice Template', 'content' => '<h1>Invoice for {{ customer_name }}</h1><p>Amount: {{ amount }}</p>', 'is_active' => true, 'extra_css' => '@page { margin: 3cm; }', // Optional]);
// Get a specific template$template = Docstron::getTemplate('template-c2465c0b-fc54-4672-b9ac-7446886cd6de');
// Update a template$template = Docstron::updateTemplate('template-c2465c0b-fc54-4672-b9ac-7446886cd6de', [ 'name' => 'Updated Invoice Template', 'is_active' => false,]);
// Delete a templateDocstron::deleteTemplate('template-c2465c0b-fc54-4672-b9ac-7446886cd6de');Documents
Section titled “Documents”Generate Document
Section titled “Generate Document”use Docstron\Laravel\Facades\Docstron;
// Generate a document with direct PDF response$pdfContent = Docstron::generateDocument([ 'template_id' => 'template-c2465c0b-fc54-4672-b9ac-7446886cd6de', 'data' => [ 'customer_name' => 'John Doe', 'amount' => '$299.00', ], 'response_type' => 'pdf', // Default]);
// Save the PDF to filefile_put_contents('invoice.pdf', $pdfContent);
// Generate with Base64 response$response = Docstron::generateDocument([ 'template_id' => 'template-c2465c0b-fc54-4672-b9ac-7446886cd6de', 'data' => [ 'customer_name' => 'John Doe', 'amount' => '$299.00', ], 'response_type' => 'json_with_base64',]);
// Generate with Document ID response$response = Docstron::generateDocument([ 'template_id' => 'template-c2465c0b-fc54-4672-b9ac-7446886cd6de', 'data' => [ 'customer_name' => 'John Doe', 'amount' => '$299.00', ], 'response_type' => 'document_id',]);
// Generate password-protected PDF$pdfContent = Docstron::generateDocument([ 'template_id' => 'template-c2465c0b-fc54-4672-b9ac-7446886cd6de', 'data' => [ 'customer_name' => 'John Doe', 'amount' => '$299.00', ], 'response_type' => 'pdf', 'password' => 'SecureDoc2025!',]);Quick Generate Document
Section titled “Quick Generate Document”use Docstron\Laravel\Facades\Docstron;
// Quick generate without pre-creating a template$pdfContent = Docstron::quickGenerateDocument([ 'html_content' => '<h1>Hello {{ name }}</h1><p>Welcome to Docstron!</p>', 'data' => [ 'name' => 'John Doe', ], 'response_type' => 'pdf',]);
// Quick generate with custom CSS$pdfContent = Docstron::quickGenerateDocument([ 'html_content' => '<h1>Invoice</h1>', 'extra_css' => '@page { margin: 2cm; }', 'response_type' => 'pdf',]);
// Quick generate and save as template$response = Docstron::quickGenerateDocument([ 'html_content' => '<h1>Receipt</h1>', 'save_as_template' => true, 'template_name' => 'Receipt Template', 'application_id' => 'app-7b4d78fb-820c-4ca9-84cc-46953f211234', 'response_type' => 'document_id',]);Manage Documents
Section titled “Manage Documents”use Docstron\Laravel\Facades\Docstron;
// Get all documents$documents = Docstron::getDocuments();
// Get a specific document$document = Docstron::getDocument('document-489a79af-8680-4a08-a777-df52f26f296f');
// Download a document$pdfContent = Docstron::downloadDocument('document-489a79af-8680-4a08-a777-df52f26f296f');file_put_contents('downloaded.pdf', $pdfContent);
// Update a document$document = Docstron::updateDocument('document-489a79af-8680-4a08-a777-df52f26f296f', [ 'attributes' => [ 'customer_name' => 'Jane Doe', 'amount' => '$399.00', ],]);
// Delete a documentDocstron::deleteDocument('document-489a79af-8680-4a08-a777-df52f26f296f');Response Types
Section titled “Response Types”When generating documents, you can specify different response types:
pdf(default): Returns the PDF file content directly as binary datajson_with_base64: Returns JSON with the PDF encoded as base64document_id: Returns JSON with the document ID for later retrieval
Error Handling
Section titled “Error Handling”The SDK throws GuzzleException for HTTP errors. You should wrap your calls in try-catch blocks:
use GuzzleHttp\Exception\GuzzleException;use Docstron\Laravel\Facades\Docstron;
try { $pdf = Docstron::generateDocument([ 'template_id' => 'template-id', 'data' => ['name' => 'John'], ]);} catch (GuzzleException $e) { // Handle error logger()->error('Docstron API Error: ' . $e->getMessage());}Contributing
Section titled “Contributing”Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
Section titled “License”This project is licensed under the MIT License - see the LICENSE file for details.
Support
Section titled “Support”- 📚 Documentation
- 💬 Issues
- 📧 Email: support@docstron.com