PHP Generate Reports - majorsilence/Reporting GitHub Wiki

You can download the php wrapper for RdlCmd from the release page or you can download it from the master branch.

Tested with php-8.4.6.

The recommend way to use this code is to copy the report.php file into your project. It is a thin layer and rarely changes.

Details

report.php

  • Namespace: MajorsilenceReporting
  • Class: Report(path_to_rdl_file, path_to_rdlcmd, path_to_dotnet)
  • Functions:
    • export(export_type, save_path)
      • export_type: "pdf", "csv", "xslx", "xml", "rtf", "tif", "html"
        • If type does not match it will default to pdf
      • save_path: the directory path and file name that will be generated
    • export_to_memory(export_type)
      • export type: "pdf", "csv", "xslx", "xml", "rtf", "tif", "html"
      • If type does not match it will default to pdf

Examples

The examples demonstrate setup and generating a report.

basic example

Example of calling a basic report and generating the file hello2.pdf.

<?php

// HOWTO run from command line:
// php.exe -f test1.php
// php.exe -f test1.php > test1.log 2>&1
// php.exe -f "C:\PHP test1.php" -- -arg1 -arg2 -arg3

error_reporting(E_ALL);
ini_set('display_errors', '1');

date_default_timezone_set('America/Los_Angeles');

require_once("../report.php");

# SETUP
$current_directory = dirname(__FILE__);
$base_directory = realpath($current_directory . '/../../../');

$db_path = realpath($current_directory . '/../../../Examples/northwindEF.db');
$report_path = realpath($current_directory . '/../../../Examples/SqliteExamples/SimpleTest1.rdl');

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    // If self-hosted or on Windows, we do not need to set the path to dotnet, rdlcmd can be run directly
    $path_to_dotnet = null;
    $path_to_rdlcmd = realpath($base_directory . '/RdlCmd/bin/Release/net8.0/win-x64/publish/RdlCmd.exe');
} else {
    // dotnet is required to run rdlcmd
    // if a self contained build is used, the path to dotnet is not needed and the call should be to RdlCmd instead of RdlCmd.dll directly
    // if a self contained build is not used, the path to dotnet is needed
    $path_to_dotnet = 'dotnet';
    $path_to_rdlcmd = realpath($base_directory . '/RdlCmd/bin/Debug/net8.0/RdlCmd.dll');
}

$output_directory = $current_directory . '/output';
if (!file_exists($output_directory)) {
    mkdir($output_directory, 0777, true);
}

# EXAMPLE REPORT

$rpt = new MajorsilenceReporting\Report($report_path, $path_to_rdlcmd, $path_to_dotnet);
$rpt->set_connection_string('Data Source=' . $db_path);
$rpt->export("pdf", $output_directory . '/test1.pdf');

?>

Example generating setting parameters and a connection string

<?php
// HOWTO run from command line:
// php.exe -f test2-connection-string-parameter.php

error_reporting(E_ALL);
ini_set('display_errors', '1');

date_default_timezone_set('America/Los_Angeles');

require_once("../report.php");


# SETUP
$current_directory = dirname(__FILE__);
$base_directory = realpath($current_directory . '/../../../');

$db_path = realpath($current_directory . '/../../../Examples/northwindEF.db');
$report_path = realpath($current_directory . '/../../../Examples/SqliteExamples/SimpleTest3WithParameters.rdl');

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    // If self-hosted or on Windows, we do not need to set the path to dotnet, rdlcmd can be run directly
    $path_to_dotnet = null;
    $path_to_rdlcmd = realpath($base_directory . '/RdlCmd/bin/Release/net8.0/win-x64/publish/RdlCmd.exe');
} else {
    // dotnet is required to run rdlcmd
    // if a self contained build is used, the path to dotnet is not needed and the call should be to RdlCmd instead of RdlCmd.dll directly
    // if a self contained build is not used, the path to dotnet is needed
    $path_to_dotnet = 'dotnet';
    $path_to_rdlcmd = realpath($base_directory . '/RdlCmd/bin/Debug/net8.0/RdlCmd.dll');
}

$output_directory = $current_directory . '/output';
if (!file_exists($output_directory)) {
    mkdir($output_directory, 0777, true);
}


# EXAMPLE REPORT

$rpt = new MajorsilenceReporting\Report($report_path, $path_to_rdlcmd, $path_to_dotnet);
$rpt->set_parameter("TestParam1", 'I am a parameter value.');
$rpt->set_parameter("TestParam2", 'The second parameter.');
$rpt->set_connection_string('Data Source=' . $db_path);
$rpt->export("pdf", $output_directory . '/test2-parameters.pdf');


?>

Generate report and return the data

This example generates a report and loads it into memory using the export_to_memory function. It then sends it to the browser to view or download.

<?php
// HOWTO run from command line:
// php.exe -f test3-streaming.php

error_reporting(E_ALL);
ini_set('display_errors', '1');

date_default_timezone_set('America/Los_Angeles');

require_once("../report.php");


# SETUP
$current_directory = dirname(__FILE__);
$base_directory = realpath($current_directory . '/../../../');

$db_path = realpath($current_directory . '/../../../Examples/northwindEF.db');
$report_path = realpath($current_directory . '/../../../Examples/SqliteExamples/SimpleTest1.rdl');

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    // If self-hosted or on Windows, we do not need to set the path to dotnet, rdlcmd can be run directly
    $path_to_dotnet = null;
    $path_to_rdlcmd = realpath($base_directory . '/RdlCmd/bin/Release/net8.0/win-x64/publish/RdlCmd.exe');
} else {
    // dotnet is required to run rdlcmd
    // if a self contained build is used, the path to dotnet is not needed and the call should be to RdlCmd instead of RdlCmd.dll directly
    // if a self contained build is not used, the path to dotnet is needed
    $path_to_dotnet = 'dotnet';
    $path_to_rdlcmd = realpath($base_directory . '/RdlCmd/bin/Debug/net8.0/RdlCmd.dll');
}

$output_directory = $current_directory . '/output';
if (!file_exists($output_directory)) {
    mkdir($output_directory, 0777, true);
}


# EXAMPLE REPORT

$rpt = new MajorsilenceReporting\Report($report_path, $path_to_rdlcmd, $path_to_dotnet);
$rpt->set_parameter("TestParam1", 'I am a parameter value.');
$rpt->set_parameter("TestParam2", 'The second parameter.');
$rpt->set_connection_string('Data Source=' . $db_path);
$data = $rpt->export_to_memory("pdf");

header("Content-type: application/octet-stream"); 
header("Content-disposition: attachment; filename=YourFileName2.pdf");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();

echo $data;

?>