Ubuntu中实现tif格式转换为pdf或者其他各种格式的方法


在做一个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

以上配置完后测试

调用这个函数

  1. private function tif_to_pdf($file_tif,$file_pdf){  
  2.         $errors     = array();  
  3.         $cmd_ps2pdf = "/usr/bin/ps2pdfwr";  
  4.       //  $file_tif   = escapeshellarg($file_tif);//escapeshellarg函数用于过滤shell参数   
  5.       //  $file_pdf   = escapeshellarg($file_pdf);   
  6.         if (!file_exists($file_tif)) $errors[] = "Original TIFF file: ".$file_tif." does not exist";  
  7.         if (!file_exists($cmd_ps2pdf)) $errors[] = "Ghostscript PostScript to PDF converter not found at: ".$cmd_ps2pdf;  
  8.         if (!extension_loaded("imagick")) $errors[] = "Imagick extension not installed or not loaded";  
  9.         if (!count($errors)) {  
  10.             // 确认文件的基本路径   
  11.             $base = $file_pdf;  
  12.             if(($ext = strrchr($file_pdf'.')) !== false) $base = substr($file_pdf, 0, -strlen($ext));  
  13.             // Determine the temporary .ps filepath   
  14.             $file_ps = $base.".ps";  
  15.             // 打开原始的.tiff文件   
  16.             $document = new Imagick($file_tif);  
  17.             // Use Imagick to write multiple pages to 1 .ps file   
  18.             if (!$document->writeImages($file_ps, true)) {  
  19.                 $errors[] = "Unable to use Imagick to write multiple pages to 1 .ps file: ".$file_ps;  
  20.             } else {  
  21.                 $document->clear();  
  22.                 // Use ghostscript to convert .ps -> .pdf   
  23.                 exec($cmd_ps2pdf." -sPAPERSIZE=a4 ".$file_ps." ".$file_pdf$o$r);  
  24.                 if ($r) {  
  25.                     $errors[] = "Unable to use ghostscript to convert .ps(".$file_ps.") -> .pdf(".$file_pdf."). Check rights. ";  
  26.                 }   
  27.             }  
  28.         }  
  29.           
  30.         // return array with errors, or true with success.   
  31.         if (!count($errors)) {  
  32.             return true;  
  33.         } else {  
  34.             return $errors;  
  35.         }  
  36.     }  

相关内容