PHP code example of dericktan / phpjasper

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

    

dericktan / phpjasper example snippets




use JasperPHP\JasperPHP;

$input = __DIR__ . '/vendor/dericktan/phpjasper/examples/hello_world.jrxml';

$jasper = new JasperPHP;
$jasper->compile($input)->execute();



use JasperPHP\JasperPHP;

$input = __DIR__ . '/vendor/dericktan/phpjasper/examples/hello_world.jasper';
$output = __DIR__;

$jasper = new JasperPHP;

$jasper->process(
	$input,
	$output,
	array("pdf", "rtf")
)->execute();



use JasperPHP\JasperPHP;

$input = __DIR__ . '/vendor/dericktan/phpjasper/examples/hello_world_params.jrxml';

$jasper = new JasperPHP;
$output = $jasper->list_parameters($input)->execute();

foreach($output as $parameter_description)
    print $parameter_description . '<pre>';



use JasperPHP\JasperPHP;

$input = __DIR__ . '/vendor/dericktan/phpjasper/examples/hello_world.jrxml';
$output = __DIR__;

$jasper = new JasperPHP;
$jasper->process(
	$input,
	$output,
	array("pdf", "rtf"),
	array("php_version" => phpversion()),
	array(
		'driver' => 'postgres',
		'username' => 'vagrant',
		'host' => 'localhost',
		'database' => 'samples',
		'port' => '5432',
	)						
)->execute();

use JasperPHP\JasperPHP;

Route::get('/reports', function () {

    $output = public_path() . '/report/'.time().'_hello_world';
    $report = new JasperPHP;
    $report->process(
    	public_path() . '/report/hello_world.jrxml',
        $output,
        array('pdf', 'rtf', 'xml'),
        array(),
        array()  
        )->execute();
});


use JasperPHP\JasperPHP;

public function xmlToPdf()
    {
        $output = public_path() . '/report/'.time().'_CancelAck';
        $ext = "pdf";
        $data_file = public_path() . '/report/CancelAck.xml';
        $driver = 'xml';
        $xml_xpath = '/CancelResponse/CancelResult/ID';
		
        $jasper = new JasperPHP;
        
        $jasper->process(
            public_path() . '/report/CancelAck.jrxml',
            $output,
            array($ext),
            array(),
            array('data_file' => $data_file, 'driver' => $driver, 'xml_xpath' => $xml_xpath),                   
            false,
            false
        )->execute();

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.time().'_CancelAck.'.$ext);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: ' . filesize($output.'.'.$ext));
        flush();
        readfile($output.'.'.$ext);
        unlink($output.'.'.$ext);

    }


use JasperPHP\JasperPHP;

public function jsonToPdf()
    {
        $output = public_path() . '/report/'.time().'_Contacts';
        $ext = "pdf";
        $driver = 'json';
        $json_query= "contacts.person";
        $data_file = public_path() . '/report/contacts.json';

        $jasper = new JasperPHP;

        $jasper->process(
            public_path() . '/report/json.jrxml',
            $output,
            array($ext),
            array(),
            array('data_file' => $data_file, 'driver' => $driver, 'json_query' => $json_query
        )->execute();

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.time().'_Contacts.'.$ext);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: ' . filesize($output.'.'.$ext));
        flush();
        readfile($output.'.'.$ext);
        unlink($output.'.'.$ext);

    }


use JasperPHP\JasperPHP;

public function jsonToPdf()
    {
        $output = public_path() . '/report/'.time().'_Contacts';
        $ext = "pdf";
        $driver = 'json';
        $json_query= "contacts.person";
        $data_file = public_path() . '/report/contacts.json';
        $password = "jasper";

        $jasper = new JasperPHP;

        $jasper->process(
            public_path() . '/report/json.jrxml',
            $output,
            array($ext),
            array(),
            array('data_file' => $data_file, 'driver' => $driver, 'json_query' => $json_query,
            null,
            null,
            $password
        )->execute();

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.time().'_Contacts.'.$ext);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: ' . filesize($output.'.'.$ext));
        flush();
        readfile($output.'.'.$ext);
        unlink($output.'.'.$ext);

    }