21

如何更改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");

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

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

4

2 回答 2

34

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

随便找

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

类似于

exec("..\library\wkhtmltopdf\wkhtmltopdf -O landscape tmp/html/pdfTmp_$numRand.html tmp/pdf/pdfTmp_$numRand.pdf");
于 2013-05-30T10:19:36.737 回答
4

我觉得这里也值得一提:

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

我的 pdf 一直以肖像形式出现,非常令人沮丧。

我说得太直白了。取出 page-height 和 page-width 选项为我解决了这个问题

于 2016-08-31T21:27:13.580 回答