feat: add Go, PHP, and Laravel SDKs
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled

- Go SDK: zero deps, functional options pattern, full endpoint coverage
- PHP SDK: PHP 8.1+, curl-based, PdfOptions class, PSR-4 autoloading
- Laravel package: ServiceProvider, Facade, config publishing
- All SDKs document complete PDF options including new v0.4.5 params
This commit is contained in:
DocFast Bot 2026-02-21 13:29:48 +00:00
parent 1545df9a7b
commit bc67c52d3a
13 changed files with 1133 additions and 0 deletions

183
sdk/php/src/Client.php Normal file
View file

@ -0,0 +1,183 @@
<?php
declare(strict_types=1);
namespace DocFast;
/**
* DocFast API client for HTML/Markdown to PDF conversion.
*
* @see https://docfast.dev/docs
*/
class Client
{
private string $apiKey;
private string $baseUrl;
private int $timeout;
public function __construct(string $apiKey, string $baseUrl = 'https://docfast.dev', int $timeout = 60)
{
if (empty($apiKey)) {
throw new \InvalidArgumentException('API key is required');
}
$this->apiKey = $apiKey;
$this->baseUrl = rtrim($baseUrl, '/');
$this->timeout = $timeout;
}
/**
* Convert HTML to PDF.
*
* @param string $html HTML content
* @param string|null $css Optional CSS to inject
* @param PdfOptions|null $options PDF options
* @return string Raw PDF bytes
* @throws DocFastException
*/
public function html(string $html, ?string $css = null, ?PdfOptions $options = null): string
{
$body = ['html' => $html];
if ($css !== null) {
$body['css'] = $css;
}
return $this->convert('/v1/convert/html', $body, $options);
}
/**
* Convert Markdown to PDF.
*
* @param string $markdown Markdown content
* @param string|null $css Optional CSS to inject
* @param PdfOptions|null $options PDF options
* @return string Raw PDF bytes
* @throws DocFastException
*/
public function markdown(string $markdown, ?string $css = null, ?PdfOptions $options = null): string
{
$body = ['markdown' => $markdown];
if ($css !== null) {
$body['css'] = $css;
}
return $this->convert('/v1/convert/markdown', $body, $options);
}
/**
* Convert a URL to PDF.
*
* @param string $url URL to convert
* @param PdfOptions|null $options PDF options
* @return string Raw PDF bytes
* @throws DocFastException
*/
public function url(string $url, ?PdfOptions $options = null): string
{
return $this->convert('/v1/convert/url', ['url' => $url], $options);
}
/**
* List available templates.
*
* @return array<array{id: string, name: string, description?: string}>
* @throws DocFastException
*/
public function templates(): array
{
$data = $this->get('/v1/templates');
$result = json_decode($data, true);
return $result['templates'] ?? [];
}
/**
* Render a template to PDF.
*
* @param string $templateId Template ID
* @param array $data Template data
* @return string Raw PDF bytes
* @throws DocFastException
*/
public function renderTemplate(string $templateId, array $data = []): string
{
return $this->post('/v1/templates/' . urlencode($templateId), ['data' => $data]);
}
private function convert(string $path, array $body, ?PdfOptions $options): string
{
if ($options !== null) {
$body = array_merge($body, $options->toArray());
}
return $this->post($path, $body);
}
private function post(string $path, array $body): string
{
$ch = curl_init($this->baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->apiKey,
'Content-Type: application/json',
'Accept: application/pdf',
],
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($response === false) {
throw new DocFastException('Request failed: ' . $error, 0);
}
if ($statusCode >= 400) {
$this->handleError($response, $statusCode);
}
return $response;
}
private function get(string $path): string
{
$ch = curl_init($this->baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->apiKey,
'Accept: application/json',
],
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($response === false) {
throw new DocFastException('Request failed: ' . $error, 0);
}
if ($statusCode >= 400) {
$this->handleError($response, $statusCode);
}
return $response;
}
private function handleError(string $response, int $statusCode): never
{
$message = "HTTP $statusCode";
$code = null;
$data = json_decode($response, true);
if (is_array($data)) {
$message = $data['error'] ?? $message;
$code = $data['code'] ?? null;
}
throw new DocFastException($message, $statusCode, $code);
}
}

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace DocFast;
class DocFastException extends \RuntimeException
{
public readonly int $statusCode;
public readonly ?string $errorCode;
public function __construct(string $message, int $statusCode, ?string $errorCode = null, ?\Throwable $previous = null)
{
$this->statusCode = $statusCode;
$this->errorCode = $errorCode;
parent::__construct($message, $statusCode, $previous);
}
}

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace DocFast;
/**
* PDF generation options.
*/
class PdfOptions
{
/** Page size: A4, Letter, Legal, A3, A5, Tabloid. Ignored if width/height set. */
public ?string $format = null;
/** Landscape orientation. */
public ?bool $landscape = null;
/** Page margins using CSS units (e.g. "20mm"). */
public ?array $margin = null;
/** Print background graphics and colors. */
public ?bool $printBackground = null;
/** Suggested filename for the PDF download. */
public ?string $filename = null;
/** HTML template for page header. Requires displayHeaderFooter: true. */
public ?string $headerTemplate = null;
/** HTML template for page footer. */
public ?string $footerTemplate = null;
/** Show header and footer templates. */
public ?bool $displayHeaderFooter = null;
/** Scale of webpage rendering (0.1 to 2.0). */
public ?float $scale = null;
/** Paper ranges to print, e.g. "1-3,5". */
public ?string $pageRanges = null;
/** Give CSS @page size priority over format. */
public ?bool $preferCSSPageSize = null;
/** Paper width with units (e.g. "8.5in"). Overrides format. */
public ?string $width = null;
/** Paper height with units (e.g. "11in"). Overrides format. */
public ?string $height = null;
public function toArray(): array
{
$data = [];
foreach ([
'format', 'landscape', 'margin', 'printBackground', 'filename',
'headerTemplate', 'footerTemplate', 'displayHeaderFooter',
'scale', 'pageRanges', 'preferCSSPageSize', 'width', 'height',
] as $key) {
if ($this->$key !== null) {
$data[$key] = $this->$key;
}
}
return $data;
}
}