3 // a simple service to run webkitpdf and return the PDF...
4 // note - this will only run on localhost by default...
7 // test: http://localhost/webkitpdf.php
9 // create a simple file:
11 ini_set('include_path',
12 dirname(__FILE__). ':' .
13 dirname(__FILE__).'/pear:' .
14 ini_get('include_path'));
16 require_once 'webkitpdf.php';
27 if (empty($_REQUEST['url'])) {
28 $this->h404("missing url");
31 require_once 'System.php';
33 $timeout= System::which('timeout');
34 if (empty($timeout)) {
35 $this->h404("missing timeout");
38 $xvfb = System::which('xvfb-run');
40 $this->h404("missing xvfb");
43 $webkitpdf = System::which('webkitpdf');
44 if (empty($webkitpdf )) {
45 $this->h404("missing webkitpdf (compile it..)");
47 // max delay 20 seconds.? bit generous..
48 $delay = empty($_REQUEST['delay']) ? 1 : max(20,$_REQUEST['delay']);
50 //?? allow injections?
53 $outpr = tempnam(ini_get('session.save_path'), 'webkitTMP');
55 $out = $outpr .'.pdf';
57 // can take 2-5 minutes...
59 $cmd = "timeout 300s $xvfb --auto-servernum $webkitpdf " .
60 " --url " . escapeshellarg($_REQUEST['url']) . ' ' .
61 " --pdf " . $out . ' ';
65 if (!file_exists($out)) {
66 $this->h404("Failed to create file $cmd ==> $res");
69 header('Content-type: application/octet-stream');
72 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
73 header("Pragma: public");
74 header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
76 //header('Content-length: '. filesize($out));
77 header('Content-Disposition: attachment; filename="' . htmlspecialchars(basename($out)). '"');
78 ini_set('display_errors', 0);
80 $fh = fopen($out, 'rb');
94 header("HTTP/1.0 404 Not Found");
95 echo htmlspecialchars($msg);