PHP code example of kriansa / h2p

1. Go to this page and download the library: Download kriansa/h2p 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/ */

    

kriansa / h2p example snippets


use H2P\Converter\PhantomJS;

$converter = new PhantomJS();

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert('http://www.google.com/', '/home/user/Documents/page.pdf');

use H2P\Converter\PhantomJS;
use H2P\TempFile;

$converter = new PhantomJS();
$input = new TempFile('<b>test string to pdf</b>', 'html'); // Make sure the 2nd parameter is 'html'

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert($input, '/home/user/Documents/page.pdf');

use H2P\Converter\PhantomJS;
use H2P\TempFile;
use H2P\Request;
use H2P\Request\Cookie;

$converter = new PhantomJS(array(
    // You should use 'search_paths' when you want to point the phantomjs binary to somewhere else
    // 'search_paths' => shell_exec('which phantomjs'),
    'orientation' => PhantomJS::ORIENTATION_LANDSCAPE,
    'format' => PhantomJS::FORMAT_A4,
    'zoomFactor' => 2,
    'border' => '1cm',
    'header' => array(
        'height' => '1cm',
        'content' => "<h1>{{pageNum}} / {{totalPages}}</h1>",
    ),
    'footer' => array(
        'height' => '1cm',
        'content' => "<h1>{{pageNum}} / {{totalPages}}</h1>",
    ),
));

// Create a full custom request
$request = new Request(
    'http://www.google.com/',
    Request::METHOD_POST,
    array('param' => 'value'), // POST params
    array('X-Header' => 'value'), // Custom headers
    array(
        new Cookie('Cookie', 'value', 'domain'), // Create a basic cookie
    )
);

$destination = new TempFile();
$converter->convert($request, $destination);