KnpLabs / KnpSnappyBundle

Easily create PDF and images in Symfony by converting html using webkit
http://knplabs.com
MIT License
1.23k stars 143 forks source link

Images not showing in PDF on live server #63

Closed SilexNoobie closed 9 years ago

SilexNoobie commented 10 years ago

I'm using this to render an HTML file to a pdf. I've built it on a test server and it all works perfectly but when I move it to the live server none of the images show at all. I've tried using png and jpg images as well as relative, absolute and full paths but nothing I do seems to work.

I've attached screen shots of both the live and test sites so you can see what I get. live

live test

pilot commented 10 years ago

It's obvious that this is problem come from the configuration of the live server.

SilexNoobie commented 10 years ago

Yes, the html displays everything as it should be. I thought that both of the servers where configured the same but I'm guessing not. I didn't set them up myself so wasn't sure

SilexNoobie commented 10 years ago

How do I check to see if it's set up properly? Everything else seems to be working fine.

pilot commented 10 years ago

to check, run wkhtmltopdf from the console of the live server with same parameters what you call it from the web and check generated pdf file, you should got same problem, but from console you'll see verbose info to recognize problem

SilexNoobie commented 10 years ago

I'm really sorry to ask such a dumb question but how do I do that? I've only just started to work with Terminal and am insure of a lot of it.

Normally I use this to upload files:

cd /Users/macuser/Sites/livesite

then

vagrant up

then once I've done that I upload files by typing this:

./deploy.sh livesite --go

SilexNoobie commented 10 years ago

If I try typing "wkhtmltopdf http://www.google.com test.pdf" into the console (after I've type vagrant up) I get an error saying -bash: wkhtmltopdf: command not found

pilot commented 10 years ago

find first where is wkhtmltopdf locate on your server, default should be /usr/local/bin or check your paramters.yml you need to setup path for web too where lib is located

SilexNoobie commented 10 years ago

I've just checked and the path is home/ubuntu/wkhtmltox/bin/wkhtmltopdf (it's the same for wkhtmltoimage too) although I couldn't find a parameters.yml file

SilexNoobie commented 10 years ago

I've tried home/ubuntu/wkhtmltox/bin/wkhtmltopdf http://www.google.com test.pdf but I still get the same error.

I'm really desperate to get this showing the images

pilot commented 10 years ago

sorry, what kind of error?

SilexNoobie commented 10 years ago

Sorry the error says -bash: wkhtmltopdf: command not found

Other than the images not showing in the pdf this is working perfectly. My boss wants a demo of it this afternoon so I'm now panicking about getting this working with the images

pilot commented 10 years ago

are you sure /home/ubuntu/wkhtmltox/bin/wkhtmltopdf http://www.google.com test.pdf

SilexNoobie commented 10 years ago

I hadn't tried it with the leading slash but having done that I still get the same error: -bash: /home/ubuntu/wkhtmltox/bin/wkhtmltopdf

I used FileZilla to check the path but have also attached a screen shot to make sure I got it right

wkhtmltopdf

SilexNoobie commented 10 years ago

I've still not managed to get this sorted and would be grateful for some help please.

pilot commented 10 years ago

run it with /home/ubuntu/wkhtmltox/bin/wkhtmltopdf http://www.google.com test.pdf

SilexNoobie commented 10 years ago

I just tried that and got this: -bash: /home/ubuntu/wkhtmltox/bin/wkhtmltopdf: No such file or directory

First of all I typed: cd /Users/macuser/Sites/livesite then launched Vagrant by using: vagrant up and before I typed in what you suggested I went into the site directory by using: ./deploy.sh livesite

I'm not sure what I'm doing wrong, normally I upload files by using: ./deploy.sh live site --go So I'm not sure why this isn't working

SilexNoobie commented 10 years ago

I don't know if it makes any difference but I'm trying to get this to work on a live AWS site not my machine.

SilexNoobie commented 10 years ago

I've also tried to see if I can access the /home/ubuntu or just /home folder and both of them say the folder doesn't exist, I'm guessing that I'm not actually connecting to the directory then. How do I go about doing this?

SilexNoobie commented 10 years ago

I'm really desperate to get this sorted today, I'd be happy to pay for help to get this set up properly

pilot commented 10 years ago

@SilexNoobie sorry can't help you with server configuration, you can just publish your help request on some freelance board like freelance.com or ask question about what is wrong for your live aws server configuration at the http://serverfault.com

As well I've recommend you not use aws, but better preconfigured digitalocean.com cloud server, which have same price

SilexNoobie commented 10 years ago

Thanks @pilot I'll have a look there. Can I ask why you recommend not using AWS?

pilot commented 10 years ago

aws is good, digitalocean is same but much more easy and clear to configure, as well as far I know they host his severs on the aws, so digital ocean is kind of management platform for aws

SilexNoobie commented 10 years ago

Thanks, I'll look into that

SilexNoobie commented 10 years ago

I've managed to access the folder now and when I go to: /home/ubuntu/wkhtmltox/bin/wkhtmltopdf http://www.google.com test.pdf

I get this:

Loading pages (1/6) QSslSocket: cannot resolve SSLv2_client_method ] 10% QSslSocket: cannot resolve SSLv2_server_method Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6) Done
ubuntu@ip-172-31-35-191:~$

