PHP SDK for the GeneratePDFs.com API, your go-to place for HTML to PDF.
Upload your HTML files, along with any CSS files and images to generate a PDF. Alternatively provide a URL to generate a PDF from it's contents.
composer require generatepdfs/php-sdkSign up for an account on GeneratePDFs.com and head to the API Tokens section and create a new token.
use GeneratePDFs\GeneratePDFs;
$client = GeneratePDFs::connect('YOUR_API_TOKEN');use GeneratePDFs\GeneratePDFs;
use GeneratePDFs\Pdf;
// Simple HTML file
$pdf = $client->generateFromHtml('/path/to/file.html');
// HTML file with CSS
$pdf = $client->generateFromHtml(
'/path/to/file.html',
'/path/to/file.css'
);
// HTML file with CSS and images
$pdf = $client->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
],
[
'name' => 'photo.jpg',
'path' => '/path/to/photo.jpg'
]
]
);$pdf = $client->generateFromUrl('https://example.com');// Retrieve a PDF by its ID
$pdf = $client->getPdf(123);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();
}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
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
- PHP 8.1 or higher
- Guzzle HTTP Client 7.0 or higher
To run the test suite and code style checker, execute:
composer testThis will run both PHP CodeSniffer (PSR-2 standard) and Pest tests.
Contributions and suggestions are welcome and will be fully credited.
We accept contributions via Pull Requests on GitHub.
- 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.
See CHANGELOG.md for a history of changes.
This project is licensed under the MIT License. See the LICENSE file for details.