docfast/sdk/php/README.md
DocFast Bot bc67c52d3a
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Has been cancelled
feat: add Go, PHP, and Laravel SDKs
- 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
2026-02-21 13:29:48 +00:00

3.6 KiB
Raw Permalink Blame History

DocFast PHP SDK

Official PHP client for the DocFast HTML/Markdown to PDF API.

Requirements

  • PHP 8.1+
  • ext-curl
  • ext-json

Installation

composer require docfast/docfast-php

Quick Start

use DocFast\Client;

$client = new Client('df_pro_your_api_key');

// HTML to PDF
$pdf = $client->html('<h1>Hello World</h1>');
file_put_contents('output.pdf', $pdf);

Usage

HTML to PDF

$pdf = $client->html('<h1>Hello</h1><p>My document</p>');

HTML with CSS

$pdf = $client->html(
    '<h1>Styled</h1>',
    'h1 { color: navy; font-family: Georgia; }'
);

HTML with PDF Options

use DocFast\PdfOptions;

$options = new PdfOptions();
$options->format = 'Letter';
$options->landscape = true;
$options->margin = ['top' => '20mm', 'bottom' => '20mm', 'left' => '15mm', 'right' => '15mm'];
$options->printBackground = true;

$pdf = $client->html('<h1>Report</h1>', null, $options);

Markdown to PDF

$pdf = $client->markdown('# Hello World\n\nThis is **bold** text.');

URL to PDF

$pdf = $client->url('https://example.com');

Headers and Footers

$options = new PdfOptions();
$options->displayHeaderFooter = true;
$options->headerTemplate = '<div style="font-size:10px;text-align:center;width:100%">My Document</div>';
$options->footerTemplate = '<div style="font-size:10px;text-align:center;width:100%">Page <span class="pageNumber"></span>/<span class="totalPages"></span></div>';
$options->margin = ['top' => '40mm', 'bottom' => '20mm'];

$pdf = $client->html($html, null, $options);

Custom Page Size

$options = new PdfOptions();
$options->width = '8.5in';
$options->height = '11in';
$options->scale = 0.8;

$pdf = $client->html($html, null, $options);

Templates

// List templates
$templates = $client->templates();

// Render a template
$pdf = $client->renderTemplate('invoice', [
    'company' => 'Acme Corp',
    'items' => [['name' => 'Widget', 'price' => 9.99]],
]);

PDF Options

Option Type Default Description
format string "A4" Page size: A4, Letter, Legal, A3, A5, Tabloid
landscape bool false Landscape orientation
margin array null Margins with top/bottom/left/right keys (CSS units)
printBackground bool true Print background graphics
filename string null Suggested filename
headerTemplate string null HTML header template
footerTemplate string null HTML footer template
displayHeaderFooter bool false Show header/footer
scale float 1 Rendering scale (0.12.0)
pageRanges string null Pages to print (e.g. "1-3,5")
preferCSSPageSize bool false Prefer CSS @page size
width string null Custom paper width
height string null Custom paper height

Error Handling

use DocFast\DocFastException;

try {
    $pdf = $client->html('<h1>Test</h1>');
} catch (DocFastException $e) {
    echo "Error: {$e->getMessage()} (status: {$e->statusCode})\n";
}

Configuration

// Custom base URL
$client = new Client('key', 'https://staging.docfast.dev');

// Custom timeout (seconds)
$client = new Client('key', 'https://docfast.dev', 120);

Laravel Integration

See the DocFast Laravel package for a dedicated Laravel integration with facades, config, and service provider.