wkhtmltopdf 无法将本地页面转换为 PDF [英] wkhtmltopdf failing to convert local pages to PDF

查看:69
本文介绍了wkhtmltopdf 无法将本地页面转换为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让 wkhtmltopdf 转换网站上的页面,但无法转换相同的页面.它将转换和存储外部页面(在 google 和 bbc.co.uk 上尝试过,都有效)所以权限很好,但是如果我尝试转换本地页面,无论是静态 html 文件还是由脚本生成的页面,大约需要 3 分钟才能失败.

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.

输出说页面加载失败,如果强行忽略这个,我最终会得到一个空白的PDF.

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

这是有问题的代码:

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

推荐答案

你有 lynx 那个确切的 url 吗?由于 wkhtmltopdf 实际上是一个小而强大的 webkit 浏览器,它会像普通浏览器一样失败.

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

检查您提供的 URL,检查您页面中的外部 URL 是否可以从您的服务器访问.它甚至会在开始制作 PDF 之前加载 CSS、外部图像、iframe 以及所有内容.

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.

就我个人而言,我喜欢 wkhtmltpdf.没有什么比它更好的了.

Personally, I love wkhtmltpdf. nothing beats it.

这篇关于wkhtmltopdf 无法将本地页面转换为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