<?php require_once 'vendor/autoload.php'; use Knp\Snappy\Pdf; $snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); // Cabeçalho para o navegador entender que o conteúdo é um PDF header('Content-Type: application/pdf'); // Se quiser forçar o download, descomente a linha abaixo e // modifica o valor do parâmetro filename para o valor desejado //header('Content-Disposition: attachment; filename="schoolofnet-blog-artigos.pdf"'); echo $snappy->getOutput(array('http://www.schoolofnet.com/2015/07/trabalhando-com-repository-no-laravel/', 'http://www.schoolofnet.com/2015/04/como-usar-os-metodos-magicos-no-php/', 'http://www.schoolofnet.com/2015/04/enviando-emails-utilizando-swift-mailer/', 'http://www.schoolofnet.com/2015/04/instalando-e-integrando-apache-com-php-no-windows/')); // Se quiser salvar numa pasta do servidor ao invés mostrar no navegador, // comente a linha do getOutput(), // descomente a linha abaixo e arrume o segundo parâmetro. //$snappy->generate( // [ // 'http://www.schoolofnet.com/2015/07/trabalhando-com-repository-no-laravel/', // 'http://www.schoolofnet.com/2015/04/como-usar-os-metodos-magicos-no-php/', // 'http://www.schoolofnet.com/2015/04/enviando-emails-utilizando-swift-mailer/', // 'http://www.schoolofnet.com/2015/04/instalando-e-integrando-apache-com-php-no-windows/', // ], // '/app/arquivos/son/artigos.pdf' //);
/** * 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.'); }
public function doGetPdf($data, $form) { global $baseDir; $website = $data['Website']; $pdf_creator = new Pdf($baseDir . '\\vendor\\bin\\wkhtmltopdf'); header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="file.pdf"'); echo $pdf_creator->getOutput($website); return $this->redirectBack(); }
<?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'))); });
/** * @param $orderUid * @return string * @throws \Exception */ protected function generatePdfReceipt($orderUid) { $snappy = new Pdf($this->pixie->config->get('parameters.wkhtmltopdf_path')); //$snappy->setOption('cookie', $_COOKIE); $snappy->setOption('viewport-size', '800x600'); $snappy->setOption('toc', false); $snappy->setOption('outline', false); $snappy->setOption('outline-depth', 0); $url = ($_SERVER['HTTPS'] == 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/payment/request_bill/' . $orderUid . '?print=1' . '&code=' . $this->generatePrintCode($orderUid); return $snappy->getOutput($url); }
<?php require_once 'vendor/autoload.php'; use Knp\Snappy\Pdf; $snappy = new Pdf('/usr/local/bin/wkhtmltopdf', ['viewport-size' => '1280x800', 'margin-top' => 0, 'margin-right' => 0, 'margin-bottom' => 0, 'margin-left' => 0]); // Cabeçalho para o navegador entender que o conteúdo é um PDF header('Content-Type: application/pdf'); // Se quiser forçar o download, descomente a linha abaixo e // modifica o valor do parâmetro filename para o valor desejado //header('Content-Disposition: attachment; filename="schoolofnet-blog-home.pdf"'); echo $snappy->getOutput('http://www.schoolofnet.com/blog/'); // Se quiser salvar numa pasta do servidor ao invés mostrar no navegador, // comente a linha do getOutput(), // descomente a linha abaixo e arrume o segundo parâmetro. //$snappy->generate('http://www.schoolofnet.com/blog/', '/app/arquivos/son/home-blog.pdf');
$log->debug("printPage.php - set exe location and options"); $snappy = new Pdf('C://"Program Files/"/wkhtmltopdf/bin/wkhtmltopdf.exe'); $snappy->setOption('page-size', 'A3'); $snappy->setOption('disable-external-links', true); } catch (Exception $e) { $log->error("printPage.php - New PDF Class Exception: " . $e->getMessage()); } if (isset($_POST['url'])) { $urlIn = urldecode($_POST['url']); $url = setPrintEncryptionUrl($urlIn); $log->debug("Use Post to get URL: " . $url); header('Content-Type: application/pdf'); header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 header('Pragma: public'); header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="print_page.pdf"'); try { echo $snappy->getOutput($url); } catch (Exception $e) { $log->error("Exception thrown PDF Generation error: " . $e->getMessage()); } } else { $errorTitle = "Print Page Error"; $log->error("Error Print Operation", "No URL sent from page to print", $pageUrl, null, null); processError("Error Print Operation", "Error Print Operation", $pageUrl, null, null, $errorTitle); exit; }