IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Extraire un texte d'un PDF > renommer le fichier avec ce texte


Sujet :

Langage PHP

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 5
    Points : 3
    Points
    3
    Par défaut Extraire un texte d'un PDF > renommer le fichier avec ce texte
    Bonjour,

    je suis nouveau sur le forum et souhaite faire exactement la meme chose que dans cette discussion : http://www.developpez.net/forums/d72...e-fichier-pdf/

    Pour être précis, je souhaite trouver un script qui puisse extraire un code chiffré d'un PDF pour le renommer avec (entre autres) ce code isin.

    Exemple :
    le fichier "Reporting blablabla.pdf" contient le texte FR008129928 et je souhaite qu'il s'appelle après coup : 30122009_FR008129928_reporting.pdf

  2. #2
    Membre expert
    Avatar de s.n.a.f.u
    Homme Profil pro
    Développeur Web
    Inscrit en
    Août 2006
    Messages
    2 760
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Août 2006
    Messages : 2 760
    Points : 3 545
    Points
    3 545
    Par défaut
    Il existe un soft qui s'appelle XPDF qui peut extraire du texte d'un pdf.

    J'ai aussi trouvé ça, mais pas testé: http://community.livejournal.com/php/295413.html
    • Avant de poser une question, n'hésitez pas à chercher dans la FAQ et les forums
    • Merci d'utiliser les balises de code (# dans l'éditeur)
    • N'oubliez pas de vous servir des boutons , et

    S.N.A.F.U

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Bonjour snafu,

    merci de ton aide,
    serais tu assez aimable pour copier dans le post l'information qui est sur la page que tu as mise en lien, car cette page est bloquée par le firewall de la boite.

    merci a toi

  4. #4
    Membre expert Avatar de Fench
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Mai 2002
    Messages
    2 353
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Groenland

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 353
    Points : 3 390
    Points
    3 390
    Par défaut
    Le lien marche bien

    Sinon de ce fait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
     
    <?php
     
    // new pdf extract
     
    print pdf2txt("test.pdf");
     
    // Function    : pdf2txt()
    // Arguments   : $filename - Filename of the PDF you want to extract
    // Description : Reads a pdf file, extracts data streams, and manages
    //               their translation to plain text - returning the plain
    //               text at the end
    // Author      : Jonathan Beckett, 2005-05-02
    function pdf2txt($filename){
     
    	$data = getFileData($filename);
     
    	// grab objects and then grab their contents (chunks)
    	$a_obj = getDataArray($data,"obj","endobj");
    	foreach($a_obj as $obj){
     
    		$a_filter = getDataArray($obj,"<<",">>");
    		if (is_array($a_filter)){
    			$j++;
    			$a_chunks[$j]["filter"] = $a_filter[0];
     
    			$a_data = getDataArray($obj,"stream\r\n","endstream");
    			if (is_array($a_data)){
    				$a_chunks[$j]["data"] = substr($a_data[0],strlen("stream\r\n"),strlen($a_data[0])-strlen("stream\r\n")-strlen("endstream"));
    			}
    		}
    	}
     
    	// decode the chunks
    	foreach($a_chunks as $chunk){
     
    		// look at each chunk and decide how to decode it - by looking at the contents of the filter
    		$a_filter = split("/",$chunk["filter"]);
     
    		if ($chunk["data"]!=""){
    			// look at the filter to find out which encoding has been used			
    			if (substr($chunk["filter"],"FlateDecode")!==false){
    				$data =@ gzuncompress($chunk["data"]);
    				if (trim($data)!=""){
    					$result_data .= ps2txt($data);
    				} else {
     
    					//$result_data .= "x";
    				}
    			}
    		}
    	}
     
    	return $result_data;
     
    }
     
     
    // Function    : ps2txt()
    // Arguments   : $ps_data - postscript data you want to convert to plain text
    // Description : Does a very basic parse of postscript data to
    //               return the plain text
    // Author      : Jonathan Beckett, 2005-05-02
    function ps2txt($ps_data){
    	$result = "";
    	$a_data = getDataArray($ps_data,"[","]");
    	if (is_array($a_data)){
    		foreach ($a_data as $ps_text){
    			$a_text = getDataArray($ps_text,"(",")");
    			if (is_array($a_text)){
    				foreach ($a_text as $text){
    					$result .= substr($text,1,strlen($text)-2);
    				}
    			}
    		}
    	} else {
    		// the data may just be in raw format (outside of [] tags)
    		$a_text = getDataArray($ps_data,"(",")");
    		if (is_array($a_text)){
    			foreach ($a_text as $text){
    				$result .= substr($text,1,strlen($text)-2);
    			}
    		}
    	}
    	return $result;
    }
     
     
    // Function    : getFileData()
    // Arguments   : $filename - filename you want to load
    // Description : Reads data from a file into a variable
    //               and passes that data back
    // Author      : Jonathan Beckett, 2005-05-02
    function getFileData($filename){
    	$handle = fopen($filename,"rb");
    	$data = fread($handle, filesize($filename));
    	fclose($handle);
    	return $data;
    }
     
     
    // Function    : getDataArray()
    // Arguments   : $data       - data you want to chop up
    //               $start_word - delimiting characters at start of each chunk
    //               $end_word   - delimiting characters at end of each chunk
    // Description : Loop through an array of data and put all chunks
    //               between start_word and end_word in an array
    // Author      : Jonathan Beckett, 2005-05-02
    function getDataArray($data,$start_word,$end_word){
     
    	$start = 0;
    	$end = 0;
    	unset($a_result);
     
    	while ($start!==false && $end!==false){
    		$start = strpos($data,$start_word,$end);
    		if ($start!==false){
    			$end = strpos($data,$end_word,$start);
    			if ($end!==false){
    				// data is between start and end
    				$a_result[] = substr($data,$start,$end-$start+strlen($end_word));
    			}
    		}
    	}
    	return $a_result;
    }
     
    ?>
    Meuuh en AI à l'INRA
    Domaines: {java, php, js, jquery}{hibernate, doctrine}{MyLib, symfony, Zend}
    fait gagner du temps à ceux qui aident , donc un message avec la balise résolu laisse plus de temps pour résoudre d'autres problèmes (balise à cliquer en bas de l'écran)

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Bonjour Fench,

    merci pour ton aide,

    Pardon pour la question de débutant en php, mais au vu du script,

    je vois a peu près comment faire pour extraire la donnée..

    mais

    je ne vois pas où je peux intervenir sur le fait de renommer le fichier pdf, avec cette donnée extraite... (Je pense que c'est cette partie la plus difficile)

  6. #6
    Membre expert Avatar de Fench
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Mai 2002
    Messages
    2 353
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Groenland

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 353
    Points : 3 390
    Points
    3 390
    Par défaut
    Tu as changé le Pdf en Txt,
    Tu as récupéré ta donnée,

    Reste plus qu'a renommer le fichier par les fonctions standarts non?
    Attention aux droits sur les fichiers (voir chmod)
    Meuuh en AI à l'INRA
    Domaines: {java, php, js, jquery}{hibernate, doctrine}{MyLib, symfony, Zend}
    fait gagner du temps à ceux qui aident , donc un message avec la balise résolu laisse plus de temps pour résoudre d'autres problèmes (balise à cliquer en bas de l'écran)

  7. #7
    Expert confirmé
    Avatar de Thes32
    Homme Profil pro
    Développeur PHP, .Net, T-SQL
    Inscrit en
    Décembre 2006
    Messages
    2 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Développeur PHP, .Net, T-SQL

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 379
    Points : 4 853
    Points
    4 853
    Par défaut
    Citation Envoyé par Fench Voir le message
    Tu as changé le Pdf en Txt,
    Reste plus qu'a renommer le fichier par les fonctions standarts non?
    Attention aux droits sur les fichiers (voir chmod)
    Tu as la fonction rename
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    if(rename('ancien_nom.txt','nouveau_nom.txt'))
      echo 'Le Fichier a été renommer avec succès';
    Développeur | Zend Certified Engineer

    Étapes Pour mieux se servir du forum:
    1. Commencez par lire les cours et tutoriels ;
    2. Faites une recherche;
    3. Faites un post si rien trouvé dans les deux étapes précédentes en respectant les règles;

    Nix>_Rien n'est plus pratique que la théorie

  8. #8
    Membre du Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Décembre 2010
    Messages
    118
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Décembre 2010
    Messages : 118
    Points : 54
    Points
    54
    Par défaut
    Citation Envoyé par Fench Voir le message
    Le lien marche bien

    Sinon de ce fait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
     
    <?php
     
    // new pdf extract
     
    print pdf2txt("test.pdf");
     
    // Function    : pdf2txt()
    // Arguments   : $filename - Filename of the PDF you want to extract
    // Description : Reads a pdf file, extracts data streams, and manages
    //               their translation to plain text - returning the plain
    //               text at the end
    // Author      : Jonathan Beckett, 2005-05-02
    function pdf2txt($filename){
     
    	$data = getFileData($filename);
     
    	// grab objects and then grab their contents (chunks)
    	$a_obj = getDataArray($data,"obj","endobj");
    	foreach($a_obj as $obj){
     
    		$a_filter = getDataArray($obj,"<<",">>");
    		if (is_array($a_filter)){
    			$j++;
    			$a_chunks[$j]["filter"] = $a_filter[0];
     
    			$a_data = getDataArray($obj,"stream\r\n","endstream");
    			if (is_array($a_data)){
    				$a_chunks[$j]["data"] = substr($a_data[0],strlen("stream\r\n"),strlen($a_data[0])-strlen("stream\r\n")-strlen("endstream"));
    			}
    		}
    	}
     
    	// decode the chunks
    	foreach($a_chunks as $chunk){
     
    		// look at each chunk and decide how to decode it - by looking at the contents of the filter
    		$a_filter = split("/",$chunk["filter"]);
     
    		if ($chunk["data"]!=""){
    			// look at the filter to find out which encoding has been used			
    			if (substr($chunk["filter"],"FlateDecode")!==false){
    				$data =@ gzuncompress($chunk["data"]);
    				if (trim($data)!=""){
    					$result_data .= ps2txt($data);
    				} else {
     
    					//$result_data .= "x";
    				}
    			}
    		}
    	}
     
    	return $result_data;
     
    }
     
     
    // Function    : ps2txt()
    // Arguments   : $ps_data - postscript data you want to convert to plain text
    // Description : Does a very basic parse of postscript data to
    //               return the plain text
    // Author      : Jonathan Beckett, 2005-05-02
    function ps2txt($ps_data){
    	$result = "";
    	$a_data = getDataArray($ps_data,"[","]");
    	if (is_array($a_data)){
    		foreach ($a_data as $ps_text){
    			$a_text = getDataArray($ps_text,"(",")");
    			if (is_array($a_text)){
    				foreach ($a_text as $text){
    					$result .= substr($text,1,strlen($text)-2);
    				}
    			}
    		}
    	} else {
    		// the data may just be in raw format (outside of [] tags)
    		$a_text = getDataArray($ps_data,"(",")");
    		if (is_array($a_text)){
    			foreach ($a_text as $text){
    				$result .= substr($text,1,strlen($text)-2);
    			}
    		}
    	}
    	return $result;
    }
     
     
    // Function    : getFileData()
    // Arguments   : $filename - filename you want to load
    // Description : Reads data from a file into a variable
    //               and passes that data back
    // Author      : Jonathan Beckett, 2005-05-02
    function getFileData($filename){
    	$handle = fopen($filename,"rb");
    	$data = fread($handle, filesize($filename));
    	fclose($handle);
    	return $data;
    }
     
     
    // Function    : getDataArray()
    // Arguments   : $data       - data you want to chop up
    //               $start_word - delimiting characters at start of each chunk
    //               $end_word   - delimiting characters at end of each chunk
    // Description : Loop through an array of data and put all chunks
    //               between start_word and end_word in an array
    // Author      : Jonathan Beckett, 2005-05-02
    function getDataArray($data,$start_word,$end_word){
     
    	$start = 0;
    	$end = 0;
    	unset($a_result);
     
    	while ($start!==false && $end!==false){
    		$start = strpos($data,$start_word,$end);
    		if ($start!==false){
    			$end = strpos($data,$end_word,$start);
    			if ($end!==false){
    				// data is between start and end
    				$a_result[] = substr($data,$start,$end-$start+strlen($end_word));
    			}
    		}
    	}
    	return $a_result;
    }
     
    ?>
    Après l’exécution de cette script voici les messages d'erreur qui s'affiche !
    Notice: Undefined variable: j in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 260

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined variable: a_result in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 361

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined index: data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 276

    Notice: Undefined variable: result_data in C:\xampp\htdocs\txtmeaning\core\function_tronquer.php on line 290

Discussions similaires

  1. Renommer un fichier avec VBScript
    Par MartinezGarcia dans le forum VBScript
    Réponses: 4
    Dernier message: 17/03/2008, 11h15
  2. renommer un fichier avec la valeur d'une cellule
    Par 241P17 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 01/03/2008, 22h55
  3. Réponses: 11
    Dernier message: 19/11/2006, 12h45
  4. [batch] renommer un fichier avec la date
    Par arcane dans le forum Windows
    Réponses: 3
    Dernier message: 13/05/2005, 14h32
  5. Renommer un fichier avec SmartUpload
    Par PrinceMaster77 dans le forum ASP
    Réponses: 2
    Dernier message: 21/12/2004, 15h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo