<commit_before><?php
namespace Rafwell\Simplegrid\Export;

use Illuminate\View\View;
use Exception;
use Rafwell\Simplegrid\Grid;

class Pdf{
    protected $view;
    protected $pdf;
    protected $config;
    protected $fileName;
    protected $name;
    protected $filePath;
    protected $rows;
    protected $headers;

    public function __construct(Grid $grid){
        $this->config = $grid->simpleGridConfig['export']['pdf']['snappy'];
        
        $rowsPerPageExport = 1000000;
        $totalPagesExport = ceil($grid->totalRows/$rowsPerPageExport); //itens per query
        
        $fieldsNamesAfterQuery = array_flip(collect($grid->fields)->pluck('alias_after_query_executed')->toArray());
				
        for($i = 1; $i<=$totalPagesExport; $i++){				
            $grid->queryBuilder->paginate($rowsPerPageExport, $i);
            $rows = $grid->queryBuilder->performQueryAndGetRows();		

            if($i===1){
                $header = [];
                $rowsHeader = [$rows[0]];
                array_unshift($rowsHeader, $grid->fields);
                foreach($rowsHeader[0] as $field=>$value){
                    $header[] = $grid->fields[$field]['label'];
                }			
            }

            $this->headers = $header;

            if($grid->processLineClosure){
                for($i = 0; $i<count($rows); $i++){	 
                    $rows[$i] = call_user_func($grid->processLineClosure, $rows[$i]);
                }
            }

            foreach($rows as $k=>$row){						
                $row = array_intersect_key($row, $fieldsNamesAfterQuery);
                //Clear html before export
                
                foreach($row as &$column){
                    $column = str_replace("\xA0", ' ', $column);	
                    $column = str_replace('&nbsp;', ' ', $column);							
                    $column = str_replace(['<br>','<br/>'], "\r", $column);		
                    $column = html_entity_decode($column, null, 'UTF-8');					
                }
                                    
                $row = array_map('strip_tags', $row);					
                
                $this->rows[] = $row;				
            }
        }

        //TODO
        //$pdfHeader = view()->make('Simplegrid::export.pdf.header',[])->render();
        $pdfFooter = view()->make('Simplegrid::export.pdf.footer')->render();

        $pdfBody = view()->make('Simplegrid::export.pdf.body', [
            'rows'=>$this->rows,
            'headers'=>$this->headers,<commit_msg>Trim before pdf export<commit_after><?php
namespace Rafwell\Simplegrid\Export;

use Illuminate\View\View;
use Exception;
use Rafwell\Simplegrid\Grid;

class Pdf{
    protected $view;
    protected $pdf;
    protected $config;
    protected $fileName;
    protected $name;
    protected $filePath;
    protected $rows;
    protected $headers;

    public function __construct(Grid $grid){
        $this->config = $grid->simpleGridConfig['export']['pdf']['snappy'];
        
        $rowsPerPageExport = 1000000;
        $totalPagesExport = ceil($grid->totalRows/$rowsPerPageExport); //itens per query
        
        $fieldsNamesAfterQuery = array_flip(collect($grid->fields)->pluck('alias_after_query_executed')->toArray());
				
        for($i = 1; $i<=$totalPagesExport; $i++){				
            $grid->queryBuilder->paginate($rowsPerPageExport, $i);
            $rows = $grid->queryBuilder->performQueryAndGetRows();		

            if($i===1){
                $header = [];
                $rowsHeader = [$rows[0]];
                array_unshift($rowsHeader, $grid->fields);
                foreach($rowsHeader[0] as $field=>$value){
                    $header[] = $grid->fields[$field]['label'];
                }			
            }

            $this->headers = $header;

            if($grid->processLineClosure){
                for($i = 0; $i<count($rows); $i++){	 
                    $rows[$i] = call_user_func($grid->processLineClosure, $rows[$i]);
                }
            }

            foreach($rows as $k=>$row){						
                $row = array_intersect_key($row, $fieldsNamesAfterQuery);
                //Clear html before export
                
                foreach($row as &$column){
                    $column = str_replace("\xA0", ' ', $column);	
                    $column = str_replace('&nbsp;', ' ', $column);							
                    $column = str_replace(['<br>','<br/>'], "\r", $column);		
                    $column = html_entity_decode($column, null, 'UTF-8');					
                }
                                    
                $row = array_map('strip_tags', $row);		
                $row = array_map('trim', $row);			
                
                $this->rows[] = $row;				
            }
        }

        //TODO
        //$pdfHeader = view()->make('Simplegrid::export.pdf.header',[])->render();
        $pdfFooter = view()->make('Simplegrid::export.pdf.footer')->render();

        $pdfBody = view()->make('Simplegrid::export.pdf.body', [
            'rows'=>$this->rows,
            'headers'=>$this->headers,
{'fraction_non_alphanumeric': 0.16191225654141256, 'fraction_numerical': 0.005508557938225457, 'mean_word_length': 1.7451403887688985, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6299914', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1378, 'n_words': 229}