0

I have created a PHP file, the file contains a base64 image, then try to browse the file it displays the image but when convert file to pdf using dompdf the image is not showing

function getdata($visit){
require_once 'dompdf/autoload.inc.php';
    $dompdf = new Dompdf();
    $dompdf->set_option('isRemoteEnabled', TRUE);
$file='http://localhost/xxx/production/datafile.php?id=';
$file .=$visit;
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->load_html_file($file);
    $dompdf->render();
    $dompdf->stream();
}``` 
in datafile, I have attached bease64 image

    Signature:.<?php echo '<img height="75%" width="150px" src="data:image/png;base64,' . $Base64img . '" />'; ?>
Salum Said
  • 11
  • 5
  • Probably unrelated to your question, but `height="75%" width="150px"` is not valid HTML: the non-CSS width and height attributes should contain just a number, which is always interpreted as a number of pixels (e.g. `width="150"` for 150 pixels wide). To use other units like percentages, you need to use CSS, e.g. `` – IMSoP Aug 29 '21 at 11:09
  • Does load_html_file take a URL or a system path? My intuition would assume the latter, try basing it off of $_SERVER["DOCUMENT_ROOT"]. It's also possible that dompdf reads the literal content of the file, and not the parsed PHP output. You may want to use loadHTML in favor of load_html_file since the amount of code is so low and it will remove a point of failure – Wasabi Thumbs Aug 29 '21 at 11:14
  • [This answer](https://stackoverflow.com/a/22911050/10808904) with 9 votes seems to have the same view as mine – Wasabi Thumbs Aug 29 '21 at 11:18
  • @WasabiThumbs i can do that but to minimize codes but – Salum Said Aug 29 '21 at 11:21
  • IT shows when opening on browser not showing on pdf only – Salum Said Aug 29 '21 at 11:22
  • when using loadhtml i will be converting the codes mine is to convert the file and it show so many data as output from php but my only issue is to display base64image on pdf – Salum Said Aug 29 '21 at 11:25

1 Answers1

0

Looking at the source code of dompdf, it's apparent that load_html_file takes in system paths. As you can see, it utilizes Helpers:getFileContent which uses file_get_contents internally, or curl if you have it. Maybe if you install curl on your server, URLs will work. As of now, they will not, because file_get_contents only uses system paths. Because it uses system paths, it will bypass apache and fetch the literal contents of the file, and not whatever the PHP codes for. I would advocate to sidestep the entire problem and simply do

$dompdf->loadHTML('<img style="height: 75%; width: 150px" src="data:image/png;base64,' . $Base64img . '" />');

or install curl if that is not servicable for your needs. Doing

ini_set('allow_url_fopen', false)

should also force it to use curl over file_get_contents.

Wasabi Thumbs
  • 303
  • 1
  • 2
  • 8