Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php wkhtmltopdf:将PDF下载到用户';s HD_Php_Wkhtmltopdf - Fatal编程技术网

Php wkhtmltopdf:将PDF下载到用户';s HD

Php wkhtmltopdf:将PDF下载到用户';s HD,php,wkhtmltopdf,Php,Wkhtmltopdf,如何自动下载(到用户的HD)使用或生成的pdf?你知道的。。用户单击链接(“将此页面下载为PDF”)并将PDF下载到其HD 问候 Javi以下是我在RoR中的做法 filename = "MyNew.pdf" fullpath = "#{RAILS_ROOT}/tmp/charts/#{filename}" # system issues a shell command system "/usr/local/bin/wkhtmltopdf \"http://localhost/page/to/p

如何自动下载(到用户的HD)使用或生成的pdf?你知道的。。用户单击链接(“将此页面下载为PDF”)并将PDF下载到其HD

问候


Javi

以下是我在RoR中的做法

filename = "MyNew.pdf"
fullpath = "#{RAILS_ROOT}/tmp/charts/#{filename}"
# system issues a shell command
system "/usr/local/bin/wkhtmltopdf \"http://localhost/page/to/pdf?download=t\" #{fullpath}"

send_data(File.read(fullpath), :type => 'application/pdf', :filename => filename, :disposition => "attachment;filename=\"#{filename}\"")

PHP:有更好的方法让wkhtmltopdf与PHP一起工作,但是,我喜欢能够打印出我的命令行,这样我就可以更容易地调试生成的页面。这不仅仅是关于代码,而是关于正确的页边距和其他页面细节。此处wkhtmltopdf是web根目录中的二进制文件,边距设置为零,背景关闭:

$do_it=$_SERVER["DOCUMENT_ROOT"]."/wkhtmltopdf --dpi 600 -B 0 -L 0 -R 0 -T 0 --no-background http://".$_SERVER['SERVER_NAME']."/".$filename." ".$_SERVER["DOCUMENT_ROOT"]."/".$pdf_url;
//var_dump($do_it); // uncomment to see your wkhtmltopdf parameters...
$whatever=passthru($do_it);
header('Content-disposition: attachment; filename='.$pdf_url);
header('Content-type: application/pdf');
readfile($pdf_url);
我认为在运行passthru时不会有太多的返回,但是,它会运行您发送的任何消息。 至于标题,将内容类型设置为PDF很重要,否则浏览器将不知道如何处理它

实际上也有一个现成的例子

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');

您是否在询问如何强制下载,而不是简单地在浏览器中打开文件?