KoolReport's Forum

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

Font and background styles in pdf reports #1037

Closed Eugene opened this topic on on Aug 12, 2019 - 29 comments

Eugene commented on Aug 12, 2019

Hi, I've already asked about this in one of my topics but recommended solution does not work.

So what i did:

  • I included all my style inline like this:

"grouping" => array(
            "catName" => array(
                "top" => "<td colspan=\"2\" <td style='color: red'><b>{catName} </b></td>",
  • Also I have added the following style section in the head part of my generated html
<style>
        body {
            -webkit-print-color-adjust: exact;
        }
    </style> 

But the exported pdf is still black and white

David Winterburn commented on Aug 13, 2019

Hi Eugene,

Would you please tell us which package did you use to export to PDF, is it Export or CloudExport? Thanks!

Eugene commented on Aug 13, 2019

I use CloudExport

Eugene commented on Aug 13, 2019

Just noticed that the font-size does not work also... it is almost impossible to make good view report.. :-(

Eugene commented on Aug 15, 2019

any ideas?

do your own pdfs keep the css formatting if you use pdf export via CloudExport?

David Winterburn commented on Aug 16, 2019

Hi Eugene,

Would you please post the relevant parts of your php code for the report's setup as well as of the pdf view file? Thanks!

Eugene commented on Aug 16, 2019

Sure i can but it will be pretty long


require_once "../../koolreport/core/autoload.php";

require_once "spoiled2_export.php";

$report = new spoiled2_export;
$report->run();
$report->cloudExport("spoiled2_export")
    ->chromeHeadlessio("***")
    ->pdf([
        "scale"=>0.6,
        "format"=>"A4",
        "landscape"=>false,
        "displayHeaderFooter"=>true,
        "printBackground"=>true,
        "margin"=>array(
            "top"=>'50px',
            "bottom"=>'40px',
        ),
        "headerTemplate"=> '<div id="header-template"
                style="font-size:7px !important; color:#808080; padding-left:10px">
                Created: <span class="date"></span><br>
                Report name: <span class="title"></span><span></span>
            </div>',
        "footerTemplate"=> '<div id="footer-template"
                style="font-size:7px !important; color:#808080; padding-left:10px">
                Page <span class="pageNumber"></span>
                of <span class="totalPages"></span>
            </div>'
    ])
    ->toBrowser("spoiled_" . date('dmyHi').".pdf");

<?php
// Require autoload.php from koolreport library
require_once "../../koolreport/core/autoload.php";


use \koolreport\processes\CalculatedColumn;
use \koolreport\cube\processes\SuperCube;
use \koolreport\processes\Sort;
use \koolreport\processes\ColumnRename;
use \koolreport\processes\Join;



class spoiled2_export extends \koolreport\KoolReport
{
    use \koolreport\clients\Bootstrap;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\cloudexport\Exportable;


    protected function bindParamsToInputs()
    {
        return array(
            "barmode" => "barmode",
            "date" => "dateInput",
            "reportSelect" => "reportSelect",
            "viewAmount" => "viewAmountbox",
            "viewVs" => "viewVsbox",
            "consumptions" => "consumptions",
        );
    }

    public function settings()
    {
        $config = include "../../config.php";
        return array(
            "dataSources" => array(
                "quinos" => $config["quinos"]
            )
        );
    }

    protected function setup()
    { ... THIS PART IS VERY LONG 
}
<?php
use koolreport\processes\CalculatedColumn;
use \koolreport\widgets\koolphp\Table;

$periode1 = DateTime::createFromFormat("Y-m-d H:i:s", $this->params["dateStart1"])->format("F Y");
$periode2 = DateTime::createFromFormat("Y-m-d H:i:s", $this->params["dateStart2"])->format("F Y");
$stock =  ($this->params["barmode"]) ? 'BAR WAREHOUSE' : 'KITCHEN WAREHOUSE';
$sort_template = array();

if ($this->params["viewAmount"][0] == '1') {
    $sort_template = array(
        "periode - periode2 | total - sum" => "asc");
} elseif ($this->params["viewVs"][0] == '2') {
    $sort_template = array(
        "quantityVS" => "desc");
} else {
    $sort_template = array(
        "name" => "asc");
}

$calculated = $this->dataStore('spoiled')->process(new CalculatedColumn(array(

    "diff_consum" => function ($data) {
        if ($data["consumption1"] == "0") {
            return "N/A";
        } else {
            return ($data["consumption2"] - $data["consumption1"]) / $data["consumption1"] * 100;
        }
    },)));
?>

<html lang="en">
<head>
    <link rel="stylesheet" href="/qreports/koolreport/bootstrap3/assets/core/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="/qreports/koolreport/bootstrap3/assets/core/css/bootstrap-theme.min.css"/>
    <meta charset="UTF-8">
    <title>Spoiled Report</title>
    
</head>
<body style="-webkit-print-color-adjust:exact;">
<div class="container box-container">
    <div class="text-center">
        <h2>Spoiled Report</h2>
    </div>
    <hr/>
    <hr/>
    <div>
        <b>Stock: </b> <?= $stock; ?><br><br>
        <b>Main Periode: </b> <?= $periode2; ?><br><br>
    </div>
    <div class="row">
        <?php
        Table::create(array(
            "dataStore" => $calculated,
            "showFooter" => "top",
            "sorting" =>  $sort_template,
            "columns" => array(
                "name" => array(
                    "label" => "Item Name",
                ),
                "periode - periode1 | quantity - sum" => array(
                    "label" => "Quantity<br>" . $periode1,
                    "formatValue" => function ($value, $row) {
                        $value1 = number_format($value, 2, '.', ',') . " " . $row["uom"];
                        return $value1;
                    },
                    "cssStyle" => "background-color: #b1dfbb;",
                ),
                "periode - periode2 | quantity - sum" => array(
                    "label" => "Quantity<br>" . $periode2,
                    "formatValue" => function ($value, $row) {
                        $value1 = number_format($value, 2, '.', ',') . " " . $row["uom"];
                        return $value1;
                    }
                ),
                "quantityVS" => array(
                    "label" => "Quantity:<br>current vs previous",
                    "formatValue" => function ($value, $row) {
                        return ($value == 'N/A') ? 'new spoiled item' : number_format($value, 1, '.', ',') . "%";
                    },
                    "cssStyle" => "background-color: #D3D3D3;",
                ),
                "periode - periode1 | total - sum" => array(
                    "label" => "Amount<br>" . $periode1,
                    "footer" => "sum",
                    "cssStyle" => "background-color: #b1dfbb;",
                    "formatValue" => function ($value, $row) {
                        $value1 = number_format($value, 0, '.', ',');
                        return $value1;}
                ),
                "periode - periode2 | total - sum" => array(
                    "label" => "Amount<br>" . $periode2,
                    "footer" => "sum",
                    "formatValue" => function ($value, $row) {
                        $value1 = number_format($value, 0, '.', ',');
                        return $value1;}
                ),
                "amountVS" => array(
                    "label" => "Amount:<br>current vs previous",
                    "formatValue" => function ($value, $row) {
                        return ($value == 'N/A') ? 'new spoiled item' : number_format($value, 1, '.', ',') . "%";
                    },
                    "cssStyle" => "background-color: #D3D3D3;",
                ),
                "diff_consum" => array(
                    "label" => "Consumption:<br>current vs previous",
                    "formatValue" => function ($value, $row) {
                        return ($value === 'N/A') ? $value : number_format($value, 1, '.', ',') . "%";
                    },
                    "cssStyle" => "background-color: orange;",
                ),
            ),
            "excludedColumns" => $this->params["excluded"],
//            "grouping" => array(
//                "category" => array(
//                    "top" => "<td <td class='top_group'><b>{category} </b></td>",
//                ),
//            ),
            "cssClass" => array(
                "table" => "table table-hover table-bordered",
                "td" => function ($row, $columnName) {
                    return ($columnName == "name") ? "cell-class-name cell-class-small" : "cell-class cell-class-small";
                },
                "th" => "cell-class",
                "tf" => "footer-class"
            )
        ))
        ?>
    </div>
</body>
</html>

Eugene commented on Aug 22, 2019

up :-/

KoolReport commented on Aug 22, 2019

We have sent the case to dev.team so no worry we will answer you soon.

David Winterburn commented on Aug 23, 2019

Hi Eugene,

Would you please let us know specifically which css tyle for which part of the page doesn't work in pdf cloud export? Thanks!

Eugene commented on Aug 23, 2019

Honestly, all CSS do not work All background-color for example...

David Winterburn commented on Aug 23, 2019

Hi Eugene,

By default, pdf cloud export uses print css media which modifies background color when printing. A direct inline style like "cssStyle" => "background-color: #b1dfbb;" won't work because it doesn't and can't set @media inline. Thus, for your background color to work, please set css class for your table's column, row or cell and add the following css rules to the page:

<style>
    @media  print {
      * {
        -webkit-print-color-adjust: exact !important;
      }   
    
      #table1 td.column-1 {
        background-color: #b1dfbb !important;
      }

    }
</style> 

Let us know if this background-color rules work for you and if there's any css issue left when cloud exporting. Thanks!

Eugene commented on Aug 23, 2019

What do I need to use for column names?

David Winterburn commented on Aug 23, 2019

To set class name for Table's element please use the cssClass property with function:

            "cssClass" => array(
                "td" => function ($row, $columnName) {
                    $cssClasses = "some-name"; //based on $columnName and $row
                    return $cssClasses;
                },
                "th" =>  function ($columnName) {
                    $cssClasses = "some-name"; //based on $columnName
                    return $cssClasses;
                },
                "tf" =>  function ($columnName) {
                    $cssClasses = "some-name"; //based on $columnName
                    return $cssClasses;
                }
            )
Eugene commented on Aug 23, 2019

and after that I do like this

<style>
    @media  print {
      * {
        -webkit-print-color-adjust: exact !important;
      }   
    
     #table1 td.some-name {
        background-color: #b1dfbb !important;
      }

    }
</style> 

correct?

David Winterburn commented on Aug 23, 2019

Yes, you are correct. Your table's name should be "table1", too.

Eugene commented on Aug 23, 2019

ok. Thank you. I will try and let you know...

Eugene commented on Aug 23, 2019

Sorry I try to implement your recommendations and I am a bit confused - I don't have any table name...

Could you show the example based on my code above

KoolReport commented on Aug 23, 2019

You can name it:

Table::create(array(
    "name"=>"table1",
    ...
));
David Winterburn commented on Aug 23, 2019

Here's an example of Table's name:

Table::create(array(
    "name" => "table1",
    ...
Eugene commented on Aug 23, 2019

ok. hope it finally helps...

Eugene commented on Aug 23, 2019

Wow, it works now... Dear David it took a long time but finally, I get what I need and I am really happy. Thank you so much.

Is it any way to see what html code is sent to ChromeHeadless.io to be sure that I send the correct code.

David Winterburn commented on Aug 23, 2019

Hi Eugene,

Please look in your server's temporary folder (or a local "tmp" folder if using ->settings(['useLocalTempFolder' => true])).

There should be a folder and a zip file like "5d5fb3b8c8b6f" and "5d5fb3b8c8b6f.zip" which contains export.html, js, css and image files to be sent to cloud export service to convert to pdf.

Eugene commented on Feb 3, 2020

Hi David,

I am sorry that I return to this theme but I was playing with my pdf report formatting and noticed the printed version lose some default table CSS styles. All my custom styles work nice (I did everything as you suggested and the result is fine) but for example, the default group's bottom style disappears. Look what I mean:

This is my screen version:

This is my pdf version:

As you can see the last line lost the gray background color. in the screen version it controls by table.css:

tr.row-group
{
    background:#eee;
}

Because the pdf report has some specific moments with CSS formatting I decided that this CSS style doesn't work with pdf export and added the following to my pdf.view file

<style>
        @media print {
            * {
                -webkit-print-color-adjust: exact !important;
            }
            #directRecipe tr.row-group{
                background-color: #eee !important;

.... many others my CSS styles for pdf report that work fine ....
}

but it didn't help too...

So my question - where is my mistake and how to keep this gray background in pdf report?

PS I checked the export.html file and it looks ok so I am really confused why all my custom styles work but this one does not.

<tr from='0' to='1' class='row-group' ><td colspan="4"class='textAlignRight'><b> <span class='marginRight6'>Cost: 3,122; Overhead: 0%; Total Cost: 3,122</span> </b></td></tr>

the class row-group exists in the export.html file same as the style definition

@media print {
            * {
                -webkit-print-color-adjust: exact !important;
            }
            #directRecipe tr.row-group{
                background-color: #eee !important;
            }

but the style does not apply.

David Winterburn commented on Feb 3, 2020

Hi Eugene,

If you extract the zip folder and open export.html on Chrome, does the row group tr has the gray background?

Eugene commented on Feb 3, 2020

Hi David,

If I open export.html it looks like this:

And it is normal because all my styles are in @media print query so if I delete

@media print {
            * {
                -webkit-print-color-adjust: exact !important;
            } 
         

and keep only styles - the export.html looks ok as it must be in pdf

And this one I have in the pdf report:

If you compare you can notice that only the gray background of the group bottom disappears. Do you have any idea why?

PS just noticed that Category name (ADD-ONs) also lost its white color... It is set by the following code and works for the generated export.html file and does not work in the pdf.

#directRecipe td.top_group {
                background-color: #b6001d !important;
                color: white !important;
            }
...

"grouping" => array(
                "category" => array(
                    "top" => "<td class='top_group'><b>{category} </b></td>",
                ),
...
David Winterburn commented on Feb 4, 2020

Hi Eugene,

Please try the following 2 methods:

  1. Open the file export.html in Chrome and use Chrome print to see do the colors look ok? If it doesn't please try to modify your @media print CSS to make Chrome print looks allright.

  2. Remove @media print from the CSS rules, then open export.html in Chrome to see how the colors look and compare it to your web page.

Thanks!

Eugene commented on Feb 4, 2020

David,

Method 2 I've already done it and it looks fine as I wish - see my previous message.

For me, it means that my CSSs are correct. Am I right?

Method 1 It does NOT look ok and I am confused with your recommendation - if the page with the same styles looks ok on the screen after the removing @media print from CSS - what do you suggest to change if the CSSs are already complete enough for the correct view on the screen - I have no idea.

David Winterburn commented on Feb 4, 2020

Hi Eugene,

I assume you're using Bootstrap CSS. If so, by default Bootstrap has their @media print rule as well. The Bootstrap's one that should conflict with your own rule is this:

.table td,
.table th {
    background-color: #fff !important;
}

So for background color print to work you have 2 ways:

  1. Remove print style rules in your Bootstrap CSS.

  2. Make your @media print rules more specific than Bootstrap's one, such as:

    @media print {
      #Table1 td {
        background-color: lightgray !important;
        -webkit-print-color-adjust: exact !important;
      }
    }

<?php 
    Table::create(array(
        "name" => "Table1",
        ...
    ));

Hope this works for you. Thanks!

Eugene commented on Feb 4, 2020

Thank you, David

Yes, you are right, Bootstrap CSS overwrites my styles.

I made my CSS more specific (a bit different way than you suggested but it works) and now I got the report of my dream :-)))

I also must say that as we've found the reason for the problems was NOT in koolreport or CloudExport functionality!

So special thanks for your time.

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
help needed
solved

CloudExport