CMS  Version 3.9
pdf.inc
Go to the documentation of this file.
1 <?php
7 Fakoli::using("settings", "auto_login");
8 
9 interface PDFConvertor
10 {
11  function coverPage($url);
12  function footer($text, $position = "center", $size = 11);
13  function generate();
14  function screenshot();
15  function preprocessTemplate($template);
16 };
17 
19 {
20  private $pdf;
21  private $footer;
22 
23  function __construct($pdf)
24  {
25  $this->pdf = $pdf;
26  }
27 
33  function coverPage($url)
34  {
35  $this->cover = $url;
36  return $this;
37  }
38 
45  function footer($text, $position = "center", $size = 11)
46  {
47  $this->footer = "--footer-font-size {$size} --footer-{$position} \"{$text}\"";
48  }
49 
50  function generate()
51  {
52  global $config;
53 
54  $exe = Settings::getValue("pdf", "wkhtmltopdf_path");
55  if (!$exe) throw new FakoliException("Convertor path not specified");
56 
57  $uri = $this->pdf->uri;
58 
59  if ($this->pdf->useToken)
60  {
62  $uri = "/action/auto_login/authenticate?token=$token";
63  }
64 
65  $url = "http".($this->pdf->ssl?"s":"")."://".$config["http_host"].$uri;
66 
67  $flags = "";
68  if ($this->pdf->landscape)
69  {
70  $flags = "-O landscape ";
71  }
72 
73  $tmpName = sha1($url . now()) . ".pdf";
74 
75  $footer = ($this->footer) ? $this->footer : "";
76 
77  $output = $this->pdf->output ? $this->pdf->output : (PdfManager::getTempDirectory() . DIRECTORY_SEPARATOR . $tmpName);
78  $cmd = "{$exe} {$footer} {$flags}-q \"{$url}\" {$output}";
79 
80  trace($cmd, 3);
81 
82  $out = array();
83 
84  exec($cmd, $out);
85 
86  trace(implode("\n", $out), 3);
87 
88  $content = file_get_contents($output);
89  if (!$this->pdf->output) unlink($output);
90 
91  return $content;
92  }
93 
94  function screenshot()
95  {
96  return "";
97  }
98 
99  function preProcessTemplate($template)
100  {
101  return $template;
102  }
103 }
104 
106 {
107  private $pdf;
108  private $footer;
109 
110  function __construct($pdf)
111  {
112  $this->pdf = $pdf;
113  }
114 
120  function coverPage($url)
121  {
122  $this->cover = $url;
123  return $this;
124  }
125 
132  function footer($text, $position = "center", $size = 11)
133  {
134  $this->footer = "--footer-font-size {$size} --footer-{$position} \"{$text}\"";
135  }
136 
137  function generate()
138  {
139  global $config;
140 
141  $exe = Settings::getValue("pdf", "chrome_path");
142 
143  if (!$exe) throw new FakoliException("Convertor path not specified");
144 
145  $uri = $this->pdf->uri;
146  $uri = appendToQueryString($uri, "__pdfoutput=1");
147  if ($this->pdf->landscape)
148  {
149  $uri = appendToQueryString($uri, "__pdflayout=landscape");
150  }
151 
152  if ($this->pdf->useToken)
153  {
155  $uri = "/action/auto_login/authenticate?token=$token";
156  }
157 
158  $url = "http".($this->pdf->ssl?"s":"")."://".$config["http_host"].$uri;
159 
160  $flags = "--headless --disable-gpu --no-margins --virtual-time-budget=2000";
161 
162  $tmpName = sha1($url . now()) . ".pdf";
163 
164  $output = $this->pdf->output ? $this->pdf->output : (PdfManager::getTempDirectory() . DIRECTORY_SEPARATOR . $tmpName);
165  $cmd = "{$exe} {$flags} \"{$url}\" --print-to-pdf={$output}";
166 
167  trace($cmd, 3);
168 
169  $out = array();
170 
171  exec($cmd, $out);
172 
173  trace(implode("\n", $out), 3);
174 
175  $content = file_get_contents($output);
176  if (!$this->pdf->output) unlink($output);
177 
178  return $content;
179  }
180 
181  function screenshot()
182  {
183  global $config;
184 
185  $exe = Settings::getValue("pdf", "chrome_screenshot_path");
186 
187  if (!$exe) throw new FakoliException("Convertor path not specified");
188 
189  $uri = $this->pdf->uri;
190  $uri = appendToQueryString($uri, "__pdfoutput=1");
191  if ($this->pdf->landscape)
192  {
193  $uri = appendToQueryString($uri, "__pdflayout=landscape");
194  }
195 
196  if ($this->pdf->useToken)
197  {
199  $uri = "/action/auto_login/authenticate?token=$token";
200  }
201 
202  $url = "http".($this->pdf->ssl?"s":"")."://".$config["http_host"].$uri;
203 
204  $flags = "";
205 
206  $tmpName = sha1($url . now()) . ".png";
207 
208  $output = $this->pdf->output ? $this->pdf->output : (PdfManager::getTempDirectory() . DIRECTORY_SEPARATOR . $tmpName);
209  $cmd = "{$exe} {$flags} \"{$url}\" {$output}";
210 
211  trace($cmd, 3);
212 
213  $out = array();
214 
215  exec($cmd, $out);
216 
217  trace(implode("\n", $out), 3);
218 
219  $content = file_get_contents($output);
220  if (!$this->pdf->output) unlink($output);
221 
222  return $content;
223  }
224 
225  function preProcessTemplate($template)
226  {
227  if (!isset($_GET["__pdfoutput"])) return $template;
228  $layout = ($_GET["__pdflayout"] == "landscape") ? " @page { size: landscape; }" : "";
229  $styles = "<style type='text/css' media='print'>{$layout} @page {margin:0;} body { margin: 1.5cm }</style></head>";
230 
231  $template = preg_replace("/<\\/head>/i", $styles, $template);
232  return $template;
233  }
234 }
235 
237 {
238  private $pdf;
239  private $footer;
240 
241  function __construct($pdf)
242  {
243  $this->pdf = $pdf;
244  }
245 
251  function coverPage($url)
252  {
253  //NOTE: Not available with external processor
254  $this->cover = $url;
255  return $this;
256  }
257 
264  function footer($text, $position = "center", $size = 11)
265  {
266  //TODO: Implement support for this
267  }
268 
269  function generate()
270  {
271  global $config;
272 
273  $handler = Settings::getValue("pdf", "external_PDF_url");
274  if (!$handler)
275  {
276  $handler = $config["external_pdf_url"];
277  }
278 
279  if (!$handler) throw new FakoliException("External PDF convertor URL not specified");
280 
281  $uri = $this->pdf->uri;
282  $uri = appendToQueryString($uri, "__pdfoutput=1");
283  if ($this->pdf->landscape)
284  {
285  $uri = appendToQueryString($uri, "__pdflayout=landscape");
286  }
287 
288  if ($this->pdf->useToken)
289  {
291  $uri = "/action/auto_login/authenticate?token=$token";
292  }
293 
294  $urlparam = "http".($this->pdf->ssl?"s":"")."://".$config["http_host"].$uri;
295 
296  $url = $handler."?url=".urlencode($urlparam);
297 
298  $content = getRemote($url);
299 
300  if ($this->pdf->output)
301  {
302  file_put_contents($this->pdf->output, $content);
303  }
304 
305  return $content;
306  }
307 
308  function screenshot()
309  {
310  global $config;
311 
312  $handler = Settings::getValue("pdf", "external_PNG_url");
313  if (!$handler)
314  {
315  $handler = $config["external_png_url"];
316  }
317 
318  if (!$handler) throw new FakoliException("External PDF convertor URL not specified");
319 
320  $uri = $this->pdf->uri;
321  $uri = appendToQueryString($uri, "__pdfoutput=1");
322  if ($this->pdf->landscape)
323  {
324  $uri = appendToQueryString($uri, "__pdflayout=landscape");
325  }
326 
327  if ($this->pdf->useToken)
328  {
330  $uri = "/action/auto_login/authenticate?token=$token";
331  }
332 
333  $urlparam = "http".($this->pdf->ssl?"s":"")."://".$config["http_host"].$uri;
334 
335  $url = $handler."?url=".urlencode($urlparam);
336 
337  $content = getRemote($url);
338 
339  if ($this->pdf->output)
340  {
341  file_put_contents($this->pdf->output, $content);
342  }
343 
344  return $content;
345  }
346 
347  function preProcessTemplate($template)
348  {
349  if (!isset($_GET["__pdfoutput"])) return $template;
350  $layout = ($_GET["__pdflayout"] == "landscape") ? " @page { size: landscape; }" : "";
351  $styles = "<style type='text/css' media='print'>{$layout} @page {margin:0;} body { margin: 1.5cm }</style></head>";
352 
353  $template = preg_replace("/<\\/head>/i", $styles, $template);
354  return $template;
355  }
356 }
357 
358 class PDF
359 {
360  var $uri;
361  var $ssl;
362  var $output;
363  var $footer;
364  var $landscape = false;
365 
375  function PDF($uri, $ssl = false, $useToken = true, $output = null, $landscape = false)
376  {
377  $this->uri = $uri;
378  $this->ssl = $ssl;
379  $this->useToken = $useToken;
380  $this->output = $output;
381  $this->landscape = $landscape;
382  $this->convertor = PdfManager::createConvertor($this);
383  }
384 
385 
391  function coverPage($url)
392  {
393  $this->convertor->coverPage($url);
394  }
395 
402  function footer($text, $position = "center", $size = 11)
403  {
404  $this->convertor->footer($text, $position, $size);
405  }
406 
407  function generate()
408  {
409  return $this->convertor->generate();
410  }
411 }
412 
414 {
415  var $uri;
416  var $ssl;
417  var $output;
418  var $footer;
419  var $landscape = false;
420 
430  function __construct($uri, $ssl = false, $useToken = true, $output = null, $landscape = false)
431  {
432  $this->uri = $uri;
433  $this->ssl = $ssl;
434  $this->useToken = $useToken;
435  $this->output = $output;
436  $this->landscape = $landscape;
437  $this->convertor = PdfManager::createConvertor($this);
438  }
439 
440 
446  function coverPage($url)
447  {
448  $this->convertor->coverPage($url);
449  }
450 
457  function footer($text, $position = "center", $size = 11)
458  {
459  $this->convertor->footer($text, $position, $size);
460  }
461 
462  function generate()
463  {
464  return $this->convertor->screenshot();
465  }
466 }
467 ?>
$handler
Definition: event_form.inc:62
$out
Definition: page.inc:66
$size
Definition: download.inc:47
static createOneTimeToken($url, $ip_addr=null)
footer($text, $position="center", $size=11)
Definition: pdf.inc:132
__construct($pdf)
Definition: pdf.inc:110
coverPage($url)
Provides a separate URL to use as the cover page of the PDF.
Definition: pdf.inc:120
preProcessTemplate($template)
Definition: pdf.inc:225
preProcessTemplate($template)
Definition: pdf.inc:347
__construct($pdf)
Definition: pdf.inc:241
footer($text, $position="center", $size=11)
Definition: pdf.inc:264
coverPage($url)
Provides a separate URL to use as the cover page of the PDF.
Definition: pdf.inc:251
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
Definition: pdf.inc:359
PDF($uri, $ssl=false, $useToken=true, $output=null, $landscape=false)
Creates a new PDF object configured to generate a PDF from the supplied URI on the local web applicat...
Definition: pdf.inc:375
$footer
Definition: pdf.inc:363
footer($text, $position="center", $size=11)
Definition: pdf.inc:402
$output
Definition: pdf.inc:362
$landscape
Definition: pdf.inc:364
$ssl
Definition: pdf.inc:361
coverPage($url)
Provides a separate URL to use as the cover page of the PDF.
Definition: pdf.inc:391
generate()
Definition: pdf.inc:407
$uri
Definition: pdf.inc:360
static createConvertor($pdf)
Definition: pdf_manager.inc:53
static getTempDirectory()
Definition: pdf_manager.inc:32
$landscape
Definition: pdf.inc:419
__construct($uri, $ssl=false, $useToken=true, $output=null, $landscape=false)
Creates a new PDF object configured to generate a PDF from the supplied URI on the local web applicat...
Definition: pdf.inc:430
coverPage($url)
Provides a separate URL to use as the cover page of the PDF.
Definition: pdf.inc:446
footer($text, $position="center", $size=11)
Definition: pdf.inc:457
generate()
Definition: pdf.inc:462
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
footer($text, $position="center", $size=11)
Definition: pdf.inc:45
preProcessTemplate($template)
Definition: pdf.inc:99
coverPage($url)
Provides a separate URL to use as the cover page of the PDF.
Definition: pdf.inc:33
__construct($pdf)
Definition: pdf.inc:23
$output
Definition: generate.inc:9
global $config
Definition: import.inc:4
coverPage($url)
footer($text, $position="center", $size=11)
preprocessTemplate($template)
$styles
if(! $blog->published||! $blog->enable_rss_feed||!checkRole($blog->allow_read)) $url
Definition: rss.inc:58
if(array_key_exists("HTTP_IF_MODIFIED_SINCE", $_SERVER)) $content
Definition: styles.css.inc:24
$uri
Definition: tearoff.inc:4