php 使用 wkhtmltopdf 设置横向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16833528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:46:02  来源:igfitidea点击:

Use wkhtmltopdf to set landscape orientation

phppdfwkhtmltopdf

提问by Daykeras

How I can change the orientation of my pdffile which is generated with Wkhtmltopdf. I invoke it in PHP like following:

如何更改pdf使用Wkhtmltopdf. 我在 PHP 中调用它,如下所示:

$file = fopen("tmp/html/pdfTmp_$numRand.html", 
    "w") or exit("Unable to open file!");
fwrite($file, $html);
fclose($file);

exec("..\library\wkhtmltopdf\wkhtmltopdf " . 
    "tmp/html/pdfTmp_$numRand.html tmp/pdf/pdfTmp_$numRand.pdf");

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=".$nom."_".$residence.".pdf");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize("tmp/pdf/pdfTmp_$numRand.pdf"));
ob_clean();
flush();
readfile("tmp/pdf/pdfTmp_$numRand.pdf");

$htmlcontains my whole page in html, and this opens a temporary file.

$html包含我在 html 中的整个页面,这会打开一个临时文件。

This generates a .pdfin portrait orientation. I know wkhtlktopdf has an option -O landscapeto change orientation, but I don't know where and how I can write this in my PHP script. I'm using Windows 7.

这会生成.pdf纵向的 。我知道 wkhtlktopdf 有一个-O landscape更改方向的选项,但我不知道在哪里以及如何在我的 PHP 脚本中编写它。我正在使用 Windows 7。

回答by Mantas

Option -O landscapewill do the trick.

选项-O landscape可以解决问题。

Just chage

只是改变

exec("..\library\wkhtmltopdf\wkhtmltopdf tmp/html/pdfTmp_$numRand.html tmp/pdf/pdfTmp_$numRand.pdf");

to something like

exec("..\library\wkhtmltopdf\wkhtmltopdf -O landscape tmp/html/pdfTmp_$numRand.html tmp/pdf/pdfTmp_$numRand.pdf");

回答by DamienB

I think this is also worth mentioning here:

我认为这里也值得一提:

I was having trouble with this too but I had set --orientation "Landscape" and had also set the --page-height '210mm' and --page-width '297mm'

我也遇到了这个问题,但我已经设置了 --orientation "Landscape" 并且还设置了 --page-height '210mm' 和 --page-width '297mm'

My pdf's kept coming out in Portrait, very frustrating.

我的 pdf 不断出现在肖像中,非常令人沮丧。

I was being too explicit. Taking out the page-height and page-width options resolved the issue for me

我说得太露骨了。取出页面高度和页面宽度选项为我解决了这个问题