public function getPdfContent($name, array $data = array()) { $this->init(); $tplFile = sprintf('%s:%s.pdf.twig', $this->options['tplShortDirectory'], $name); $htmlContent = $this->twig->render($tplFile, $data); $pdfContent = $this->snappy->getOutputFromHtml($htmlContent); return $pdfContent; }
/** * 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('PDF Generator requires a html or file in order to produce output.'); }
<?php require_once 'vendor/knplabs/knp-snappy/src/autoload.php'; use Knp\Snappy\Pdf; $snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); $snappy->setOption('disable-javascript', true); $snappy->setOption('disable-local-file-access', true); $snappy->setOption('orientation', 'Landscape'); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="My NUSMods.com Timetable.pdf"'); echo $snappy->getOutputFromHtml(urldecode($_POST['html']));
/** * Prints the service into PDF * * @param Request $request * @param Response $response * @param array $args * * @return Response */ public function pdf(Request $request, Response $response, array $args) { $entity = $this->em->getRepository($this->entityClass)->find($args['id']); if ($entity) { $ext = pathinfo($request->getRequestUri(), PATHINFO_EXTENSION); $isPdf = $ext === 'pdf'; $content = $this->twig->render($this->views['print'], ['entity' => $entity, 'isPdf' => $isPdf]); if ($isPdf) { $pdf = new Pdf(getenv('WKHTMLTOPDF')); $response->headers->set('Content-Type', 'application/pdf'); // $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s.pdf"', $entity->getId())); $content = $pdf->getOutputFromHtml($content); } $response->setContent($content); return $response; } throw new NotFoundException(); }
protected function _generate() { $snappy = new Pdf($this->binLocation); $this->content = $snappy->getOutputFromHtml($this->content); }
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It is a breeze. Simply tell Lumen the URIs it should respond to | and give it the Closure to call when that URI is requested. | */ use Illuminate\Http\Request; use Knp\Snappy\Pdf; $app->get('/', function () use($app) { echo "Make a post request to either /html or /url"; }); $app->get('/html', function () use($app) { echo "Use post with a base64 encoded html parameter"; }); $app->post('/html', function (Request $request) use($app) { $pdf = new Pdf('xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf'); echo base64_encode($pdf->getOutputFromHtml(base64_decode(file_get_contents('php://input')))); }); $app->get('/url', function () use($app) { echo "Use post with a url parameter"; }); $app->post('/url', function (Request $request) use($app) { $pdf = new Pdf('xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf'); echo base64_encode($pdf->getOutput($request->get('url'))); });
<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; $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.pdf', ['encoding' => 'UTF8']);