Visualizzazione dei risultati da 1 a 2 su 2

Discussione: PDF to plain text

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    21

    PDF to plain text

    Avrei bisogno di un aiutino...
    Devo fare una funzione che dato un file in pdf mi restituisca il testo in esso contenuto.
    Naturalmente per i file pdf costituiti da sole immagini questo non è possibile, ma per gli altri penso proprio che si possa fare.

    Ho trovato questo codice su internet:
    codice:
          $test = pdf2string("prog-sys-0304.pdf" );
    
          echo $test;
    
          function pdf2string($sourcefile)
          {
    
            $fp = fopen($sourcefile, 'rb');
    
            $content = fread($fp, filesize($sourcefile));
            fclose($fp);
           
            # Locate all text hidden within the stream and endstream tags
            $searchstart = 'stream';
            $searchend = 'endstream';
            $pdfdocument = "";
           
            $pos = 0;
            $pos2 = 0;
            $startpos = 0;
            # Iterate through each stream block
            while( $pos !== false && $pos2 !== false )
            {
              # Grab beginning and end tag locations if they have not yet been parsed
              $pos = strpos($content, $searchstart, $startpos);
              $pos2 = strpos($content, $searchend, $startpos + 1);
              if( $pos !== false && $pos2 !== false )
              {
                  # Extract compressed text from between stream tags and uncompress
                  $textsection = substr($content, $pos + strlen($searchstart) + 2, $pos2 - $pos - strlen($searchstart) - 1);
                  $data = @gzuncompress($textsection);
                  # Clean up text via a special function
                  $data = ExtractText($data);
                  # Increase our PDF pointer past the section we just read
                  $startpos = $pos2 + strlen($searchend) - 1;
                  if( $data === false ) { return -1; }
                 $pdfdocument = $pdfdocument . $data;
              }
            }
           
           return $pdfdocument;
          }
           
          function ExtractText($postScriptData)
          {
            while( (($textStart = strpos($postScriptData, '(', $textStart)) && ($textEnd = strpos($postScriptData, ')', $textStart + 1)) && substr($postScriptData, $textEnd - 1) != '\\') )
            {
              $plainText .= substr($postScriptData, $textStart + 1, $textEnd - $textStart - 1);
              if( substr($postScriptData, $textEnd + 1, 1) == ']' ) // This adds quite some additional spaces between the words
              {
                  $plainText .= ' ';
              }
           
              $textStart = $textStart < $textEnd ? $textEnd : $textStart + 1;
            }
               // Translate special characters and put back brackets.
            $trans = array(
                '...'                => '&hellip;',
                '\205'                => '&hellip;',
                '\221'                => chr(145),
                '\222'                => chr(146),
                '\223'                => chr(147),
                '\224'                => chr(148),
                '\226'                => '-',
                '\267'                => '&bull;',
                '\222'                => "'",
                '\351'                => '&eacute;',
                '\350'                => '&egrave;',
                '\347'                => '&ccedil;',
                '\253'                => '&quot;',
                '\273'                => '&quot;',      
                '\('                => '(',
                '\['                => '[',
                '##ENDBRACKET##'    => ')',
                '##ENDSBRACKET##'    => ']',
                chr(133)            => '-',
                chr(141)            => chr(147),
                chr(142)            => chr(148),
                chr(143)            => chr(145),
               chr(144)            => chr(146),
            );
            $plainText = strtr($plainText, $trans);
           
           
            return stripslashes($plainText);
          }
    A quanto ho potuto notare, il problema sta nella decompressione del file.

    $data = @gzuncompress($textsection);

    mi restitusce sempre una stringa vuota.
    Ho provato anche a abilitare l'estensione php_zlib.dll ma il risultato non cambia.

    Se qualcuno mi potesse aiutare gliene sarei grato. Grazie!

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    21
    Nessuno sa aiutarmi?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.