barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.67k stars 967 forks source link

Images are not working #144

Closed CallumEddisford closed 9 years ago

CallumEddisford commented 9 years ago

Hi Barry/all,

I have been having problems getting images to work when exporting to PDF, everything else is working fine but I keep getting this error:

file_put_contents(): Filename cannot be empty

// Image found, put in cache and process else { //e.g. fetch.php?media=url.jpg&cache=1 //- Image file name might be one of the dynamic parts of the url, don't strip off! //- a remote url does not need to have a file extension at all //- local cached file does not have a matching file extension //Therefore get image type from the content file_put_contents($resolved_url, $image); } }

Now, I have tried every method I can find to fix this problem and I have had no luck.

I have tried changing the image source to every variation available: /img/image.png - img/image.png - ./img/image.png - http://mywebsite.com/img/image.png - laravel asset etc... I have made the temp folder writeable and used the correct temp directory link. DOMPDF_ENABLE_REMOTE is set to true.

when I use /img/image.png as the image source it downloads the pdf but I get: "Image not readable or empty" with the grey X image.

Im running laravel locally on a mac through XAMPP.

I don't usually post things but I have ran out of options for this problem - I have literally tried everything - am I missing something? ;(

Let me know if you want me to post any of my code.

Cheers,

Callum

CallumEddisford commented 9 years ago

Forgot to mention that I cant actually link to anything such as bootstrap css on a cdn or internal style sheets.

barryvdh commented 9 years ago

Can you please paste the code you're using to generate/save the pdf? Are you using the save($path) option, or are you saving it some other way?

First try to render the view/html without PDF, then in the PDF. You can also enable errors in the config.

CallumEddisford commented 9 years ago

Hi Barry,

Thanks for your reply,

Here is my full function which can correctly download a pdf with all the correct data.

public function pdfexportmain($id) {

    $loginSession = Session::get('loginSession');

    if($loginSession == 1){

    $applicants = ApplicantsMain::find($id);

    $businessName = $applicants->businessName;

    $data = array(
        'businessName' => $applicants->businessName,
        'elevatorPitch' => $applicants->elevatorPitch,
        'logo'=> $applicants->logo,
        'stage' => $applicants->stage,
        'category' => $applicants->category,
        'categoryOther' => $applicants->categoryOther,
        'youtubePitch' => $applicants->youtubePitch,
        'traction' => $applicants->traction,
        'tm1Name' => $applicants->tm1Name,
        'tm1Position' => $applicants->tm1Position,
        'tm1Email' => $applicants->tm1Email,
        'tm1Phone' => $applicants->tm1Phone,
        'tm2Name' => $applicants->tm2Name,
        'tm2Position' => $applicants->tm2Position,
        'tm2Email' => $applicants->tm2Email,
        'tm2Phone' => $applicants->tm2Phone,
        'website' => $applicants->website,
        'twitter' => $applicants->twitter,
        'facebook' => $applicants->facebook,
        'linkedin' => $applicants->linkedin,
        'instagram' => $applicants->instagram,
        'whereDidYouHear' => $applicants->whereDidYouHear,
        'reference' => $applicants->reference

    );
    //return View::make('pdfExportMain', $data);

    $mytime = Carbon\Carbon::now();

    $pdf = PDF::loadView('pdfExportMain', $data);
    return $pdf->download($businessName . $mytime . '.pdf');

     }

My view is literally just a blade template with a table in it.

I use the logo string above which I call in my view - I also have tried just to use my site logo as an image. I have tried using both a full link (http://domain.com/images/image.jpg) or a relative link (/folder/folder/image.jpg).

When I render the view the images work just fine and all looks great.

Thanks for your time! :)

Callum

arianfarid commented 9 years ago

I ran into this the other day, you need to reference the public path of your image. So if 'image.jpg' is in your public/images folder and stored in a variable called $image, you can do something like:

<img src="{{ public_path() . $image }}">

dompdf needs the absolute path on the server, not what the client would see!

CallumEddisford commented 9 years ago

Forgot to close this - fixed the problem over a month ago!

For anyone who wants the answer simply, just change your image path in your view for pdf'ing from src="images/image.jpg" to src="/var/www/site/images/image.jpg" basically you gotta use the full path on a server even a local one then you will be good to go.

If you are doing a local host on windows then you need to use c://folder/folder/etc and the same for Mac Applications/website/folder/etc

Enjoy!

pankajbansal commented 8 years ago

Hi @callumEddisford & @barryvdh,

I have tried the absolute path also but image not coming in pdf.

francorafael commented 6 years ago

<?php echo $_SERVER["DOCUMENT_ROOT"].'/public/images/topo.jpg'; ?>

arturpasnik commented 6 years ago

My problem was that after adding in my view {{asset()}} it was taking about 5sec to generate pdf. After change to {{public_path()}} time is back under 1sec :) so better USE public_path() for images and other assets like css

DaliaPinto commented 5 years ago

@arturpasnik Thank you, you saved my life <3

soft4aseman7 commented 1 year ago

i have this problem and everytime i see this line : http://localhost:8000/public\uploads\users\profile168656736044CamScanner 06-12-2023 14.22_1.jpg instead of my image

arturpasnik commented 1 year ago

We need more info like where files are saved so full path, the image file name and how you pull it in the view file.

alhassan-cse commented 5 months ago

@arturpasnik "{{ public_path('backend/img/poshra-logo.png') }}"

not working