WKHTMLTOPDF 与 PHP 不接受页面大小的参数


WKHTMLTOPDF with PHP not accepting args for page size?

我正在使用WKHTMLTOPDF作为客户端。我的PHP很粗糙...问题是,我的论点不起作用,在下面的示例中,它们可能是错误的,但没有一个有效......例如缩放、页面大小等。例如,在 switch 语句中,您可以看到 Building 类型。我希望它输出为标准的美国字母类型。--页面大小 A,还是 --页面大小"A",还是 --页面大小"信件"?但无论如何,我没有正确传递参数,因为无论我更改什么(即 --zoom .10)进行测试,都不会改变。任何帮助不胜感激!

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
try {
$mydir = getcwd();
// Figure out the URL
$url = $_GET['url'];
$url = str_replace(';', ' ', $url);
// Figure out the mode
$mode = isset($_GET['mode']) ? $_GET['mode'] : 'standard';
// Figure out the mode
$name = isset($_GET['name']) ? '-' . $_GET['name'] . '-' : '';
// Generate GUID filename
$file = $mydir . '''' . uniqid('', true) . '.pdf';
// Build arguments
$args = '
    --load-error-handling ignore
';
switch($mode) {
    case 'Site';
        $args .= ' --title "Site Report"';
        $filename = 'Site-Report' . $name . '.pdf';
    break;
    case 'Building';
        $args .= ' --title "Building Report" --page-size "A"';
        $filename = 'Building-Report' . $name . '.pdf';
    break;
    case 'standard';
    default;
        $args .= ' --title "Development Report" --zoom .55';
        $filename = 'Development-Web-Report.pdf';
    break;
}

// Build the command
putenv("path=" . getenv("path") . ';"C:'Program Files (x86)'wkhtmltopdf"');
$cmd = escapeshellcmd('CMD /c wkhtmltopdf ' . $url . ' ' . $file);
$com = new COM("WScript.Shell");
$shell = $com->Exec($cmd);
$shell->StdErr->ReadAll;
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename=' . $filename);
    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($file));
    ob_clean();
    flush();
    readfile($file);
    unlink($file);
    exit;
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>

您甚至没有将参数附加到命令字符串。
试试这个:

$cmd = escapeshellcmd('CMD /c wkhtmltopdf ' . $args . ' ' . $url . ' ' . $file);

还很高兴知道,在Windows上,您不能使用绝对路径(如C:')指向html文件,因为它需要url或相对路径,因此您必须使用wkhtmltopdf file:///d:/in.html out.pdf,还要注意/而不是'。即使对于最新版本(0.11.0 rc1)也是如此,尽管将来它可能会支持绝对系统路径以获胜。