wkhtmltopdf failing to convert local pages to PDF
Asked Answered
R

1

1

I've been trying to get wkhtmltopdf to convert pages on a website and it's failing to convert pages that are on the same. It'll convert and store external pages (tried it with google and bbc.co.uk, both worked) so the permissions are fine but if I try to convert a local page, either a static html file or one generated by a script, it takes around 3 minutes before failing.

The output says the page has failed to load, if forcibly ignore this, I end up with a blank PDF.

I thought it might be session locking but closing the session resulted in the same issue. I feel it's something down to the way the server may be behaving though

Here's the code in question:

            session_write_close ();
    set_time_limit (0);
            ini_set('memory_limit', '1024M');
    Yii::app()->setTheme("frontend");

    // Grabbing the page name
    $ls_url = Yii::app()->request->getHostInfo().Yii::app()->request->url;

    // Let's remove the PDF otherwise we'll be in endless loop
    $ls_url = str_replace('.pdf','',$ls_url);

    // Setting paths
    $ls_basePath = Yii::app()->basePath."/../extras/wkhtmltopdf/";
    if(PHP_OS=="Darwin")
        $ls_binary = $ls_basePath . "wkhtmltopdf-osx";
    else 
        $ls_binary = $ls_basePath . "wkhtmltopdf";

    $ls_generatedPagesPath = $ls_basePath . "generated-pages/";
    $ls_outputFileName = str_replace(array("/",":"),"-",$ls_url)."--".date("dmY-His").".pdf";
    $ls_outputFile = $ls_generatedPagesPath. $ls_outputFileName;

    // making sure no nasty chars are in place
    $ls_command = escapeshellcmd($ls_binary ." --load-error-handling ignore " . $ls_url . " " . $ls_outputFile);


    // Let's run things now
    system($ls_command);
Retreat answered 8/5, 2012 at 16:48 Comment(2)
Step one for me when debugging wkhtmltopdf is to just hardcode the command in a shell_exec(). If that works, you know it's not a wkhtmltopdf problem. I would get that one line of code working with the local copy before dealing with anything else.Excerpta
but if I try to convert a local page ... it takes around 3 minutes before failing - is that the case if you run it from the command line also?Geriatric
N
2

did you lynx that exact url? since wkhtmltopdf is actually small but powerful webkit browser, it fails places just like a normal browser.

check the URL you gave, check external URLs within your page are accessible from your server. It loads CSS, external images, iframes, everything before it even starts making PDF.

Personally, I love wkhtmltpdf. nothing beats it.

Noles answered 8/5, 2012 at 17:11 Comment(7)
Just tested with Lynx. Exactly the same symptoms, local address takes 30 seconds with no output (presumably it's failed), google returns output quickly. Looks like it's something up with the server configuration. Any ideas what could be causing it? Are my hosts doing something like locking apache to a single process?Retreat
means its not wkhtmltpdf. are your using localhost in URL? try pinging the domain name of your URL from within your server. if you can'tm means your domain name which resolves to your IP which is picked up by your firewall+NAT and so you can never connect to yourself from external IP.Noles
It's been doing it through the url generated by Yii which in this case is an IP address (domain's not active at the moment). The ping failed too so it's looking very likely it's being firewalled. I'd thought I'd tried allowing all through port 80 but it looks like there's some additional measures I don't have access to. Looks like I might have to give my hosts a call.Retreat
there is always a localhost/youpath/yourfile.php that you can use to fetch your site. if your apache virtualhost does not have ServerName then it will work just fine.Noles
Managed to get it sorted in the end. The hosts pointed an internal IP to the proper site root which stops it being firewalled and didn't need much recoding to implement. Thanks for the help.Retreat
Annnnd when I copied it over to the live folder and pointed the domain at it, it broke. It now just completely fails to run at all (--version/--help give no output), tested Lynx, that's still working, exact same files, no issues with the file permissions. Even more confusingly, when I pointed the IP address back at the previously working dev version, that was broken too. No errors, nothing in the log. I've spent days on this now, it's driving me mad.Retreat
completely fails to run at all was it wkhtmltopdf that failed? if your have access to your site's apache config, you need to setup a localhost entry within it for this to work.Noles

© 2022 - 2024 — McMap. All rights reserved.