HwapX

Untitled

Sep 10th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. <?php
  2.  
  3. function pdf2Json($pdfFilePath) {
  4.     $output = \shell_exec('pdf2json -noframes -q -enc UTF-8 '.\escapeshellarg($pdfFilePath).' /dev/stdout');
  5.  
  6.     if(empty($output))
  7.         throw new \RuntimeException('Failed to run pdf2json binary on pdf file at '.$pdfFilePath."\n".\implode("\n", $output));
  8.  
  9.     return json_decode($output, true)[0]['text'];
  10. }
  11.  
  12. function buildPhrases($items) {
  13.     $phrases = [];
  14.    
  15.     foreach($items as $item) {
  16.         $item['right'] = $item['left'] + $item['width'];
  17.        
  18.         $found = false;
  19.         foreach($phrases as &$p) {
  20.             if($p['top'] == $item['top'] &&
  21.                 ($p['right'] >= $item['left'] - 1 && $p['right'] <= $item['left'] + 1)) {
  22.                 $p['data'] .= ($item['data']);
  23.                 $p['right'] = $item['right'];
  24.                 $p['width'] = $item['right'] - $p['left'];
  25.                 $found = true;
  26.             }
  27.         }
  28.        
  29.         if($found)
  30.             continue;
  31.  
  32.         $phrases[] = $item;
  33.     }
  34.    
  35.     return $phrases;
  36. }
  37.  
  38. function toMatrix($items) {
  39.     $out = [];
  40.    
  41.     foreach($items as $item) {
  42.         $out[$item['left']][$item['top']] = $item;
  43.     }
  44.    
  45.     return $out;
  46. }
  47.  
  48. function dataFromPdf($filePath) {
  49.     $itemsRaw = pdf2Json($filePath);
  50.     $items = buildPhrases($itemsRaw);
  51.     $itemsRaw = toMatrix($itemsRaw);
  52.     $items = toMatrix($items);
  53.    
  54.     return [
  55.         'proprietario'  => $items[66][100]['data'],
  56.         'orgao_atuador' => $items[66][130]['data'],
  57.         'local'         => $items[66][161]['data'],
  58.         'infracao'      => $items[66][191]['data'],
  59.         'natureza'      => $items[66][252]['data'],
  60.        
  61.         'velocidade_permitida'      => $items[66][222]['data'],
  62.         'velocidade_aferida'      => $items[232][222]['data'],
  63.         'velocidade_considerada'      => $items[398][222]['data'],
  64.         'instrumento'      => $items[563][222]['data'],
  65.         'placa'      => $items[66][283]['data'],
  66.         'marca_modelo'      => $items[398][283]['data'],
  67.         'condutor'      => @$items[66][313]['data'],
  68.        
  69.         'auto_infracao' => $items[232][130]['data'],
  70.        
  71.         'agente_atuador' => $items[398][130]['data'],
  72.        
  73.         'data' => $items[563][130]['data'],
  74.        
  75.         'emissao' => $items[716][65]['data'],
  76.         'renainf' => $items[716][100]['data'],
  77.         'hora'    => $items[716][130]['data'],
  78.         'pontos'  => $items[716][252]['data'],
  79.         'data_apresentacao' => $itemsRaw[751][356]['data'],
  80.     ];
  81. }
  82.  
  83. $data = dataFromPdf('pdf/grm-3.pdf');
  84. print_r($data);
  85.  
Advertisement
Add Comment
Please, Sign In to add comment