generatepdfs / laravel-sdk
Laravel SDK for GeneratePDFs.com API
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/generatepdfs/laravel-sdk
Requires
- php: ^8.1
- generatepdfs/php-sdk: ^1.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0
- phpstan/phpstan: ^1.10
- squizlabs/php_codesniffer: ^3.7
README
Laravel SDK for the GeneratePDFs.com API, your go-to place for HTML to PDF.
This package provides seamless integration of the GeneratePDFs PHP SDK into your Laravel application, with service providers, facades, and configuration management.
Installation
composer require generatepdfs/laravel-sdk
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=generatepdfs-config
This will create a config/generatepdfs.php file. Add your API token to your .env file:
GENERATEPDFS_API_TOKEN=your-api-token-here
Get your API Token
Sign up for an account on GeneratePDFs.com and head to the API Tokens section and create a new token.
Usage
Using the Facade
use GeneratePDFs\Laravel\Facades\GeneratePDFs; // Generate PDF from HTML file $pdf = GeneratePDFs::generateFromHtml('/path/to/file.html'); // Generate PDF from HTML with CSS $pdf = GeneratePDFs::generateFromHtml( '/path/to/file.html', '/path/to/file.css' ); // Generate PDF from HTML with CSS and images $pdf = GeneratePDFs::generateFromHtml( '/path/to/file.html', '/path/to/file.css', [ [ 'name' => 'logo.png', 'path' => '/path/to/logo.png', 'mime_type' => 'image/png' // Optional, will be auto-detected ], ] ); // Generate PDF from URL $pdf = GeneratePDFs::generateFromUrl('https://example.com'); // Get PDF by ID $pdf = GeneratePDFs::getPdf(123); // Download PDF if ($pdf->isReady()) { $pdfContent = $pdf->download(); $pdf->downloadToFile('/path/to/save/output.pdf'); } // Refresh PDF data to check for status updates $refreshedPdf = $pdf->refresh(); if ($refreshedPdf->isReady()) { $pdfContent = $refreshedPdf->download(); }
Using Dependency Injection
use GeneratePDFs\GeneratePDFs; class YourController { public function __construct( private GeneratePDFs $generatePDFs ) {} public function generate() { $pdf = $this->generatePDFs->generateFromUrl('https://example.com'); return response()->download( $pdf->downloadToFile(storage_path('app/temp.pdf')) ); } }
Using the Service Container
$generatePDFs = app('generatepdfs'); $pdf = $generatePDFs->generateFromUrl('https://example.com');
Working with PDF Objects
The SDK returns Pdf objects that provide easy access to PDF information and downloading:
// Access PDF properties $pdfId = $pdf->getId(); $pdfName = $pdf->getName(); $status = $pdf->getStatus(); $downloadUrl = $pdf->getDownloadUrl(); $createdAt = $pdf->getCreatedAt(); // Check if PDF is ready if ($pdf->isReady()) { // Download PDF content as string $pdfContent = $pdf->download(); // Or save directly to file $pdf->downloadToFile('/path/to/save/output.pdf'); } // Refresh PDF data from the API (useful for checking status updates) $refreshedPdf = $pdf->refresh(); if ($refreshedPdf->isReady()) { $pdfContent = $refreshedPdf->download(); }
Client Methods
generateFromHtml(string $htmlPath, ?string $cssPath = null, array $images = []): Pdf- Generate a PDF from HTML file(s)generateFromUrl(string $url): Pdf- Generate a PDF from a URLgetPdf(int $id): Pdf- Retrieve a PDF by its IDdownloadPdf(string $downloadUrl): string- Download PDF binary content from a download URL
PDF Object Methods
getId(): int- Get the PDF IDgetName(): string- Get the PDF filenamegetStatus(): string- Get the current status (pending, processing, completed, failed)getDownloadUrl(): string- Get the download URLgetCreatedAt(): DateTimeImmutable- Get the creation dateisReady(): bool- Check if the PDF is ready for downloaddownload(): string- Download and return PDF binary contentdownloadToFile(string $filePath): bool- Download and save PDF to a filerefresh(): Pdf- Refresh PDF data from the API and return a new Pdf instance with updated information
Requirements
- PHP 8.1 or higher
- Laravel 10.0, 11.0, or 12.0
- GeneratePDFs PHP SDK 1.0 or higher
Testing
To run the test suite and code style checker, execute:
composer test
This will run both PHP CodeSniffer (PSR-12 standard) and Pest tests.
Contributing
Contributions and suggestions are welcome and will be fully credited.
We accept contributions via Pull Requests on GitHub.
Pull Requests
- PSR-12 Extended Coding Standard - The easiest way to apply the conventions is to install PHP Code Sniffer.
- Add tests! - Your patch won't be accepted if it doesn't have tests.
- Document any change in behaviour - Make sure the README / CHANGELOG and any other relevant documentation are kept up-to-date.
- Consider our release cycle - We try to follow semver. Randomly breaking public APIs is not an option.
- Create topic branches - Don't ask us to pull from your master branch.
- One pull request per feature - If you want to do more than one thing, send multiple pull requests.
- Send coherent history - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
Changelog
See CHANGELOG.md for a history of changes.
License
This project is licensed under the MIT License. See the LICENSE file for details.