back

PHP documentation

Install typeset.sh

There are two ways to use typeset.sh 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 typeset.sh functions and classes.

<?php require_once 'typeset.sh.phar'; $html = file_get_contents('invoice.html'); $pdf = typesetsh\pdf($html); $pdf->save('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 packages.typeset.sh to your composer.json.

composer config repositories.typesetsh composer https://packages.typeset.sh

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 http-basic.packages.typeset.sh "{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 typeset.sh, 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');

Performance

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 typeset.sh is the right tool for your needs.