PHP code example of spacetab-io / wkhtmltopdf

1. Go to this page and download the library: Download spacetab-io/wkhtmltopdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spacetab-io / wkhtmltopdf example snippets


use Amp\Loop;
use Spacetab\WkHTML;

Loop::run(static fn() => 
  yield WkHTML\ToPDF::new()->fromHtml('<p>hello world</p>')->asFile('hi.pdf')
);

use Amp\Loop;
use Spacetab\WkHTML;

Loop::run(static fn() => 
  yield WkHTML\ToPDF::new()->fromUrl('https://google.com')->asFile('google.pdf')
);

use Amp\Loop;
use Spacetab\WkHTML;
use Spacetab\WkHTML\OptionBuilder;

Loop::run(static function () {
  $option = new OptionBuilder\PDF();
  $option->addGrayscale();

  yield WkHTML\ToPDF::new()->fromUrl('https://google.com')->asFile('google.pdf');
});

Loop::run(static fn() =>
    yield [
        WkHTML\ToPDF::new()->fromHtml('<p>hi1</p>')->asFile('hi1.pdf'),
        WkHTML\ToPDF::new()->fromHtml('<p>hi2</p>')->asFile('hi2.pdf'),
        WkHTML\ToPDF::new()->fromHtml('<p>hi3</p>')->asFile('hi3.pdf'),
    ]
);

use Amp\Loop;
use Spacetab\WkHTML;
use Spacetab\WkHTML\OptionBuilder;
use Spacetab\WkHTML\OptionBuilder\OptionBuilderInterface;
use Spacetab\WkHTML\OptionGroup\OptionGroupInterface;

Loop::run(static function () {
    $pdf = new WkHTML\ToPDF(new class implements OptionGroupInterface {
        public function __invoke(): OptionBuilderInterface {
            $option = new OptionBuilder\PDF();
            $option->addGrayscale();

            return $option;
        }
    });

    yield $pdf->fromUrl('https://google.com')->asFile('google.pdf');
});