Postingan lainnya
Dompdf tidak tampil gambar dicodeigniter 3
logo.PNG[img]https://i.imgur.com/RSaqCHw.png[/img]
// view
<div class="header">
<img src="<?= base_url('upload/circle.jpg') ?>">
<h3>PEMERINTAHAN KABUPATEN KARAWANG<br>KECAMATAN TEMPURAN<br>DESA LEMAH MAKMUR</h3>
</div>
// Controller
public function pdf($id_surat)
{
$this->load->model('ModelSurat');
$data['surat'] = $this->ModelSurat->detail($id_surat);
$this->load->library('pdf');
$this->pdf->setPaper('A4', 'potrait');
$this->pdf->filename = "surat_pengantar.pdf";
$this->pdf->load_view('surat/printSurat', $data);
}
// Lib
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter DomPDF Library
*
* Generate PDF's from HTML in CodeIgniter
*
* @packge CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Ardianta Pargo
* @license MIT License
* @link https://github.com/ardianta/codeigniter-dompdf
*/
use Dompdf\Dompdf;
class Pdf extends Dompdf{
/**
* PDF filename
* @var String
*/
public $filename;
public function __construct(){
parent::__construct();
$this->filename = "laporan.pdf";
}
/**
* Get an instance of CodeIgniter
*
* @access protected
* @return void
*/
protected function ci()
{
return get_instance();
}
/**
* Load a CodeIgniter view into domPDF
*
* @access public
* @param string $view The view to load
* @param array $data The view data
* @return void
*/
public function load_view($view, $data = array()){
$html = $this->ci()->load->view($view, $data, TRUE);
$this->load_html($html);
// Render the PDF
$this->render();
// Output the generated PDF to Browser
$this->stream($this->filename, array("Attachment" => false));
}
}
Permisi suhu ada yang tau ini masalah nya dimna ya padahal saya sudah memanggil gambar sesuai direktori yg saya bikin.
ada solusi ataw saran harus ganti lib ?
2 Jawaban:
<div>Untuk menampilkan gambar dengan dompdf, coba set Root-nya terlebih dahulu, contohnya saya seperti ini :<br><br>1. PDFLib (Custom library)</div><pre><em><?php defined</em>('BASEPATH') <em>OR exit</em>('No direct script access allowed');
<em>use </em>Dompdf\Dompdf; <em>use </em>Dompdf\Options; <em>class </em>PDFlib <em>extends </em>Dompdf{ <em>protected </em>$ci; <em>private </em>$filename;
<em>public function </em>__construct()
{
<em>parent</em>::<em>__construct</em>();
$this-&gt;ci =&amp; get_instance();
}
<em>public function </em>setFileName($filename)
{ $this->filename = $filename; }
<em>public function </em>loadView($viewFile, $data = <em>array</em>()) { $options = <em>new </em>Options(); $options->setChroot(FCPATH); <-- Set root nya ke /var/www/html/nama-project $options->setDefaultFont('courier');
$this-&gt;setOptions($options);
$html = $this-&gt;ci-&gt;load-&gt;view($viewFile, $data, true);
$this-&gt;loadHtml($html);
$this-&gt;render();
$this-&gt;stream($this-&gt;filename, ['Attachment' =&gt; 0]);
}
}
<em>/* End of file PDFlib.php */</em></pre><div><br>2. Controller</div><pre><em>public function </em>cetak() { $this->pdflib->setFileName('Nama_file.pdf'); $this->pdflib->setPaper('A4', 'Landscape'); $this->pdflib->loadView('pelanggan/cetak-pelanggan', $data); }</pre><div><br>3. View</div><pre><img <em>src</em>="<em><?php echo </em>'assets/img/logo.png'; <em>?></em>" <em>alt</em>=""> <-- Tinggal ehco aja langsung, tanpa base_url juga bisa</pre><div><br>Silahkan di coba.</div>
Tanggapan
terimakasih, sangat membantu.
ini cara set rootnya bagaimana ya mas? apa ditulis di dalam si function setChroot? atau bukan?
method setChroot itu bagian dari class dompdf Options, bisa ditulis pada bagian mau generate pdf-nya, silahkan cek script di atas
<div>Referensinya :<br><a href="https://github.com/dompdf/dompdf/issues/2229#issuecomment-692338880">https://github.com/dompdf/dompdf/issues/2229#issuecomment-692338880</a><br><br>Link ini menggunakan bantuan variabel global $_SERVER<br><a href="https://stackoverflow.com/questions/25558449/dompdf-image-not-readable-or-empty">https://stackoverflow.com/questions/25558449/dompdf-image-not-readable-or-empty</a></div>
Tanggapan
terima kasih mas sangat membantu semoga di balas sama allah swt amiiinn