1

I am creating a pdf file with dompdf. In the file is a logo .png image with background transparent. When the file is created it renders the image background black and foreground white. Which is not correct. Can someone help to rectify this?

    public function downloadPdf($id)
      {
        $application = FertilAppUser::application($id);
        $schedule = FertilAppUser::schedule($id);
        $fertiluser = FertilAppUser::detail($id);

        $publicPath = public_path('/');
        $view = false;

        // Generate file name
        $filename = $this->makeSafe($application[0]->fname . '_' . $application[0]->bname);
        
        $pdf = PDF::loadView('admin.fertil.schedule.pdf', compact('application', 'schedule', 'fertiluser', 'view'))->setPaper('a4', 'landscape');
        return $pdf->download($filename . '_Bemesting_Skedule.pdf');
    }

Here is the pdf html image path:

<img src="{{ asset('images/logo.png') }}" alt="Logo" style="width: 120px; height:66px;"/>

I also tried public_path('images/logo.png') and './images/logo.png'. This did not work. I also tried using a .jpg file with alpha channel removed with no change in rendering a color photo.

What is strange though is that on my localhost the image is rendered correctly but on my shared hosting provider's server this incorrect image rendering persists. When I logOutputFile it gives:

<span style='color: #000' title='Frames'>   242</span><span style='color: #009' title='Memory'>  20480.00 KB</span><span style='color: #900' title='Time'>    233.81 ms</span><span  title='Quirksmode'>  <span style='color: #d00'> ON</span></span><br />
Hmerman6006
  • 1,622
  • 1
  • 20
  • 45
  • Try hardcoding the full folder path for the image. /var/www/vhosts/subfolder_names/logo.png, or C:/xampp/htdocs/subfolder_names/logo.png or http://yoursite.com/subfolder_names/logo.png. if works one you can do something. – ashanrupasinghe Aug 21 '20 at 12:00
  • @ashanrupasinghe Thanks for your comment. I tried the full path with https and www for `mysite.com/images/logo.png`. It is still rendering a black and white logo. Do you know anything else I can try? – Hmerman6006 Aug 22 '20 at 07:23
  • Your solution works if I use the .jpg file with the alpha channel removed. I also noticed that the .jpg file I used earlier had transparent parts. So I created a new image and explicitly removed the alpha channel, copied it to the `public_path` and `asset` path. Now it renders colour. It seems the main problem is that the domPdf engine cannot render transparency. – Hmerman6006 Aug 22 '20 at 07:40
  • this may help you: https://stackoverflow.com/questions/25558449/dompdf-image-not-readable-or-empty – ashanrupasinghe Aug 24 '20 at 04:18

0 Answers0