<?php
require_once "MyReport.php";
$report = new MyReport;
$report->run();
/**
 * Follow the instruction here to get token key
 * https://www.koolreport.com/docs/cloudexport/chromeheadlessio/#get-token-key
 */
$secretToken = 'my_cloud_export_secret_token';

/**
 * The version 2 service host runs an up-to-date Chromium engine. It is opt-in:
 * leave serviceHost unset and you keep hitting the version 1 host exactly as
 * before. Switching can move page breaks, font metrics and spacing slightly,
 * so compare against a known-good file before moving production reports over.
 */
$serviceHost = 'https://service.chromeheadless.io/v2';

$type = isset($_GET['type']) ? $_GET['type'] : 'PDF';
$settings = [
	// 'useLocalTempFolder' => true,
	"pageWaiting" => "networkidle2", //load, domcontentloaded, networkidle0, networkidle2

	/**
	 * Resource cache. The client hashes the assets your page references and
	 * sends a manifest naming the ones the export server already has, instead
	 * of re-uploading them. KoolReport's own js and css are the same bytes on
	 * every install, so they stop being re-zipped into every request.
	 *
	 * From php-client 2.1.0 against a service base ending in /v2 it is already
	 * on and cacheCustom scope is already global, so nothing below is needed
	 * and this export uses the cache without configuring it. The block is
	 * spelled out, commented, so you can see the options and what they default
	 * to. Uncomment it on an older client or any other host, where the cache
	 * stays off until you turn it on, or to change one of the values. Setting
	 * "enabled" => false opts out. A 1.x client ignores the block entirely,
	 * because the manifest is built client-side and 1.x has no such code.
	 */
	// "resourceCache" => [
	// 	"enabled"  => true,               // already true on a /v2 base
	// 	"cacheDir" => sys_get_temp_dir(), // the default; must be writable, NOT the package folder
	// 	"sync" => true,                   // pull the shared hash list from the service
	// 	"syncInterval" => 86400,          // seconds between those pulls
	// 	"cacheCustom" => ["scope" => "global"], // default whenever caching is active
	// ],
];

if ($type === 'cloudJPG') {
    $report->cloudExport("MyReportPDF")
    ->chromeHeadlessio($secretToken, $serviceHost)
	->settings($settings)
    ->jpg(array(
        // "format"=>"A4",
        // "fullPage" => false
    ))
    ->toBrowser("MyReport.jpg")
    ;
} else if ($type === 'cloudPDF') {
    $pdfOptions = [
        "format"=>"A4",
        'landscape'=>false,
        "noRepeatTableFooter" => true,
    ];
    /**
     * Passing the host as the second argument of chromeHeadlessio() is the same
     * thing as putting "serviceHost" => $serviceHost inside settings(). Either
     * form works, and khtml() and phantomjs() take the same second argument.
     */
    $report->cloudExport("MyReportPDF")
    ->chromeHeadlessio($secretToken, $serviceHost)
    ->settings($settings)
    ->pdf($pdfOptions)
    ->toBrowser("MyReport.pdf")
    ;
}
