webkitpdf.php
[app.webkitpdf] / webkitpdf.php
1 <?php
2
3 // a simple service to run webkitpdf and return the PDF...
4 // note - this will only run on localhost by default...
5
6 // needs pear in the 
7 // test:  http://localhost/webkitpdf.php
8
9 // create a simple file:
10 /*
11 ini_set('include_path', 
12             dirname(__FILE__). ':' . 
13             dirname(__FILE__).'/pear:' . 
14             ini_get('include_path'));
15
16 require_once 'webkitpdf.php';
17 new WebkitPdf();
18 */
19
20
21
22 class WebkitPdf
23 {
24     
25     function WebkitPdf()
26     {
27         if (empty($_REQUEST['url'])) {
28             $this->h404("missing url");
29         }
30         
31         require_once 'System.php';
32         
33         $timeout= System::which('timeout');
34         if (empty($timeout)) {
35             $this->h404("missing timeout");
36         }
37         
38         $xvfb = System::which('xvfb');
39         if (empty($xvfb)) {
40             $this->h404("missing xvfb");
41         }
42         
43         $webkitpdf = System::which('webkitpdf');
44         if (empty($webkitpdf )) {
45             $this->h404("missing webkitpdf (compile it..)");
46         }
47         // max delay 20 seconds.? bit generous..
48         $delay = empty($_REQUEST['delay']) ? 1 : max(20,$_REQUEST['delay']);
49         
50         //?? allow injections?
51         // not yet..
52         
53         $outpr = tempnam(ini_get('session.save_path'), 'webkitTMP');
54         unlink($outpr);
55         $out = $outpr .'.pdf';
56         
57         $cmd = "timeout 30s $xvfb --auto-servernum $webkitpdf  " .
58                 " --url " . escapeshellarg($_REQUEST['url']) . ' ' .
59                 " --pdf " . $out . ' ';
60                 
61         $res = `$cmd`;
62         
63         if (!file_exist($out)) {
64             $this->h404("Failed to create file $cmd  ==> $res");
65         }
66         
67         header('Content-type: application/octet-stream');
68         
69         header("Expires: ");
70         header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
71         header("Pragma: public");     
72         header("Last-Modified: " . gmdate("D, d M Y H:i:s",  $ts) . " GMT");
73
74         header('Content-length: '. filesize($out));
75         header('Content-Disposition: '.$type.'; filename="' . htmlspecialchars($filename).  '"');
76         ini_set('display_errors', 0);
77         
78         $fh = fopen($out, 'rb');
79         //fpassthru($fh);
80         
81         // passthrough seems to have problems -- trying fread
82         while(!feof($fh))
83         {
84             echo @fread($fh, 1024*8);
85             ob_flush();
86             flush();
87         }
88         
89         fclose($fh);
90     
91     
92         unlink($out);
93         exit;
94         
95         
96     }
97     
98     function h404($msg)
99     {
100         
101     }
102     
103 }