KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

How to generate a report for a single record - rather than foreach row? (i.e. an Invoice) #2067

Open Jeremy Gray opened this topic on on May 10, 2021 - 4 comments

Jeremy Gray commented on May 10, 2021

I've looked at all of the examples, but I can't seem to find anything that does what we want.

Example...

Maybe I want to create a pdf / report of an "Invoice" which is typically going to be a single row in a table 'invoices'.

What method would be the best way to generate a printable report of a single record like this?

Sebastian Morales commented on May 11, 2021

Let's try these steps:

  1. Get rows of invoices from your database/file/etc

  2. Loop through the rows and generate report class and PDF view file for each row, then create the report and export it to PDF:

foreach ($rows as $i => $row) {
    //...build $reportClassContent and $reportPDFViewContent here based on $row
    file_put_contents("MyReport" . $i . ".php", $reportClassContent);
    file_put_contents("MyReport" . $i . "PDF.view.php", $reportPDFViewContent);
    include "MyReport" . $i . ".php";
    $reportClass = "MyReport" . $i;
    $report = new $reportClass();
    $report->run->export("MyReport" . $i . "PDF")->pdf(array(...))->saveAs("MyReport" . $i . ".pdf");
}

Let us know if you have any question. Rgds,

KoolReport commented on May 11, 2021

Here is another solution:

You create an Invoice report which receive invoiceId as parameter, the usage will be like this:

//index.php
$invoiceId = 3; // Let say you want to print invoice number 3
$invoice = new Invoice([
    "invoiceId"=>$invoiceId,
]);
$invoice->export()->pdf()->saveAs("invoice_$i.pdf");

So you create Invoice.php like this:

class Invoice extends \koolreport\KoolReport
{
    use \koolreport\export\Exportable;

    protected function settings()
    {
        // return your datasources here
    }

    protected function setup()
    {
        //Simple flow to get single record
        $this->src("mydb")
        ->query("select * from invoices where invoiceId=:invoiceId")
        ->params([
            ":invoiceId"=>$this->params["invoiceId"]
        ])
        ->pipe($this->dataStore("result"));
    }
}

Here is the Invoice.view.php file:

<?php
    $invoiceRow = $this->dataStore("result")->get(0); // Get the data row in form of associate array
    //Base on the 
?>
<html>
<body>
<!-- You design your own invoice table html here and you have your data from $invoiceRow -->
</body>
</html>

Let me know if you need further assistance.

KoolReport commented on May 11, 2021

Another way (which I think the best): If you have your invoice data already, you can insert into Invoice class as parameters. The Invoice class will not need to connect to database, rather just create PDF only:

$invoice = new Invoice([
    "invoiceData"=>$data
]);
$invoice->export()->pdf()->saveAs("invoice.pdf");
<?php
//Invoice.php
class Invoice extends \koolreport\KoolReport
{
    use \koolreport\export\Exportable;
}
//Invoice.view.php
<html>
<body>
    <h1><?php echo $this->params["invoiceData"]["customerName"]; ?></h1>
</body>
</html>
Jeremy Gray commented on May 11, 2021

A little confused on where $data comes from on the last example

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None