我刚刚发现了 wkhtmltopdf,我正在尝试在 perl CGI 脚本中使用它来生成 PDF。基本上,perl 脚本编写一个 HTML 文件,通过 system() 调用 wkhtmltopdf 创建一个 pdf,然后下载 pdf 并删除临时文件。
open NNN, ">$path_to_files/${file}_pdf.html" or die "can't write file: $!";
print NNN $text;
close NNN;
my @pdfSettings = (
"d:/very/long/path/wkhtmltopdf",
"$path_to_files/${file}_pdf.html",
"$path_to_files/$file.pdf"
);
system(@pdfSettings);
open(DLFILE, '<', "$path_to_files/$file.pdf");
print $q->header(
-type=> 'application/x-download',
-attachment => "$file.pdf",
-filename => "$file.pdf",
'Content-length' => -s "$path_to_files/$file.pdf",
);
binmode DLFILE;
print while <DLFILE>;
close (DLFILE);
unlink("$path_to_files/${file}_pdf.html");
unlink("$path_to_files/${file}.pdf");
这在我的本地服务器上运行良好。但是,当我将它上传到我的公共服务器时,它会创建 pdf 文件,然后因“指定的 CGI 应用程序因未返回一组完整的 HTTP 标头而行为异常”而死掉。
将“print $q->header”移动到 system() 调用之前会导致 pdf 在文件顶部生成 wkhtmltopdf 的控制台输出(“Loading pages (1/6)”等),所以我认为正在发生的事情是 wkhtmltopdf 正在向服务器发送无标题的信息并导致它失败。但是我在 wkhtmltopdf 文档中找不到任何关闭控制台输出的选项,而且我无法找到一种 perl 方法来抑制/重定向该输出。
(是的,我知道 WKHTMLTOPDF.pm,但我在安装它时遇到了我的 ActivePerl 风格的问题,我想尽可能避免切换。)