| 1 | | <?php |
|---|
| 2 | | /** |
|---|
| 3 | | * The sfDomPDFPlugin uses the domPDF library to convert HTML documents to PDF |
|---|
| 4 | | * Documentation is located at http://trac.symfony-project.com/trac/wiki/sfDomPDFPlugin |
|---|
| 5 | | * |
|---|
| 6 | | */ |
|---|
| 7 | | class sfDomPDFPlugin |
|---|
| 8 | | { |
|---|
| 9 | | /** |
|---|
| 10 | | * The relative path to the dompdf configuration file |
|---|
| 11 | | * |
|---|
| 12 | | * @var string |
|---|
| 13 | | */ |
|---|
| 14 | | private $config_file = 'lib/dompdf/dompdf_config.inc.php'; |
|---|
| 15 | | |
|---|
| 16 | | /** |
|---|
| 17 | | * The variable that will hold our dompdf object |
|---|
| 18 | | * |
|---|
| 19 | | * @var object |
|---|
| 20 | | */ |
|---|
| 21 | | private $dompdf = null; |
|---|
| 22 | | |
|---|
| 23 | | /** |
|---|
| 24 | | * Instantiates our domPDF class. |
|---|
| 25 | | * This constructor will check for the presence of domPDF |
|---|
| 26 | | * You can optionally pass your HTML to this constructor |
|---|
| 27 | | * |
|---|
| 28 | | * @param string $input |
|---|
| 29 | | */ |
|---|
| 30 | | public function __construct($input = null) |
|---|
| 31 | | { |
|---|
| 32 | | // If the configuration cannot be found, throw an error |
|---|
| 33 | | if (!file_exists($this->getConfigFile())) |
|---|
| 34 | | { |
|---|
| 35 | | throw new sfException('The domPDF configuration file could not be found in ' . $this->getConfigFile()); |
|---|
| 36 | | } |
|---|
| 37 | | |
|---|
| 38 | | // Include the configuration file |
|---|
| 39 | | require_once ($this->getConfigFile()); |
|---|
| 40 | | |
|---|
| 41 | | // Instantiate the new DOMPDF class |
|---|
| 42 | | $this->setPDF(new DOMPDF()); |
|---|
| 43 | | |
|---|
| 44 | | // If input is passed in the constructor, set it here |
|---|
| 45 | | if (!empty($input)) $this->setInput($input); |
|---|
| 46 | | |
|---|
| 47 | | // Set the initial paper settings |
|---|
| 48 | | $this->setPaper(); |
|---|
| 49 | | } |
|---|
| 50 | | |
|---|
| 51 | | /** |
|---|
| 52 | | * Set the path to the domPDF configuration file |
|---|
| 53 | | * |
|---|
| 54 | | * @param string $config_file |
|---|
| 55 | | */ |
|---|
| 56 | | public function setConfigFile($config_file) |
|---|
| 57 | | { |
|---|
| 58 | | $this->config_file = $config_file; |
|---|
| 59 | | } |
|---|
| 60 | | |
|---|
| 61 | | /** |
|---|
| 62 | | * Check to see if this system is running a Windows platform |
|---|
| 63 | | * |
|---|
| 64 | | * @return bool |
|---|
| 65 | | */ |
|---|
| 66 | | public function isWindows() |
|---|
| 67 | | { |
|---|
| 68 | | return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? true : false; |
|---|
| 69 | | } |
|---|
| 70 | | |
|---|
| 71 | | /** |
|---|
| 72 | | * Gets the absolute path to the configuration file |
|---|
| 73 | | * |
|---|
| 74 | | * @return string |
|---|
| 75 | | */ |
|---|
| 76 | | public function getConfigFile() |
|---|
| 77 | | { |
|---|
| 78 | | $isWin = $this->isWindows(); |
|---|
| 79 | | |
|---|
| 80 | | if ((!$isWin && substr($this->config_file, 0, 1) !== '/') || ($isWin && substr($this->config_file, 1, 1) !== ':')) |
|---|
| 81 | | { |
|---|
| 82 | | $this->setConfigFile(realpath(dirname(__FILE__)) . '/' . $this->config_file); |
|---|
| 83 | | } |
|---|
| 84 | | |
|---|
| 85 | | return $this->config_file; |
|---|
| 86 | | } |
|---|
| 87 | | |
|---|
| 88 | | /** |
|---|
| 89 | | * Sets the dompdf object |
|---|
| 90 | | * |
|---|
| 91 | | * @param object $object |
|---|
| 92 | | */ |
|---|
| 93 | | public function setPDF($object) |
|---|
| 94 | | { |
|---|
| 95 | | $this->dompdf = $object; |
|---|
| 96 | | } |
|---|
| 97 | | |
|---|
| 98 | | /** |
|---|
| 99 | | * Returns the domPDF object |
|---|
| 100 | | * |
|---|
| 101 | | * @return object |
|---|
| 102 | | */ |
|---|
| 103 | | public function getPDF() |
|---|
| 104 | | { |
|---|
| 105 | | return $this->dompdf; |
|---|
| 106 | | } |
|---|
| 107 | | |
|---|
| 108 | | /** |
|---|
| 109 | | * The HTML input that will be converted to PDF |
|---|
| 110 | | * You can optionally include an actual file instead of a string |
|---|
| 111 | | * |
|---|
| 112 | | * @param string $input |
|---|
| 113 | | * @param boolean $is_string |
|---|
| 114 | | */ |
|---|
| 115 | | public function setInput($input, $is_string = true) |
|---|
| 116 | | { |
|---|
| 117 | | if ( (bool) $is_string === true) |
|---|
| 118 | | { |
|---|
| 119 | | $this->getPDF()->load_html($input); |
|---|
| 120 | | } |
|---|
| 121 | | else |
|---|
| 122 | | { |
|---|
| 123 | | $this->getPDF()->load_html_file($input); |
|---|
| 124 | | } |
|---|
| 125 | | } |
|---|
| 126 | | |
|---|
| 127 | | /** |
|---|
| 128 | | * Define either http:// or https:// to access your host |
|---|
| 129 | | * |
|---|
| 130 | | * @param string $protocol |
|---|
| 131 | | */ |
|---|
| 132 | | public function setProtocol($protocol) |
|---|
| 133 | | { |
|---|
| 134 | | $this->getPDF()->set_protocol($protocol); |
|---|
| 135 | | } |
|---|
| 136 | | |
|---|
| 137 | | /** |
|---|
| 138 | | * The URL to the site which hosts your CSS and images |
|---|
| 139 | | * Do not include http:// or https:// |
|---|
| 140 | | * |
|---|
| 141 | | * @param string $host |
|---|
| 142 | | */ |
|---|
| 143 | | public function setHost($host) |
|---|
| 144 | | { |
|---|
| 145 | | $this->getPDF()->set_host($host); |
|---|
| 146 | | } |
|---|
| 147 | | |
|---|
| 148 | | /** |
|---|
| 149 | | * Sets the base path for which to look for CSS files and images |
|---|
| 150 | | * |
|---|
| 151 | | * @param string $base_path |
|---|
| 152 | | */ |
|---|
| 153 | | public function setBasePath($base_path) |
|---|
| 154 | | { |
|---|
| 155 | | $this->getPDF()->set_base_path($base_path); |
|---|
| 156 | | } |
|---|
| 157 | | |
|---|
| 158 | | /** |
|---|
| 159 | | * Define the type of paper and orientation for domPDF to use |
|---|
| 160 | | * |
|---|
| 161 | | * @param string $paper |
|---|
| 162 | | * @param string $orientation |
|---|
| 163 | | */ |
|---|
| 164 | | public function setPaper($paper = 'letter', $orientation = 'portrait') |
|---|
| 165 | | { |
|---|
| 166 | | $this->getPDF()->set_paper($paper, $orientation); |
|---|
| 167 | | } |
|---|
| 168 | | |
|---|
| 169 | | /** |
|---|
| 170 | | * Converts the HTML string into a PDF |
|---|
| 171 | | * |
|---|
| 172 | | * @return binary |
|---|
| 173 | | */ |
|---|
| 174 | | public function render() |
|---|
| 175 | | { |
|---|
| 176 | | return $this->getPDF()->render(); |
|---|
| 177 | | } |
|---|
| 178 | | |
|---|
| 179 | | /** |
|---|
| 180 | | * Generates the PDF file based on our input |
|---|
| 181 | | * |
|---|
| 182 | | * @return binary |
|---|
| 183 | | */ |
|---|
| 184 | | public function execute() |
|---|
| 185 | | { |
|---|
| 186 | | // Render the output to PDF |
|---|
| 187 | | $this->render(); |
|---|
| 188 | | |
|---|
| 189 | | return $this->getPDF()->output(); |
|---|
| 190 | | } |
|---|
| 191 | | } |