/** * Output the PDF as a string. * * @return string The rendered PDF as string * @throws \InvalidArgumentException */ public function output() { if ($this->html) { return $this->snappy->getOutputFromHtml($this->html, $this->options); } if ($this->file) { return $this->snappy->getOutput($this->file, $this->options); } throw new \InvalidArgumentException('Image Generator requires a html or file in order to produce output.'); }
<?php require_once 'vendor/knplabs/knp-snappy/src/autoload.php'; $size = isset($_POST['size']) ? $_POST['size'] : 1; use Knp\Snappy\Image; $snappy = new Image('/usr/local/bin/wkhtmltoimage'); $snappy->setOption('disable-javascript', true); $snappy->setOption('disable-local-file-access', true); $snappy->setOption('quality', 100); $snappy->setOption('zoom', $size); $description = $size == 1 ? 'Normal' : ($size == 2 ? 'Big' : 'Large'); $filename = 'My NUSMods.com Timetable (' . $description . ').jpg'; header('Content-Type: image/jpeg'); header('Content-Disposition: attachment; filename=' . $filename); echo $snappy->getOutputFromHtml(urldecode($_POST['html']));
<thead> <tr> <th>Nome</th> <th>E-mail</th> <th>Telefone</th> </tr> </thead> <tbody> <tr> <td>Fulano da Silva</td> <td>fulano@dasilva.com</td> <td>11 99999-8888</td> </tr> <tr> <td>Sicrano Santos</td> <td>sicrano@gmail.com</td> <td>11 99999-7777</td> </tr> <tr> <td>João das Botas</td> <td>j.botas@empresa.com.br</td> <td>11 99999-6666</td> </tr> </thead> </table> EOD; echo $snappy->getOutputFromHtml($html, ['encoding' => 'UTF8']); // Se quiser salvar numa pasta do servidor ao invés mostrar no navegador, // comente a linha do getOutputFromHtml(), // descomente a linha abaixo e arrume o segundo parâmetro. //$snappy->generateFromHtml($html, 'relatorio.png', ['encoding' => 'UTF8']);