From 13c5f563e4ca807efbf39ff870661126e11ad242 Mon Sep 17 00:00:00 2001 From: User Date: Sun, 24 Jan 2010 02:56:42 +0900 Subject: [PATCH 1/1] [add] initialize --- lib/Converter/Factory.class.php | 28 +++++++++++++++ lib/Converter/Pdf.class.php | 71 +++++++++++++++++++++++++++++++++++++++ lib/Converter/Template.class.php | 34 +++++++++++++++++++ lib/Converter/require.php | 23 +++++++++++++ test/Converter/data/test.pdf | Bin 0 -> 917 bytes test/Converter/pdf_test.php | 21 ++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 lib/Converter/Factory.class.php create mode 100644 lib/Converter/Pdf.class.php create mode 100644 lib/Converter/Template.class.php create mode 100644 lib/Converter/require.php create mode 100644 test/Converter/data/test.pdf create mode 100644 test/Converter/pdf_test.php diff --git a/lib/Converter/Factory.class.php b/lib/Converter/Factory.class.php new file mode 100644 index 0000000..6780b94 --- /dev/null +++ b/lib/Converter/Factory.class.php @@ -0,0 +1,28 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id$ + +class Converter_Factory +{ + static function create($extension) + { + if ($extension === 'pdf') return new Converter_Pdf(); + throw new Exception("Converterer not found. [extension = $extension]"); + } +} diff --git a/lib/Converter/Pdf.class.php b/lib/Converter/Pdf.class.php new file mode 100644 index 0000000..93518d0 --- /dev/null +++ b/lib/Converter/Pdf.class.php @@ -0,0 +1,71 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id$ + +class Converter_Pdf extends Converter_Template +{ + protected $inputFile; + protected $outputFile; + + function __construct() + { + $this->inputFile = tempnam('/tmp', 'convert_pdf_input_'); + $this->outputFile = tempnam('/tmp', 'convert_pdf_output_'); + } + + function __destruct() + { + @unlink($this->inputFile); + @unlink($this->outputFile); + } + + function toText($string) + { + return $this->convert($string); + } + + function toHtml($string) + { + return $this->convert($string, true); + } + + protected function isInstalledUseCommand() + { + $check_command = 'which pdftotext 1>/dev/null 2>/dev/null'; + exec($check_command, $output, $status); + if ($status) return false; + return true; + } + + protected function convert($string, $to_html = false) + { + file_put_contents($this->inputFile, $string); + + $option = ''; + if ($to_html) { + $option = '-htmlmeta'; + } + $command = "pdftotext {$option} -enc UTF-8 -nopgbrk -eol unix {$this->inputFile} {$this->outputFile} 2>&1"; + + exec($command, $output, $status); + if ($status) throw new Exception("command error.[$command]\nstatus={$status}\noutput=".implode($output)); + + return file_get_contents($this->outputFile); + } +} diff --git a/lib/Converter/Template.class.php b/lib/Converter/Template.class.php new file mode 100644 index 0000000..4f55b53 --- /dev/null +++ b/lib/Converter/Template.class.php @@ -0,0 +1,34 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id$ + +abstract class Converter_Template +{ + abstract function toText($string); + abstract function toHtml($string); + abstract protected function isInstalledUseCommand(); + + function __construct() + { + // コンバートかける度にチェックするのでオーバーヘッドになる。 + // コマンドが実行できなくなる可能性は低いのでチェックを飛ばしても + // 運用上問題ないかもしれない。 + if (!$this->isInstalledUseCommand()) throw new Exception('essential command is not installed.'); + } +} diff --git a/lib/Converter/require.php b/lib/Converter/require.php new file mode 100644 index 0000000..69a4b25 --- /dev/null +++ b/lib/Converter/require.php @@ -0,0 +1,23 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once dirname(__FILE__) . '/Template.class.php'; +require_once dirname(__FILE__) . '/Pdf.class.php'; +require_once dirname(__FILE__) . '/Factory.class.php'; diff --git a/test/Converter/data/test.pdf b/test/Converter/data/test.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d097335982d205735c405b13605747b3b6891a6b GIT binary patch literal 917 zcmZWoL2DC1811RZfY3wn{Phs1U^Ba$>?VOyk~UotwZ=_)N(xT1lXPXXQ)VU*#e<;r zTD`|hLGk3>U!Wp_2f=^PlP8bj!EZL(Y)R*`yYHL#zW2SEEwq~chFx?>;q&j)Gg1Ky z#NlJIxk>NwWT=mzibz2HDArs8^<$=allMi)x3-AVlCu$+OiMpX9c!tv{&xCp|I7Y| zN7rt=c=+`b*<0)VZM-M`GFgjyEAL*BbDx|q*WdJt*H3>=r_WCQ{5zYjet&(d_-o~) z{;^(MdM;OA@+8cJ@y(bq7R<9!BN7uu@R?%n+uh5p)@zcZF1F@(XkdXC8eB8 zayi+_$rxNX?&ED)Z^SGNqXcdr38iO3;1waV&Q+}J;w4zv=hzth0%qA5?gD0eSNq!4Oa8QEQOqU&LO|Qm mQ;xF%4TQi`#*6}>canhslUI6Slx9+=DR^az6bic!eDWWN9|Exe literal 0 HcmV?d00001 diff --git a/test/Converter/pdf_test.php b/test/Converter/pdf_test.php new file mode 100644 index 0000000..0e36603 --- /dev/null +++ b/test/Converter/pdf_test.php @@ -0,0 +1,21 @@ +toText($data); + $html = $converter->toHtml($data); + + var_dump($string); + var_dump($html); + +} catch (Exception $e) { + echo $e->getMessage() . "\n"; + exit(1); +} + -- 2.11.0