KoolReport's Forum

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

Export to pdf results in format error for different Axis label #2971

Open Philippe Moser opened this topic on on Feb 3, 2023 - 6 comments

Philippe Moser commented on Feb 3, 2023

I have a ColumnChart witch can be exported to a pdf. It works fine. But I have a similar Chart, where I always get a format error for the exported pdf. The rendering of the chart on my website works fine for both charts.

I found out where the problem is: if I remove the variable $data['DateMonthNameDE'] from the hAxis, Also the working diagram is no longer exported.

But I don't know why this happen. For me it looks like a bug of the exporting system.

I'm using: Symfony 6 php8.0.8 koolreport/core: 6.0.1 koolreport/export: 5.2.0 koolreport/pro: 6.0.6

The working code:

//JobsYearMonth.php

namespace App\Reports\Jobs;

use koolreport\processes\CalculatedColumn;

class JobsYearMonths extends \koolreport\KoolReport
{
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;
    use \koolreport\excel\CSVExportable;

    ...

    protected function setup()
    {
        $this->src("warehouse_datasource")
            ->query("
                SELECT   dim.tDate.DateMonth, COUNT(fact.tJob.JobID) AS amount, dim.tDate.DateYear, dim.tDate.DateMonthNameDE
                FROM     fact.tJob INNER JOIN
                         dim.tDate ON fact.tJob.JobCreatedDateID = dim.tDate.DateID
                WHERE   (fact.tJob.JobCompanyID = :CompanySiteCompanyID 
                             AND ... )
                    )
                GROUP BY dim.tDate.DateYear, dim.tDate.DateMonthNameDE, dim.tDate.DateMonth
                ORDER BY dim.tDate.DateYear, dim.tDate.DateMonth
            ")
            ->params(...)

            ->pipe(new CalculatedColumn(array(
                "month_year"=>array(
                    "exp"=>function($data){
                        return $data["DateMonthNameDE"].", ".$data["DateYear"];
                    },
                    "type"=>"string",
                )
            )))
            ->pipe($this->dataStore("jobs_created"));

//JobsYearMonthsPDF.view.php
<?php

use koolreport\widgets\google\ColumnChart;

?>


<!DOCTYPE html>
<html>

<body style="margin:1in">

...

<?php
ColumnChart::create(array(
    "title"=>"Jobs erstellt",
    "dataSource"=>$this->dataStore("jobs_created"),
    "height" => "500px",
    "columns"=>array(
        "month_year"=>array(
            "label"=>"Monat"
        ),
        "amount"=>array(
            "label"=>"Jobs",
            "type"=>"number",
        )
    ),
    "options" => array(
        "backgroundColor"=>"transparent",
        "chartArea"=> array(
            "top" => 55,
        ),
        "hAxis" => array(
            "slantedText" => true,
            "slantedTextAngle" => 60,
        ),
    )
))
?>
</body>
</html>

After I remove the variable $data["DateMonthNameDE"] it doesn't export anymore:

...
->pipe(new CalculatedColumn(array(

                "month_year"=>array(
                    "exp"=>function($data){
                        return ", ".$data["DateYear"];
                    },
                    "type"=>"string",
                )
            )))
...

What I really want is a similar chart witch shows me the Quarter columns instead of the Months:

...
->query( ... // just a little different query)
...
->pipe(new CalculatedColumn(array(
                "quarter_year"=>array(
                    "exp"=>function($data){
                        return $data["DateQuarter"].". Quartal ".$data["DateYear"];
                    },
                    "type"=>"string",
                )
            )))
...

Could you explain to me why this hAxis makes such problems? Thanks :)

KoolReport commented on Feb 6, 2023

If you don't do exporting, instead you show it on browser, does the new chart work correctly.

Philippe Moser commented on Feb 8, 2023

Yes, if I show it on the browser the chart works correctly.

By the way, this is how the export Method inside the Controller

    /**
     * @Route("/report/jobs/months/pdf", name="_app_report_jobs_months_pdf")
     */
    public function jobsMonthsPdf(Request $request)
    {
        $session = $this->requestStack->getSession();
        $reportId = $session->get('reportId');
        if ($reportId === null) return $this->redirectToRoute('_app_report_selectid');

        $params = array(
            "CompanySiteCompanyID"=>$reportId,
        );

        $report = new JobsYearMonths($this->reportingWarehouseService->getDatasource(), $params);
        $report->run();
        $report->export('JobsYearMonthsPDF')
            ->settings(array(
                "phantomjs"=>$this->getParameter('kernel.project_dir').$this->phantomjsPath,
                "resourceWaiting"=>2000,
            ))
            ->pdf(
                array(
                "format"=>"A4",
                "orientation"=>"portrait",
            ))->toBrowser("JobsYearMonths.pdf");
        return new Response();
    }
Sebastian Morales commented on Feb 8, 2023

Would you pls post the screenshot of the format error? Tks,

Philippe Moser commented on Feb 9, 2023

The format error message depends on the client I try to open the PDF document.

For Mozilla Firefox, it don't show a message, it just don't shows whats inside of the pdf. For Foxit PDF Reader it looks like this:

In the code itself, I don't get an error. The code works fine.

Sebastian Morales commented on Feb 10, 2023

I see the issue. The PDF file format is corrupted might be because of redundant text at the end of file content. Pls remove the return Response() at the end of your export method or add an exit command right after toBrowser() like this:

    public function jobsMonthsPdf(Request $request)
    {
        ...
            ))->toBrowser("JobsYearMonths.pdf");
        exit; // add an exit command right after toBrowser()
        // return new Response(); // or remove, comment out this return
    } 

Let us know how this works for you. Rgds,

Philippe Moser commented on Feb 10, 2023

This works for me! :D

Thank you very much, you're the best :)

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
solved

None