Wkhtmltopdf does not print images with subdomain
I am using Wkhtmltopdf to convert some files to PDF in Symfony2 application . In fact, KnpSnappyBundle
it does. This package works great, but I have one weird problem:
The web application has two subdomains: test.domain
and prod.domain
. Currently, two subdomains have exactly the same content. For some reason, images (converted to PDF) do not print when using the subdomain prod
. 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 finally decided to temporarily use the subdomain image url test
, but this is weird ... Any idea?
Edit:
I forgot to say that the domain is prod
using SSL and test
not. This appears to be the main problem.
I ran 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 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 a bank.
If I am using a domain test
(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 get the first two errors, but it works and the file is correct.
source to share
I had a similar problem.
You should always remember that wkhtmltopdf is running on the server.
It means:
- Different ip than your developer computer (restrictions on prod.domain IP address?)
- Session not started (Only registered users can see the file?)
- Doesn't know your hosts file (perhaps you defined prod.domain in your hosts file?)
Try wget prod.domain/img/theImage.jpg
when logging into the server that is running wkhtmltopdf.
source to share
Shot in the dark ... Try proxying the image through action. Something like this, when we first load the Imagine file and then output the image content in 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 an attribute of src=
your img tag, not the image directly.
source to share
I think i had the same problem, heres my code (pretty old)
protected function prepareForRender($data) {
$data['app']=array('request' => array('schemeAndHttpHost' => "http://your.host.com"));
$this->container->enterScope('request');
$this->container->set('request', new Request(), 'request');
return $data;
}
public function generatePdf(){
$data=array();
$data=$this->prepareForRender($data);
$html=$this->container->get('templating')->render('someBundle:Report:foo.pdf.html.twig', $data);
try {
$pdfString=$this->knp_snappy->getOutputFromHtml($html, array(
'enable-javascript' => true,
'javascript-delay' => 1000,
'no-stop-slow-scripts' => true,
'no-background' => false,
'lowquality' => false,
'page-height' =>200,
'page-width' => 300,
'encoding' => 'utf-8',
'images' => true,
'cookie' => array(),
'dpi' => 300,
'image-dpi' => 300,
'enable-external-links' => true,
'enable-internal-links' => true
));
} catch(\Exception $e) {
$this->logger->crit($e->getMessage());
}
return $pdfString;
}
and img src should be sbolute url sth like:
<img width="405" height="130" src="{{ app.request.schemeAndHttpHost }}/bundles/akdjaskld/Header.png" alt="">
you are better off making a parameter from http://your.host.com
so that you have different versions for each step, maybe you just didn't specify the parameterimages=>true
source to share