PHP / Slim
In this chapter, you’ll integrate Oicana into a PHP web service using Slim. Slim is a lightweight PHP micro-framework well-suited for building APIs and web services. We’ll create a simple web service that compiles your Oicana template to PDF and serves it via an HTTP endpoint.
Let’s start with a fresh Slim project. Create a new directory for your PHP project (separate from your template directory) and initialize it with Composer:
mkdir my-pdf-servicecd my-pdf-servicecomposer initInstall Slim with its PSR-7 implementation:
composer require slim/slim slim/psr7Create an index.php file with the following basic Slim application:
<?phpdeclare(strict_types=1);
use Psr\Http\Message\ResponseInterface as Response;use Psr\Http\Message\ServerRequestInterface as Request;use Slim\Factory\AppFactory;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response) { $response->getBody()->write('Hello World'); return $response;});
$app->run();You can test it by running php -S localhost:8000 index.php and navigating to http://localhost:8000 in your browser. You should see “Hello World”.
New service endpoint
Section titled “New service endpoint”We will define a new endpoint to compile our Oicana template to a PDF and return the PDF file to the user.
-
Create a new directory in the PHP project called
templatesand copyexample-0.1.0.zipinto that directory. -
Add the
oicana/oicanaComposer package as a dependency. The package is hosted on a custom Composer repository, so add the following to yourcomposer.jsonfirst:{"repositories": [{"type": "composer","url": "https://composer.oicana.com"}],"require": {"oicana/oicana": "^0.1.0-alpha.4"}}Then configure the minimum stability, allow the Oicana installer plugin, and install:
Terminal window composer config minimum-stability alphacomposer config allow-plugins.oicana/installer truecomposer install -
Before proceeding, make sure your template is packaged. Navigate to your template directory and run:
Terminal window cd /path/to/your/templateoicana pack -
Update
index.phpto load the template at startup and add a compile endpoint:index.php <?phpdeclare(strict_types=1);use Oicana\CompilationMode;use Oicana\Template;use Psr\Http\Message\ResponseInterface as Response;use Psr\Http\Message\ServerRequestInterface as Request;use Slim\Factory\AppFactory;require __DIR__ . '/vendor/autoload.php';// Load the template at startup$templateBytes = file_get_contents('templates/example-0.1.0.zip');if ($templateBytes === false) {die('Failed to load template file. Make sure templates/example-0.1.0.zip exists.');}$template = new Template($templateBytes);$app = AppFactory::create();$app->post('/compile', function (Request $request, Response $response) use ($template) {$pdf = $template->compile(mode: CompilationMode::Development);$response->getBody()->write($pdf);return $response->withHeader('Content-Type', 'application/pdf')->withHeader('Content-Disposition', 'attachment; filename="example.pdf"');});$app->run();This code loads the template once at application startup. The
/compileendpoint compiles the template and returns the PDF file. We explicitly pass the compilation mode with$template->compile(mode: CompilationMode::Development)so the template uses the development value you defined for theinfoinput ({ "name": "Chuck Norris" }). In a follow-up step, we will set an input value instead.
Start the PHP development server with PHP_INI_SCAN_DIR set so the extension is loaded:
export PHP_INI_SCAN_DIR=":/path/to/my-pdf-service/vendor/oicana/installer/php"php -S localhost:8000 index.phpTest the endpoint with curl:
curl -X POST http://localhost:8000/compile --output example.pdfThe generated example.pdf file should contain your template with the development value.
About performance
Section titled “About performance”The PDF generation should not take longer than a couple of milliseconds. You can measure the compilation time with PHP’s built-in microtime(true) before and after the compile call.
Passing inputs from PHP
Section titled “Passing inputs from PHP”Our compile endpoint is currently calling $template->compile() with development mode. Now we’ll provide explicit input values and switch to production mode:
$app->post('/compile', function (Request $request, Response $response) use ($template) { $pdf = $template->compile( jsonInputs: ['info' => ['name' => 'Baby Yoda']] );
$response->getBody()->write($pdf); return $response ->withHeader('Content-Type', 'application/pdf') ->withHeader('Content-Disposition', 'attachment; filename="example.pdf"');});Notice that we removed the explicit mode: CompilationMode::Development parameter. The compile() method defaults to CompilationMode::Production when no mode is specified. Production mode is the recommended default for all document compilation in your application - it ensures you never accidentally generate a document with test data. In production mode, the template will never fall back to development values. If an input value is missing in production mode and the input does not have a default value, the compilation will fail unless your template handles none values for that input.
Calling the endpoint now will result in a PDF with “Baby Yoda” instead of “Chuck Norris”. Building on this minimal service, you could set input values based on database entries or the request payload. Take a look at the open source PHP example project on GitHub for a more complete showcase of the Oicana PHP integration, including blob inputs, error handling, and request validation.
Complete code at the end of this chapter
<?phpdeclare(strict_types=1);
use Oicana\Template;use Psr\Http\Message\ResponseInterface as Response;use Psr\Http\Message\ServerRequestInterface as Request;use Slim\Factory\AppFactory;
require __DIR__ . '/vendor/autoload.php';
// Load the template at startup$templateBytes = file_get_contents('templates/example-0.1.0.zip');if ($templateBytes === false) { die('Failed to load template file. Make sure templates/example-0.1.0.zip exists.');}$template = new Template($templateBytes);
$app = AppFactory::create();
$app->post('/compile', function (Request $request, Response $response) use ($template) { $pdf = $template->compile( jsonInputs: ['info' => ['name' => 'Baby Yoda']] );
$response->getBody()->write($pdf); return $response ->withHeader('Content-Type', 'application/pdf') ->withHeader('Content-Disposition', 'attachment; filename="example.pdf"');});
$app->run();