Does that help with the set up of it?

SilexNoobie commented 10 years ago

I've just tested it again and when I go to /home/ubuntu/wkhtmltox/bin/wkhtmltopdf http://www.google.com test.pdf I get this:

Loading pages (1/6) QSslSocket: cannot resolve SSLv2_client_method ] 10% QSslSocket: cannot resolve SSLv2_server_method Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6) Done

Then it saves a copy of the homepage to the server. So it's definitely working but I don't understand why it's not working working when I use it on the site.

pilot commented 10 years ago

@SilexNoobie so test.pdf contain all images?

if all right, this is meant that in the template what you use for generate pdf you need to put absolute path to the images, otherwise lib doesn't find it

SilexNoobie commented 10 years ago

All of the paths are absolute, originally they were http://{{ domain }}/pdf/images/background_logo.png, http://{{ domain }}/pdf/images/header.png and http://{{ domain }}/pdf/images/footer.png. I changed them to http://subdomain.livesite.com/pdf/images/background_logo.png, http://subdomain.livesite.com/pdf/images/header.png and http://subdomain.livesite.com/pdf/images/footer.png to make sure that the path was definitely correct but that didn't make any difference. I've tested the paths in a browser and they load no problems.

This works fine on the test server so I'm not sure why its not working on the live site. The code used to generate the pdf is:

public function generatePdf($app) { $products = $this->getRecurringOrderProducts(); if (count($products) > 0) { $filename = $this->getPdfFilename($app); if (!file_exists($filename)) { // Generate HTML $html = $app['twig']->render('order/pdf.twig', array( 'entry' => $this, 'products' => $products, 'expiry_date' => $this->getExpiryDate(), // Add this line 'content_height' => intval( (8_100_count($products))/95 )+1, 'domain' => $app['config']['domain'], )); // Generate PDF $snappy = new Pdf($app['wkhtmltox_bin']); $snappy->setOption('orientation', 'landscape'); $snappy->generateFromHtml(utf8_decode($html), $filename); } } }

SilexNoobie commented 10 years ago

Yep the test.pdf had all of the images, I've tried to generate the pdf again myself but it's still not working. I've tried using:

http://{{ domain }}/pdf/images/background_logo.png http://subdomain.livesite.com/pdf/images/background_logo.png

both of which work on the live server.

I this is really puzzling me and would be grateful for any suggestions

SilexNoobie commented 10 years ago

I've managed to fix this, the problem was that the image url needed to be absolute but when I tested it before the server hadn't cached it so was displaying the old file.

webdevilopers commented 9 years ago

Please close issue then. Thanks.

swapnil-woxi commented 9 years ago

I am using mpdf. It work perfectly on local server. When I put that file on live server then it not showing images in pdf. It gives error 'Could not find image file'. I have check image url. On url image is available. I have same problem on TCPDF & mpdf.

webdevilopers commented 9 years ago

Have you used the absolute path to the image @swapnil-woxi ?

swapnil-woxi commented 9 years ago

I have provide image path through database. It fetch from it & put into src="{image path}" http://www.universalmanagement.co.in/alpha/assets/Tata-Docomo.jpg

webdevilopers commented 9 years ago

The path you provided should work. Have you tried to output your Twig template and check if the image is shown? Perhaps you should experiment with Twig and absolute paths or different URLs: https://gist.github.com/webdevilopers/e0d34a1c04ef2050a450#comment-1368355

swapnil-woxi commented 9 years ago

I am using CodeIgniter framework & mpdf or TCPDF is base on php. I call that pdf file using absolute path. Images also call using absolute path because they are stored in database with absolute path.

webdevilopers commented 9 years ago

CodeIgniter framework & mpdf or TCPDF - what does your issue have to do with this bundle and wkhtmltopdf?

swapnil-woxi commented 9 years ago

I have create PDF on one button click. So I use mpdf & tcpdf code creating pdf. This pdf working properly on localhost. But when I put this code on server I get data properly on pdf ,but images are not showing on that . Project for which this pdf functionality is use , that project develop in CodeIgniter Framework.

webdevilopers commented 9 years ago

This is issue described here is about the Symfony2 KnpSnappyBundle and wkhtmltopdf!

I guess you are looking for:

vishma9 commented 7 years ago

how to add water mark in pdf. i have used KnpSnappyBundle

miheretab commented 6 years ago

@SilexNoobie I know this is old post but can you tell me how you fix this issue, it is not clear when you say absolute path, it seems you gave absolute url before too, please advise me when you can, I had the same issue and couldn't solve it.

sudeepdk commented 6 years ago

This will work 100 % -- follow this it worked for me ...

https://www.vultr.com/docs/how-to-install-phantomjs-on-ubuntu-16-04

Brokan commented 5 years ago

Similar problem solved by upgrading wkhtmltopdf to 0.12.5

NiazMuhammad1 commented 4 years ago

simply do

<img src="<?php echo 'data:image/jpg;base64,'.base64_encode(file_get_contents('./template/assets/frontend/images/pdf-header.jpg')); ?>" style="width:100%;">

niravgabani commented 1 year ago

this problame i,am fatched. thets solltution has compaletly path given."../".

this try with complatly path../

iddre commented 1 year ago

If you work with PHP (ex. TCPDF), you need install GD library (php-gd).