Created
March 22, 2012 14:57
-
-
Save s4l1h/2158823 to your computer and use it in GitHub Desktop.
PHP wkhtmltopdf class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Örnek Kullanım | |
| * | |
| $detay="<h1>Test</h1>"; | |
| $dosya="test.pdf"; | |
| $wkPdf= new WkPdf(); | |
| $wkPdf->setWkPath('/usr/bin/wkhtmltopdf'); | |
| $wkPdf->setHtml($detay); | |
| $wkPdf->setFile($dosya); | |
| // Debian İçin : $wkPdf->setCommand('xvfb-run -a -s "-screen 0 640x480x16" -f "/tmp/" %s -q - -'); | |
| $wkPdf->setCommand('%s -q - -'); | |
| $wkPdf->init(); | |
| * | |
| * | |
| * Tarayıcıya Çıktı Gönderir | |
| $wkPdf->getPdf(); | |
| * | |
| * Dosyayı Kaydeder | |
| $wkPdf->setFilePath('/var/www/ff.com/public_html/pdf/'); | |
| $wkPdf->saveFile(); /var/www/ff.com/public_html/pdf/test.pdf şeklinde Kaydeder. | |
| * | |
| */ | |
| class WkPdfException extends Exception | |
| { | |
| public function show() | |
| { | |
| echo "<pre>"; | |
| echo $this->getMessage(); | |
| echo "<br> | |
| File:"; | |
| echo $this->getFile(); | |
| echo "<br> | |
| Line:"; | |
| echo $this->getLine(); | |
| echo "<br> | |
| TraceAsString:<br><pre>"; | |
| echo $this->getTraceAsString(); | |
| echo "</pre>"; | |
| } | |
| } | |
| class WkPdf{ | |
| public $wkhtmltopdf_path='/usr/bin/wkhtmltopdf'; | |
| //Debian için | |
| // wkhtmltopdf: cannot connect to X server hatası verirse xvfb kurmak gerekir -> apt-get install xvfb | |
| // xvfb eğer sleep not found hatası verirse -> ln -s /bin/sleep /usr/bin/sleep | |
| //public $command='xvfb-run -a -s "-screen 0 640x480x16" -f "/tmp/" %s -q - -'; | |
| public $command='%s -q - -'; | |
| public $html; | |
| public $file; | |
| public $file_path; | |
| public $pdf_source; | |
| public $errors; | |
| public $return_values; | |
| public function init(){ | |
| try{ | |
| $this->_init(); | |
| }catch(WkPdfException $e){ | |
| echo $e->show(); | |
| exit; | |
| } | |
| try{ | |
| $this->_initCommand(); | |
| }catch(WkPdfException $e){ | |
| echo $e->show(); | |
| exit; | |
| } | |
| } | |
| public function _init(){ | |
| if($this->html==NULL){ | |
| throw new WkPdfException("Lütfen Dönüştürmek İstediğiniz Html Kodunu Belirtin 'setHtml()'"); | |
| } | |
| if($this->file==NULL){ | |
| throw new WkPdfException("Lütfen Oluşuturulacak Dosya Adını Belirtin 'setFile()'"); | |
| } | |
| if(file_exists($this->wkhtmltopdf_path)===FALSE){ | |
| throw new WkPdfException($this->wkhtmltopdf_path." Bulunamadı Değil"); | |
| } | |
| } | |
| function _initCommand() | |
| { | |
| // https://gist.github.com/1965886#file_generate_pdf.php | |
| // Run wkhtmltopdf | |
| $descriptorspec = array( | |
| 0 => array('pipe', 'r'), // stdin | |
| 1 => array('pipe', 'w'), // stdout | |
| 2 => array('pipe', 'w'), // stderr | |
| // Oluşacak Hataları Dosyayada Yazabiliriz. | |
| // 2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to | |
| ); | |
| $process = proc_open(sprintf($this->command,$this->wkhtmltopdf_path), $descriptorspec, $pipes); | |
| if (is_resource($process)===FALSE) { | |
| throw new WkPdfException("Process Başlatılamadı"); | |
| } | |
| // Send the HTML on stdin | |
| fwrite($pipes[0], $this->html); | |
| fclose($pipes[0]); | |
| // Read the outputs | |
| $this->pdf_source = stream_get_contents($pipes[1]); | |
| $this->errors = stream_get_contents($pipes[2]); | |
| // Close the process | |
| fclose($pipes[1]); | |
| $this->return_values = proc_close($process); | |
| if ($this->errors!=NULL) { | |
| throw new WkPdfException("Pdf Oluşturulurken Hata Oluştu Hata Mesajı: ".nl2br(htmlspecialchars($this->errors))); | |
| } | |
| } | |
| public function getPdf(){ | |
| if (ini_get('zlib.output_compression')) | |
| ini_set('zlib.output_compression', 'Off'); | |
| header('Content-Type: application/pdf'); | |
| header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); | |
| header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); | |
| header("Cache-Control: private", false); // required for certain browsers | |
| header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); | |
| header("Pragma: no-cache"); | |
| header("Content-Disposition: attachment; filename=\"" . basename($this->file) . "\";"); | |
| header('Content-Transfer-Encoding: binary'); | |
| //header("Content-Type:application/octet-stream"); | |
| echo $this->pdf_source; | |
| } | |
| public function savePdf(){ | |
| try{ | |
| $this->_checkFilePath(); | |
| }catch(WkPdfException $e){ | |
| echo $e->show(); | |
| exit; | |
| } | |
| try{ | |
| $this->_savePdf(); | |
| }catch(WkPdfException $e){ | |
| echo $e->show(); | |
| exit; | |
| } | |
| } | |
| public function _savePdf(){ | |
| if(file_put_contents($this->file_path.$this->file,$this->pdf_source)===FALSE){ | |
| throw new WkPdfException($this->file_path.$this->file." Dosyasına Pdf Yazılırken Hata Oluştu"); | |
| } | |
| } | |
| public function _checkFilePath(){ | |
| if($this->file_path===NULL){ | |
| throw new WkPdfException("Lütfen Pdf dosyasının oluşturulacağı dizini belirtin 'setFilePath()'"); | |
| } | |
| if(is_writable($this->file_path)===FALSE){ | |
| throw new WkPdfException($this->file_path." Yazılabilinir Değil setFilePath()"); | |
| } | |
| } | |
| public function setHtml($html=NULL){ | |
| $this->html=$html; | |
| } | |
| public function setFile($file=NULL){ | |
| $this->file=$file; | |
| } | |
| public function setFilePath($file_path=NULL){ | |
| $this->file_path=$file_path; | |
| } | |
| public function setWkPath($f){ | |
| try{ | |
| $this->_setWkPath($f); | |
| }catch(WkPdfException $e){ | |
| echo $e->show(); | |
| exit; | |
| } | |
| $this->wkhtmltopdf_path=$f; | |
| } | |
| public function _setWkPath($f){ | |
| if(file_exists($f)===FALSE){ | |
| throw new WkPdfException($f." Bulunamadı"); | |
| } | |
| } | |
| public function setCommand($command){ | |
| $this->command=$command; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment