1 <?php
2
3 4 5 6 7
8
9 namespace nk2580\wordsmith\Utillities;
10
11 use \Knp\Snappy\Pdf as Snappy;
12
13 14 15 16 17 18 19
20 class PDF {
21
22 public $snappy;
23
24 public function __construct($path = NULL) {
25 if (!empty($path)) {
26 $pdf_binary_path = $path;
27 } else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
28 $pdf_binary_path = 'C:\wkhtmltopdf\bin\wkhtmltopdf.exe';
29 } else {
30 $pdf_binary_path = '/usr/local/bin/wkhtmltopdf';
31 }
32 $this->snappy = new Snappy($pdf_binary_path);
33 $upload_dir = wp_upload_dir();
34 $this->snappy->setTemporaryFolder($upload_dir['path']);
35 }
36
37 38 39 40 41 42 43 44
45 public function generate($url, $filename, $options = array()) {
46 $this->cleanExport($filename);
47 $this->snappy->generate($url, $filename, $options);
48 return $filename;
49 }
50
51 52 53 54 55 56 57 58
59 public function generateFromHtml($html, $filename, $options = array()) {
60 $this->cleanExport($filename);
61 $this->snappy->generateFromHtml($html, $filename, $options);
62 return $filename;
63 }
64
65 66 67 68 69 70
71 public function output($url, $filename, $options = array()) {
72 header('Content-Type: application/pdf');
73 header('Content-Disposition: attachment; filename="' . $filename . '"');
74 echo $this->snappy->getOutput($url, $options);
75 }
76
77 78 79 80 81 82
83 public function outputFromHtml($html, $filename, $options = array()) {
84 header('Content-Type: application/pdf');
85 header('Content-Disposition: attachment; filename="' . $filename . '"');
86 echo $this->snappy->getOutputFromHtml($html, $options);
87 }
88
89 private function cleanExport($filename) {
90 if (isset($filename) && file_exists($filename)) {
91 unlink($filename);
92 }
93 }
94
95 }
96