Displaying image in PDF inside a while

Asked

Viewed 586 times

0

Good afternoon, I am breaking my head, I need to display images from my bank, in a PDF. The name of the field in the bank is called foto, it keeps the path of the images that are in a directory.

WITHOUT THE DOMPDF, I can display the images like this in PHP:

echo"<td><img src='http://www.meusite.com/".$row_transacoes['foto']."' width='50'></td>"; 

However, with the DOMPDF, I can’t, maybe because I’m changing some accents, to "recognize", so I left it like this:

$html .= '<td><img src="http://www.meusite.com/'.$row_transacoes['foto'].'" width="50"></td>'; 

And is not working, appears that the image was not found...

I’ll be dropping the code, I appreciate any kind of help, I’ve been trying for a long time...

<?php 
// CONEXÃO        
$host = "exemplo";
$user = "exemplo";
$key = "exemplo";
$bd = "exemplo";

$con = new mysqli($host, $user, $key, $bd);
if ($con->connect_errno) {
    echo "Falha ao conectar ao banco: (" . $con->connect_errno . ") " . $con->connect_error;
}


    $html .= '<center><h3>Prontuário</h3></center>';
    $html .= '<table class="table">'; 
    $html .= '<thead>';
    $html .= '<tr>';
    $html .= '<th>Nome</th>';
    $html .= '<th>Foto</th>';
    $html .= '</tr>';
    $html .= '</thead>';
    $html .= '<tbody>';

     // SELECT NOS CAMPOS A SEREM EXIBIDOS
    $result_transacoes = "SELECT nome, foto FROM clientes";

    $resultado_trasacoes = mysqli_query($con, $result_transacoes);

    while($row_transacoes = mysqli_fetch_assoc($resultado_trasacoes)){

        $html .= '<tr><td>'.$row_transacoes['nome'] . "</td>";
        $html .= '<td><img src="http://www.meusite.com/'.$row_transacoes['foto'].'" width="50"></td>';  

}
    $html .= '</tbody>';
    $html .= '</table';

  //referenciar o DomPDF com namespace
    use Dompdf\Dompdf;

  // include autoloader
    require_once("dompdf/autoload.inc.php");

  //Criando a Instancia
    $dompdf = new DOMPDF();

  // Carrega seu HTML
    $dompdf->load_html('
        <div class="header"> 
        <img src="timbrado.png" height="100%" width="100%">
        </div>

        '. $html .'

        '); 

  //Renderizar o html
    $dompdf->render();

  //Exibibir a página
    $dompdf->stream(
        "Prontuario.pdf", 
        array(
       "Attachment" => false //Para realizar o download somente alterar para true
   )
);
?>

I can display image on DOMPDF, thus: echo"<td><img src='http://www.meusite.com/uploads_foto/imagem.png' width='50'></td>";

Only I’m not getting through with the while ($row_transacoes['foto'])

1 answer

1


The problem is that DOMPDF comes with disabled remote images, strangely apply only:

$html .= '<td><img src="/'.$row_transacoes['foto'].'" width="50"></td>';

Should solve, either you did not save correctly or got confused in something in the test, but anyway if you need remote images should use the Dompdf\Options, thus:

use Dompdf\Dompdf;
use Dompdf\Options;

require_once 'dompdf/autoload.inc.php';

$options = new Options();
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);

Here is a simplified example:

<?php

use Dompdf\Dompdf;
use Dompdf\Options;

require_once 'dompdf/autoload.inc.php';

$options = new Options();
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);

$dompdf->loadHtml("
    <img src='/uploads_foto/Ademilson L.jpg' width='50%'><br>
    <img src='http://www.meusite.com/uploads_foto/Ademilson L.jpg' width='50%'>
");

$dompdf->setPaper('A4', 'landscape');

$dompdf->render();

$dompdf->stream('teste.pdf', array('Attachment' => false));
  • That... worked, thank you very much for the attention, sorry anything. Can I ask you to edit and change the name of my site? if possible...

  • @Samuel done and comments removed ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.