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 ?

avatar Amaulid
@Amaulid

10 Kontribusi 0 Poin

Diperbarui 3 tahun yang lalu

Tanggapan

Coba kirim potongan kode nya

udh saya edit gan pertanyaan nyaa ..

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>&lt;?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;

&lt;em&gt;public function &lt;/em&gt;__construct()
{
   &lt;em&gt;parent&lt;/em&gt;::&lt;em&gt;__construct&lt;/em&gt;();
    $this-&amp;gt;ci =&amp;amp; get_instance();
}

&lt;em&gt;public function &lt;/em&gt;setFileName($filename)

{ $this-&gt;filename = $filename; }

<em>public function </em>loadView($viewFile, $data = <em>array</em>()) { $options = <em>new </em>Options(); $options-&gt;setChroot(FCPATH); &lt;-- Set root nya ke /var/www/html/nama-project $options-&gt;setDefaultFont('courier');

  $this-&amp;gt;setOptions($options);

  $html = $this-&amp;gt;ci-&amp;gt;load-&amp;gt;view($viewFile, $data, true);
  $this-&amp;gt;loadHtml($html);
  $this-&amp;gt;render();
  $this-&amp;gt;stream($this-&amp;gt;filename, [&#039;Attachment&#039; =&amp;gt; 0]);

}

}

<em>/* End of file PDFlib.php */</em></pre><div><br>2. Controller</div><pre><em>public function </em>cetak() { $this-&gt;pdflib-&gt;setFileName('Nama_file.pdf'); $this-&gt;pdflib-&gt;setPaper('A4', 'Landscape'); $this-&gt;pdflib-&gt;loadView('pelanggan/cetak-pelanggan', $data); }</pre><div><br>3. View</div><pre>&lt;img <em>src</em>="<em>&lt;?php echo </em>'assets/img/logo.png'; <em>?&gt;</em>" <em>alt</em>=""&gt; &lt;-- Tinggal ehco aja langsung, tanpa base_url juga bisa</pre><div><br>Silahkan di coba.</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

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>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

Tanggapan

terima kasih mas sangat membantu semoga di balas sama allah swt amiiinn

Login untuk ikut Jawaban