Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wkhtmltopdf doesn't print images with subdomain

I'm using Wkhtmltopdf to convert some files to PDF in a Symfony2 application. In fact, KnpSnappyBundle does. This bundle works great, but I have one weird issue:

The web application has two subdomains: test.domain and prod.domain. The two subdomains have exactly the same content at this moment. For some reason, the images (converted to PDF) are not printed when using the prod subdomain. But if I access the URL directly, the image exists. An example would be:

<img src="prod.domain/img/theImage.jpg" /> <!-- This doesn't print the image but the URL is accessible -->
<img src="test.domain/img/theImage.jpg" /> <!-- This works right -->

I've finally decided temporally use the test subdomain image URL, but it's weird... Any idea?

Edit:

I forgot to say that the prod domain use SSL, while test doesn't. This seems to be the main problem.

I've executed from the server:

 wkhtmltopdf https://prod.domain test.pdf

Loading pages (1/5)
QSslSocket: cannot resolve SSLv2_client_method               ] 10%
QSslSocket: cannot resolve SSLv2_server_method
Error: Failed loading page https://prod.domain (sometimes it will work just to ignore this error with --ignore-load-errors)

So I've tried again:

wkhtmltopdf --ignore-load-errors https://prod.domain test.pdf

Loading pages (1/5)
QSslSocket: cannot resolve SSLv2_client_method               ] 10%
QSslSocket: cannot resolve SSLv2_server_method
Warning: Failed loading page https://prod.domain (ignored)  
Resolving links (2/5)
Counting pages (3/5)                                                      
Printing pages (5/5)                                                      
Done               

But the new file is bank.

If I use the test domain (no SSL):

 wkhtmltopdf http://test.domain test.pdf

Loading pages (1/5)
QSslSocket: cannot resolve SSLv2_client_method               ] 21%
QSslSocket: cannot resolve SSLv2_server_method
Resolving links (2/5)                                              
Counting pages (3/5)                                                      
Printing pages (5/5)                                                      
Done                      

I'm getting the first two errors, but it works and the file is correct.

like image 225
Manolo Avatar asked Jan 18 '26 15:01

Manolo


2 Answers

I had a similiar problem once.

You always have to remember that wkhtmltopdf runs on the server.

That means:

  • Different ip than your dev machine (IP restrictions on prod.domain? )
  • No session started (Only logged in users can see the file? )
  • Doesn't know your hosts file (Maybe you have defined prod.domain in your hosts file? )

Try to wget prod.domain/img/theImage.jpg when logged in on the server running wkhtmltopdf.

like image 154
Marcel Burkhard Avatar answered Jan 20 '26 13:01

Marcel Burkhard


Shot in the dark... Try proxying the image through an action. Something like this where we first load the file with Imagine and then output the image content in the Response:

/**
 * @Route("/someroute/image", name="someroute_image")
 * @Method("get")
 */
public function imageAction(Request $request)
{
    $imagine = new Imagine();
    $absoluteUrl = $request->getSchemeAndHttpHost() . $this->get('templating.helper.assets')->getUrl('/images/logo.png');
    $image = $imagine->open($absoluteUrl);
    $response = new Response($image->get('png'));
    $response->headers->set('Content-Type', 'image/png');
    return $response;
}

Link to the action in the src= attribute of your img tag rather than the image directly.

like image 37
nurikabe Avatar answered Jan 20 '26 14:01

nurikabe