dompdf : image not found or type unknown

2019-07-29 22:29发布

问题:

Here is my code, I mostly tried all the methods for displaying image on PDF but still didnt work. Could you please help me for this. I also set DOMPDF_ENABLE_REMOTE to true and still results is same.

require_once("dompdf/autoload.inc.php");
//require('WriteHTML.php');
$html = '<body>
 <div id="watermark"><img src="/var/htdocs/PDF/header.png" height="100%" width="100%"></div>
  <div id="header">
    <h3 style="padding-top:10px;">'.$_POST['name'].'</h3>
  </div>
  <div id="footer">
    <h3 style="padding-top:8px; padding-right:25px;" class="CIJ">ABSOLUTE MARKET INSIGHTS</h3>
  </div>
   <div>
    <br><br><div class="ooo" style="padding-top: 20px;">'.$_POST['comment'].'</div>
  </div>
</body>';

use Dompdf\Dompdf;
$dompdf = new Dompdf();
$canvas = $dompdf->get_canvas();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($_POST["name"], array("Attachment" => false));

回答1:

You should be using the full URL instead of a direct path. Especially when it is not a static image: dompdf will open that php script directly, so it won't be executed as if it's a PHP script.

If the full URL doesn't work, you can also show what the result of header.php is. Some good things to keep in mind are to send proper content-type headers and so on.



回答2:

Have you tried full path/url:
<img src="http://domain.com/var/htdocs/PDF/header.png" height="100%" width="100%">

Or with a more variable solution:

$baseurl = "http://www.domain.com";
echo '<img src="' . $baseurl . '/var/htdocs/PDF/header.php" height="100%" width="100%">';