【发布时间】:2020-02-29 15:11:50
【问题描述】:
我使用 KnpSnappyBundle 1.6.0 和 wkhtmltopdf 0.12.5 在 PHP 中从 HTML 生成 PDF,如下所示:
$html = $this->renderView(
'pdf/template.html.twig',
[ 'entity' => $entity, ]
);
return new PdfResponse($snappy->getOutputFromHtml($html,
['encoding' => 'UTF-8', 'images' => true]), 'file'.$entity->getUniqueNumber().'.pdf'
);
我的问题: 在我的生产服务器上,当我引用托管在与我的代码相同的服务器上的资产(图像或 css)时,生成 PDF 大约需要 40-50 秒。即使我只使用托管在同一台服务器上的小图像,也需要 40 秒。我可以使用托管在另一台服务器上的更大的图像,并且会立即生成 PDF。
我的服务器在服务资产或文件方面并不慢。如果我只是将 HTML 呈现为一个页面,它会立即发生(有或没有资产)。当我在本地(在我的笔记本电脑上)从我的生产服务器请求资产以生成 PDF 时,它也会立即发生。
我需要的 HTML 中需要呈现为 PDF 的资产都具有绝对 URL,这是 wkhtmltopdf 工作所必需的。例如:<img src="https://www.example.com/images/logo.png"> 困难的是,一切正常,但速度很慢。没有指向会导致超时的不存在资产。
我一开始以为可能和wkhtmltopdf有关,所以我尝试了不同的版本和不同的设置,但这并没有改变任何东西。我还尝试指向同一服务器上的另一个域,问题仍然存在。我尝试不使用 KnpSnappyBundle,但问题仍然存在。
所以我现在的猜测是这是一个服务器问题(或与 wkhtmltopdf 的组合)。我正在运行 Nginx-1.16.1 并通过 SSL 提供所有内容。我已安装 OpenSSL 1.1.1d 2019 年 9 月 10 日(库:OpenSSL 1.1.1g 2020 年 4 月 21 日),我的操作系统是 Ubuntu 18.04.3 LTS。其他一切都在此服务器上按预期工作。
当我查看 Nginx 访问日志时,我可以看到在使用来自同一服务器的资产时,我自己的 IP 地址发出了一个获取请求。我不明白为什么这要花这么长时间,而且我已经不知道接下来要尝试什么了。任何想法表示赞赏!
我将为我的域添加我的 Nginx 配置(以防万一):
server {
root /var/www/dev.example.com/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name dev.example.com www.dev.example.com;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.(?:jpg|jpeg|gif|png|ico|woff2|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|js|css)$ {
gzip_static on;
# Set rules only if the file actually exists.
if (-f $request_filename) {
expires max;
access_log off;
add_header Cache-Control "public";
}
try_files $uri /index.php$is_args$args;
}
error_log /var/log/nginx/dev_example_com_error.log;
access_log /var/log/nginx/dev_example_com_access.log;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/dev.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/dev.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = dev.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name dev.example.com www.dev.example.com;
listen 80;
return 404; # managed by Certbot
}
Udate 2020 年 8 月 5 日:我尝试了 wkhtmltopdf 0.12.6,但这给了我完全相同的问题。几个月前我发布的作为我的问题的答案的“解决方案”远非完美,这就是我寻找新建议的原因。任何帮助表示赞赏。
【问题讨论】:
-
很难说。您可以尝试不使用 Knp 捆绑包吗?试试这个github.com/mikehaertl/phpwkhtmltopdf。这是我关于包的博客笔记delboy1978uk.wordpress.com/2014/11/24/…
-
@delboy1978uk,感谢您的建议。我用那个捆绑包试过了,但我有完全相同的问题。很高兴知道我可以排除 Knp Bundle。
-
如果输出正确,但生成时间太长,那么我认为问题是 1/ https - 对等 SSL 验证或 2/ CLI 模式下的 DNS 主机名查找。修复 1/ 尝试将所有链接从
https切换到http。对于 2/ 检查在 CLI 模式下看到 PHP 的服务器名称。不要信任 HTTP 模式 - 可能使用不同的 php.ini / env 变量。最终正确定义 etc/hostname。 -
@lubosdz,
http没有改变。当我在 cli 中使用 PHP(使用php -a)并运行echo gethostname();时,它会显示我的名字server,这与/etc/hostname中的相同。 -
@DirkJ.Faber 1. 由于我们仍然怀疑资产存在问题,您是否尝试过在不包含任何资产的情况下生成 PDF? 2. 从服务器本身检查您的一项资产上的response time from curl。 3. 如果可能,尝试在您呈现的 HTML 页面上直接从
wkhtmltopdfCLI 生成。
标签: php pdf nginx wkhtmltopdf