2
require_once '../system/conf/options.class.php';

$file = basename(urldecode($_GET['logo']));
$fileDir = Options::getSetting('S_UPLOAD_DIRECTORY');

if (file_exists($fileDir . $file)) {

    // Note: You should probably do some more checks 
    // on the filetype, size, etc.
    $contents = file_get_contents($fileDir . $file);

    $ext = @end(explode('.', $file));
    $file = @end(explode('/', $file));

    // Note: You should probably implement some kind 
    // of check on filetype
    header('Content-type: image/' . $ext);

    echo $contents;

}

When doing <img class="logo" src="getLogo.php?logo={$PAGE['general']['logo_file']}" /> From inside the PDF template, I get this error

Image type unknown getLogo.php?logo=75c562d5-8718-411c-a464-72c4d1c35f49.png

I've also tried <img class="logo" href="getLogo.php?logo={$PAGE['general']['logo_file']}" /> which doesn't show the error, but doesn't show the image, either.

Is there a simple solution?

DavidScherer
  • 863
  • 1
  • 14
  • 26
  • How are you implementing dompdf? Your getLogo.php script may be sufficient, but since your PHP needs to be parsed your script has to be accessed through a web server. This has implications for how you load your document into dompdf and how you reference the image source. – BrianS Mar 02 '13 at 04:56
  • I'm pretty sure that since dompdf is running as php on the server loading the image file outside the web root wont be an issue so currently I'm just loading the static image. If its an issue in production i may move them into the document root as they're just logos. As for how I'm implementing dompdf I'm nit sure I know what you mean. I fetch the html for the dmpdf obj via smarty and stream the pdf. – DavidScherer Mar 03 '13 at 06:40
  • 1
    Yes, loading an image from the file system is fine. As far as implementmentation I mean the code you're using. Based on your comment I guess you're generating your HTML document then loading it into dompdf using `$dompdf->load_html()`. In this instance dompdf treats the document as if it came from the local file system. Which means images that are referenced without a domain will be fetched via the local file system. So image references that are PHP scripts won't work because the PHP won't be parsed. Short of it, use something like ` – BrianS Mar 04 '13 at 04:44
  • Did you solve this thing? I have the same issue, the image is served from MongoDB GridFS, in browser the same URL shows the image correctly but not in dompdf. I'm using absosulte path to server ... – David Marko Nov 24 '14 at 11:31
  • @DavidMarko I ended up using the path to the file, rather than trying to echo the content with another php script as DOMPDF does not seem to load external resources when rendering, so it won't fetch image content from a URL, but it will load a local File System resource, so if the images are stored on the local hard disk, just use /path/to/file.img – DavidScherer Nov 24 '14 at 16:31
  • @BrianS your comment here is basically the solution to this, if you'd be so kind as to write it up as the answer I would gladly accept it so this question doesn't sit unanswered forever. Apparently a lot of people search for this. – DavidScherer Sep 21 '16 at 12:58
  • @DavidScherer will do. I'll try to add some additional info about how external resource references are parsed. – BrianS Sep 21 '16 at 18:15

1 Answers1

0

Based on comments ... you're generating your HTML document then loading it into Dompdf using $dompdf->load_html(). In this instance Dompdf treats the document as if it came from the local file system. Which means images that are referenced without a domain will be fetched via the local file system. So image references that are PHP scripts won't work because the PHP won't be parsed. Short of it, use something like <img src="http://example.com/getLogo.php..."> if you can.


There are a few SO answers that may help in determining how paths are determined when loading a local file using load_html_file and load_html.

Community
  • 1
  • 1
BrianS
  • 13,284
  • 15
  • 62
  • 125