Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function pdf2Json($pdfFilePath) {
- $output = \shell_exec('pdf2json -noframes -q -enc UTF-8 '.\escapeshellarg($pdfFilePath).' /dev/stdout');
- if(empty($output))
- throw new \RuntimeException('Failed to run pdf2json binary on pdf file at '.$pdfFilePath."\n".\implode("\n", $output));
- return json_decode($output, true)[0]['text'];
- }
- function buildPhrases($items) {
- $phrases = [];
- foreach($items as $item) {
- $item['right'] = $item['left'] + $item['width'];
- $found = false;
- foreach($phrases as &$p) {
- if($p['top'] == $item['top'] &&
- ($p['right'] >= $item['left'] - 1 && $p['right'] <= $item['left'] + 1)) {
- $p['data'] .= ($item['data']);
- $p['right'] = $item['right'];
- $p['width'] = $item['right'] - $p['left'];
- $found = true;
- }
- }
- if($found)
- continue;
- $phrases[] = $item;
- }
- return $phrases;
- }
- function toMatrix($items) {
- $out = [];
- foreach($items as $item) {
- $out[$item['left']][$item['top']] = $item;
- }
- return $out;
- }
- function dataFromPdf($filePath) {
- $itemsRaw = pdf2Json($filePath);
- $items = buildPhrases($itemsRaw);
- $itemsRaw = toMatrix($itemsRaw);
- $items = toMatrix($items);
- return [
- 'proprietario' => $items[66][100]['data'],
- 'orgao_atuador' => $items[66][130]['data'],
- 'local' => $items[66][161]['data'],
- 'infracao' => $items[66][191]['data'],
- 'natureza' => $items[66][252]['data'],
- 'velocidade_permitida' => $items[66][222]['data'],
- 'velocidade_aferida' => $items[232][222]['data'],
- 'velocidade_considerada' => $items[398][222]['data'],
- 'instrumento' => $items[563][222]['data'],
- 'placa' => $items[66][283]['data'],
- 'marca_modelo' => $items[398][283]['data'],
- 'condutor' => @$items[66][313]['data'],
- 'auto_infracao' => $items[232][130]['data'],
- 'agente_atuador' => $items[398][130]['data'],
- 'data' => $items[563][130]['data'],
- 'emissao' => $items[716][65]['data'],
- 'renainf' => $items[716][100]['data'],
- 'hora' => $items[716][130]['data'],
- 'pontos' => $items[716][252]['data'],
- 'data_apresentacao' => $itemsRaw[751][356]['data'],
- ];
- }
- $data = dataFromPdf('pdf/grm-3.pdf');
- print_r($data);
Advertisement
Add Comment
Please, Sign In to add comment