wkhtmltopdf failing to convert local pages to PDF

扶醉桌前 提交于 2019-12-11 10:42:13

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/10502873/wkhtmltopdf-failing-to-convert-local-pages-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!