在做一個OA系統項目中,開發到傳真模塊,傳真數據是通過aofax收發的,現在要把收到的tif文檔顯示到浏覽器上。最好的辦法是把tif文檔轉換成pdf的格式。
步驟如下:
1、運行以下五條代碼
sudo aptitude update
sudo aptitude install make php5-cli php5-gd php5-dev php-pear gs-common ghostscript
sudo aptitude remove php5-imagick
sudo apt-get install libmagick9-dev
sudo pecl install imagick //如果只安裝這個會出問題就把上面四個都安裝了
2、在/etc/php5/apache/php.ini中加入extension=imagick.so擴展
3、重啟apache,/etc/init.d/apache restart
以上配置完後測試
調用這個函數
- private function tif_to_pdf($file_tif,$file_pdf){
- $errors = array();
- $cmd_ps2pdf = "/usr/bin/ps2pdfwr";
- // $file_tif = escapeshellarg($file_tif);//escapeshellarg函數用於過濾shell參數
- // $file_pdf = escapeshellarg($file_pdf);
- if (!file_exists($file_tif)) $errors[] = "Original TIFF file: ".$file_tif." does not exist";
- if (!file_exists($cmd_ps2pdf)) $errors[] = "Ghostscript PostScript to PDF converter not found at: ".$cmd_ps2pdf;
- if (!extension_loaded("imagick")) $errors[] = "Imagick extension not installed or not loaded";
- if (!count($errors)) {
- // 確認文件的基本路徑
- $base = $file_pdf;
- if(($ext = strrchr($file_pdf, '.')) !== false) $base = substr($file_pdf, 0, -strlen($ext));
- // Determine the temporary .ps filepath
- $file_ps = $base.".ps";
- // 打開原始的.tiff文件
- $document = new Imagick($file_tif);
- // Use Imagick to write multiple pages to 1 .ps file
- if (!$document->writeImages($file_ps, true)) {
- $errors[] = "Unable to use Imagick to write multiple pages to 1 .ps file: ".$file_ps;
- } else {
- $document->clear();
- // Use ghostscript to convert .ps -> .pdf
- exec($cmd_ps2pdf." -sPAPERSIZE=a4 ".$file_ps." ".$file_pdf, $o, $r);
- if ($r) {
- $errors[] = "Unable to use ghostscript to convert .ps(".$file_ps.") -> .pdf(".$file_pdf."). Check rights. ";
- }
- }
- }
-
- // return array with errors, or true with success.
- if (!count($errors)) {
- return true;
- } else {
- return $errors;
- }
- }