1

I'm using WKHTMLTOPDF for a client. My PHP is quite rough... The problem is, my arguments aren't working, and in the below example they could be wrong, but none are working... such as zoom, page-size, etc. So for example, in the switch statement, you can see the Building type. I want this to output as a standard US Letter type. --page-size A, or is it --page-size "A", or --page-size "Letter"? But regardless, I'm not passing the arguments properly, because no matter what I change, (i.e. --zoom .10) for testing, nothing changes. Any help is appreciated!

<?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();
}

?>
user1447679
  • 3,076
  • 7
  • 32
  • 69
  • 1./ Does built CMD work if you run it directly in console? 2./ Why don't you use `exec` for running CMD instead of COM ? – Glavić Sep 10 '13 at 17:53

1 Answers1

0

You are not even attaching your arguments to command string.
Try this:

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

Also good to know that on Windows you cannot use absolute paths (like C:\) to point to a html file since it expects url's or relative paths, so you must use wkhtmltopdf file:///d:/in.html out.pdf, also note the / instead of \. This is true even for the latest version (0.11.0 rc1), although in future it may support absolute system paths for win.

Ivan Hušnjak
  • 3,493
  • 3
  • 20
  • 30