PHP documentation

There are two ways to use in your php project: you can use the phar file or use composer.

Using phar file

Once you have signed up and bought a license, you can download the phar file from your project page.

You will then include the phar file in your own php script, simply including it in your code. From there, you can use any functions and classes.

<?php require_once ''; $html = file_get_contents('invoice.html'); $pdf = Typesetsh\createPdf($html); $pdf->toFile('invoice.pdf');

Composer access

You first need to add the repository to your project. This process is simple: run the following command in your composer project, turn adding to your composer.json.

composer config repositories.typesetsh composer

You now need to tell composer how it can authenticate, using the project id and a token. You can create more than one token for each developer or deployment setup if you want. The following command will add the token to your global composer configuration.

composer config -g "{PUBLIC_ID}" "{TOKEN}"

Now all that is left to do; require the typesetsh lib.

composer require typesetsh/typesetsh

Additional information

Security concerns

Always make sure you only allow access to a set of whitelist files. This will make your project secure, preventing any sensitive data leaks. Through, you can define a callback function to validate and modify any external resource.

<?php $resolveUri = function(string $uri, string $base = null) { throw new \RuntimeException("File not allowed"); } $pdf = Typesetsh\createPdf($content, $resolveUri); $pdf->toFile('test.pdf');

Suppose you want to only allow remote files (accessible via http(s)), then you need to make sure those resources are downloaded first.

The class \Typesetsh\Resource\Cache can help you with that, here is an example:

<?php $cache = new \Typesetsh\Resource\Cache(__DIR__.'/cache'); $cache->timeout = 15; // 15 seconds $cache->downloadLimit = 1024 * 1024 * 5; // 5mb $resolveUri = function (string $url) use ($cache) { if (strpos($url, '//') === 0) { $url = 'https:'.$url; } if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) { return $cache->fetch($url); } return ''; // Ignore file }; $pdf = \Typesetsh\createPdf($content, $resolveUri); $pdf->toFile('test.pdf');

The resolveUri method gives you control on how to handle any external resources (Images, CSS, Fonts, etc.)

You can also use the \Typesetsh\UriResolver class. It's a simple implementation for an uri-resolver that can easily by extended.

<?php $pdf = \Typesetsh\createPdf($content, \Typesetsh\UriResolver::httpAndCurrentDir()); $pdf->toFile('test.pdf');

PDF/A-1B conform.

You can save a PDF/A-1B conform PDF by adding a save handler. Certain features are not supported then and must be avoided. For instance, you should not use a color-alpha value or opacity property, as transparency is not allowed.

<?php use Typesetsh\Pdf; $html = "Hello World, I am a pdf/a-1b conform pdf!"; $service = new HtmlToPdf(); $service->saveHandler['pdf_a'] = new Pdf\A_1B_Web(); $result = $service->render($html, \Typesetsh\UriResolver::all(null, __DIR__)); $result->toFile(__DIR__.'/hello.signed.pdf');

Signing a PDF

Pdfs can also be digital signed using a certificate that you provide. This process is quite simple. All you need to provide is a certificate and an optional private key if stored separately.

You can simple create a self signed certificate for testing.

openssl req -x509 -nodes -days 365000 -newkey rsa:2048 -keyout my-certificate.crt -out my-certificate.crt

Then all you need to do, is adding the signature to your HtmlToPdf service.

<?php use Typesetsh\Pdf; $html = "Hello World!"; $signature = new Pdf\Signature('file://'.__DIR__.'/my-certificate.crt'); $signature->ContactInfo = ''; $signature->Location = 'DE'; $signature->Name = 'FooBar'; $signature->Reason = 'Testing'; $service = new HtmlToPdf(); $service->saveHandler['signature'] = $signature; $service->saveHandler['pdf_a'] = new Pdf\A_1B_Web(); $result = $service->render($html, \Typesetsh\UriResolver::all(null, __DIR__)); $result->toFile(__DIR__.'/hello.signed.pdf');


We try our best to ensure optimal performance. From the very start, performance was a key concern, driving many of our architectural design decisions. PHP is not C, but it can still be fast. Of course, rendering is a complex process, and it does require some work. While you can render small documents with a couple of pages within a few hundred milliseconds, big documents with lots of text can eat up significant time and memory.

Please feel free to get in touch with us if you are not sure if is the right tool for your needs.