WKHTMLTOPDF与PHP不接受页面大小的args?

时间:2013-09-10 17:49:47

标签: php wkhtmltopdf

我正在为客户端使用WKHTMLTOPDF。我的PHP非常粗糙......问题是,我的论点不起作用,在下面的例子中它们可能是错的,但没有一个正在工作......例如缩放,页面大小等等。例如,在switch语句中,您可以看到Building类型。我希望这个输出为标准的美国字母类型。 - 页面大小A,或者是 - 页面大小“A”,或--page-size“Letter”?但无论如何,我并没有正确地传递论据,因为无论我改变什么,(即 - 缩放.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();
}

?>

1 个答案:

答案 0 :(得分:0)

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

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

另外很高兴知道在Windows上你不能使用绝对路径(如C:\)来指向html文件,因为它需要url或相对路径,所以你必须使用wkhtmltopdf file:///d:/in.html out.pdf,同时注意/代替\。即使对于最新版本(0.11.0 rc1)也是如此,尽管将来它可能支持win的绝对系统路径。