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
| <?php
//1-download fichier à http://www.pdflib.com/download/tet/ (version windows xp 32 bits, TET-4.0p2-MSWin32.msi)
//2-installer sur un ordi, pas obliger d'être le serveur
//3-aller C:\Program Files (x86)\PDFlib\TET 4.0p2 32-bit\bind\php, il y a les versions de php
//4-prendre libtet_php.dll dans le bon répertoire représentant la version de php installé
//5-mettre ce fichier dans le répertoire de php\ext et modifier php.ini pour mettre extension=libtet_php.dll
// il doit y avoir une section TET lorsqu'on fait php phpinfo();
// *** $Id: extractor.php,v 1.11 2010/07/21 19:58:35 rjs Exp $ Simple PDF text extractor based on PDFlib TET ***
$globaloptlist = "searchpath={../data ../../data ../../../resource/cmap}"; //global option list
$docoptlist = ""; // document-specific option list
$pageoptlist = "granularity=page"; // page-specific option list
$separator = "\n"; // separator to emit after each chunk of text. This depends on the application's needs; for granularity=word a space character may be useful
$pageno = 0;
try {
$tet = new TET();
//mettre ici les infos nécessaire
$filepdf = file_get_contents('http://www.irs.gov/pub/irs-pdf/fw4.pdf'); //adresse web du fichier PDF
$pvf_filename = "/pvf/virt.pdf"; //nom du fichier virtuel, peux laisser tel quel
$tet->create_pvf($pvf_filename, $filepdf,"" ); //ici c'est la création dufichier virtuel, donc pas touche
$infilename = $pvf_filename;
$outfilename = "fichier.txt"; //nom du fichier de sortie
if (!$outfp = fopen("$outfilename", "wb" )) {
die("Couldn't open output file '" . $outfilename . "'\n" );
}
$tet->set_option($globaloptlist);
$doc = $tet->open_document($infilename, $docoptlist);
if ($doc == -1) {
die("Error ". $tet->get_errnum() . " in " . $tet->get_apiname()
. "(): " . $tet->get_errmsg() . "\n" );
}
/* get number of pages in the document */
$n_pages = $tet->pcos_get_number($doc, "length:pages" );
/* loop over pages in the document */
for ($pageno = 1; $pageno <= $n_pages; ++$pageno) {
$page = $tet->open_page($doc, $pageno, $pageoptlist);
if ($page == -1) {
print("Error ". $tet->get_errnum() ." in ". $tet->get_apiname()
. "(): " . $tet->get_errmsg() . "\n" );
next; /* try next page */
}
/* Retrieve all text fragments; This is actually not required
* for granularity=page, but must be used for other granularities.
*/
while (($text = $tet->get_text($page)) != "" ) {
fwrite($outfp, $text); /* print the retrieved text */
/* print a separator between chunks of text */
fwrite($outfp, $separator);
}
if ($tet->get_errnum() != 0) {
print("Error ". $tet->get_errnum() . " in " .
$tet->get_apiname() . "(): on page $pageno"
. $tet->get_errmsg() . "\n" );
}
$tet->close_page($page);
}
fclose($outfp);
$tet->close_document($doc);
}
catch (TETException $e) {
if ($pageno == 0) {
die("TET exception occurred in extractor sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n" );
} else {
die("TET exception occurred in extractor sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
"on page $pageno: " . $e->get_errmsg() . "\n" );
}
}
catch (Exception $e) {
die($e);
}
$tet = 0;
?> |
Partager