【问题标题】:Use wkhtmltopdf to set landscape orientation使用 wkhtmltopdf 设置横向
【发布时间】:2013-05-25 20:56:41
【问题描述】:

如何更改使用Wkhtmltopdf 生成的pdf 文件的方向。我在 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。

【问题讨论】:

  • 在 wkhtmltopdf 命令之后传递两个标志-O landscape,例如:wkhtmltopdf -O landscape foobar.html foobar.pdf

标签: php pdf wkhtmltopdf


【解决方案1】:

我觉得这里也值得一提:

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

我的 pdf 总是以纵向显示,非常令人沮丧。

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

【讨论】:

  • 那里有一点逻辑陷阱。如果你的宽度大于你的高度,那么横向会给你看起来像纵向的方向(因为它交换了宽度和高度)。
【解决方案2】:

选项-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-12-25
  • 1970-01-01
  • 2013-02-11
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
相关资源
最近更新 更多