KoolReport's Forum

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

Background color on DataGrid's DataTables does not work when exported to PDF #1851

Closed Daniel opened this topic on on Jan 19, 2021 - 7 comments

Daniel commented on Jan 19, 2021

Hi again,

It seems that I've noticed another bug. CSS styling has no effect when the table is exported to PDF. Since it works fine when exported to JPG, I'm guessing the problem lies in export package itself. Here's the code:

//ReportController.php
    $myReport = new MyReport($reportParams);
    $result = $myReport->run();
    if ($request->format === 'pdf') {
      $result->export()->pdf(array(
        "format"=>"A4",
        "orientation"=>$report->visualization === 'table' || $report->visualization === 'pie' || $report->visualization === 'donut' ? 'portrait' : 'landscape',
        "zoom"=>$report->visualization === 'table' ? '1' : '0.7',
      ))->toBrowser($report->title . ".pdf");
    }
    elseif ($request->format === 'jpg') {   
      $result->export()->jpg(array(
        "width"=>"1024px",
        "height"=>"768px",
      ))->toBrowser($report->title . ".jpg");
    }
//MyReport.view.php
         if (!empty($this->params['colors'])) {
          $cssClass = 
          [
            'table' => 'tableColor',
            'td' => 'tableColor'
          ];
          $columns = [];
          foreach ($this->params['columns'] as $column) {
            $columns[$column] = ['className' => 'tableColor'];
          }
        }
        else {
          $columns = $this->params['columns'];
        }
        Datatables::create([
          'name' => 'uniqueTable',
          'Title' => $this->params['title'],
          'dataSource' => $dataSource,
          'columns' => $columns,
          'options' => 
          [
            'searching' => $searching, 
            'colReorder' => true, 
            'paging' => $paging
          ],
          'serverSide' => $serverSide,
          'cssClass' => $cssClass
        ]);

Is there any workaround?

David Winterburn commented on Jan 19, 2021

Which css rule doesn't work when you export to pdf? If it's background color, it's disabled by default when printing to pdf and printing in general to avoid ink depletion. If it's the case please adding the following css rule to your pdf view:

<style>
    @media print {

        * {
            -webkit-print-color-adjust: exact !important;
        }
        
    }
</style>
Daniel commented on Jan 19, 2021

Yeah it's background color. It still doesn't work. As for being disabled to save ink, if I use other type of chart such as bar chart and convert it to pdf, the style works fine.

     <style type="text/css">
      <?php if ($this->params['visualization'] === 'table' && !empty($this->params['colors'])): ?>
        .tableColor
        {
            background-color:<?php echo $this->params['colors'][0] ?>;
        }

        @media print {

          * {
              -webkit-print-color-adjust: exact !important;
          }
            
        }
      <?php endif; ?>
    </style>
David Winterburn commented on Jan 19, 2021

Please try add the rule both inside and outside of @media print:

        .tableColor
        {
            background-color:<?php echo $this->params['colors'][0] ?>;
        }
          * {
              -webkit-print-color-adjust: exact !important;
          }

        @media print {

        .tableColor
        {
            background-color:<?php echo $this->params['colors'][0] ?>;
        }

          * {
              -webkit-print-color-adjust: exact !important;
          }
            
        } 
Daniel commented on Jan 20, 2021

Still doesn't work.

    <style type="text/css">
      <?php if ($this->params['visualization'] === 'table' && !empty($this->params['colors'])): ?>
        .tableColor
        {
          background-color:<?php echo $this->params['colors'][0] ?>;
        }

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

        @media print 
        {
          .tableColor
          {
            background-color:<?php echo $this->params['colors'][0] ?>;
          }

          * {
            -webkit-print-color-adjust: exact !important;
          }
        }
      <?php endif; ?>
    </style>
David Winterburn commented on Jan 20, 2021

Please use the following css selector to see how it works:

          #uniqueTable td.tableColor
          {
            background-color:<?php echo $this->params['colors'][0] ?>;
          }
Daniel commented on Jan 20, 2021

Still not working.

<style type="text/css">
  <?php if ($this->params['visualization'] === 'table' && !empty($this->params['colors'])): ?>
    #uniqueTable td.tableColor
    {
      background-color:<?php echo $this->params['colors'][0] ?>;
    }

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

    @media print 
    {
      #uniqueTable td.tableColor
      {
        background-color:<?php echo $this->params['colors'][0] ?>;
      }

      * {
        -webkit-print-color-adjust: exact !important;
      }
    }
  <?php endif; ?>
</style>
Daniel commented on Jan 20, 2021

Got it working now. Here's the solution:

Source

    <style type="text/css">
      <?php if ($this->params['visualization'] === 'table' && !empty($this->params['colors'])): ?>
        .tableColor
        {
            background-color:<?php echo $this->params['colors'][0] . '!important' ?>;
        }
      <?php endif; ?>
    </style>

We just need to add !important for background-color attribute. Thanks for your help.

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

Export