Install composer
Install composer sebelum menjalankan perintah composer di terminal. jika sudah terinstall perintah composer langsung saja kelangkah berikutnya.
Install dompdf versi 2.0 dengan perintah berikut :
composer require dompdf/dompdf
dengan hasil file composernya seperti berikut :
{ "require": { "dompdf/dompdf": "^2.0" } }
Setting Applikasi
setelah bserhasil install composer ubah setting $config['composer_autoload'] pada folder aplication/config/config.php
default :
$config['composer_autoload'] = FALSE;
ubah menjadi
$config['composer_autoload'] = 'vendor/autoload.php';
Cara Menggunakan
kemudian create file di folder aplication/library/ dengan nama Pdf.php dengan isi sebagai berikut :
<?php defined('BASEPATH') OR exit('No direct script access allowed');
use Dompdf\Dompdf;
use Dompdf\Options;
class Pdf extends Dompdf{
public $filename;
public function __construct(){
parent::__construct();
$this->filename = "laporan.pdf";
}
protected function ci()
{
return get_instance();
}
public function createpdf($view, $data = array()){
$options = new Options();
$options->setChroot(FCPATH);
$options = $this->getOptions();
$options->set(array('isRemoteEnabled' => true));
$this->setOptions($options);
$html = $this->ci()->load->view($view, $data, TRUE);
$this->load_html($html);
$this->render();
$pdf_string = $this->output();
file_put_contents("pdf/".$this->filename, $pdf_string );
}
public function previewpdf($view, $data = array()){
$options = new Options();
$options->setChroot(FCPATH);
$options = $this->getOptions();
$options->set(array('isRemoteEnabled' => true));
$this->setOptions($options);
$html = $this->ci()->load->view($view, $data, TRUE);
$this->load_html($html);
$this->render();
$this->stream($this->filename, array("Attachment" => false));
}
}
?>
buat file controller di ci3 dan halaman view nya untuk digenerate.
untuk create pdf :
$sign =array("datas"=>"ini file pdf"); $file_name = "inifilenya.pdf"; $this->load->library('pdf'); $this->pdf->setPaper('A4', 'potrait'); $this->pdf->filename = $file_name; $this->pdf->createpdf('file',$sign);Untuk preview pdf :
$sign =array("datas"=>"ini file pdf");$this->load->library('pdf'); $this->pdf->setPaper('A4', 'potrait'); $this->pdf->filename = "iniFile.pdf"; $this->pdf->previewpdf("file",$sign);
viewnya beri name file.php dengan isi sebagai berikut : <?php echo $datas; ?>