private function snappy($url) { $bin = PHP_INT_SIZE === 8 ? 'bin/wkhtmltopdf-amd64' : 'bin/wkhtmltopdf-i386'; $snappy = new \Knp\Snappy\Pdf(Config::get('local_root') . '/' . VENDOR_DIR . $bin); $snappy->setOption('print-media-type', true); $snappy->setOption('disable-javascript', true); $snappy->setOption('lowquality', false); $snappy->setOption('load-error-handling', 'ignore'); // Use $snappy->getOptions() to see all possible options // @see http://wkhtmltopdf.org/usage/wkhtmltopdf.txt // d($snappy->getOptions());exit; return $snappy->getOutput($url); }
/** * @return bool */ public function AjaxMessageGetPdfFromHtml() { $oAccount = $this->getAccountFromParam(); if ($oAccount) { $sSubject = (string) $this->getParamValue('Subject', ''); $sHtml = (string) $this->getParamValue('Html', ''); $sFileName = $sSubject . '.pdf'; $sMimeType = 'application/pdf'; $sSavedName = 'pdf-' . $oAccount->IdAccount . '-' . md5($sFileName . microtime(true)) . '.pdf'; include_once PSEVEN_APP_ROOT_PATH . 'libraries/other/CssToInlineStyles.php'; $oCssToInlineStyles = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles($sHtml); $oCssToInlineStyles->setEncoding('utf-8'); $oCssToInlineStyles->setUseInlineStylesBlock(true); $sExec = \CApi::DataPath() . '/system/wkhtmltopdf/linux/wkhtmltopdf'; if (!file_exists($sExec)) { $sExec = \CApi::DataPath() . '/system/wkhtmltopdf/win/wkhtmltopdf.exe'; if (!file_exists($sExec)) { $sExec = ''; } } if (0 < strlen($sExec)) { $oSnappy = new \Knp\Snappy\Pdf($sExec); $oSnappy->setOption('quiet', true); $oSnappy->setOption('disable-javascript', true); $oSnappy->generateFromHtml($oCssToInlineStyles->convert(), $this->ApiFileCache()->generateFullFilePath($oAccount, $sSavedName), array(), true); return $this->DefaultResponse($oAccount, __FUNCTION__, array('Name' => $sFileName, 'TempName' => $sSavedName, 'MimeType' => $sMimeType, 'Size' => (int) $this->ApiFileCache()->fileSize($oAccount, $sSavedName), 'Hash' => \CApi::EncodeKeyValues(array('TempFile' => true, 'AccountID' => $oAccount->IdAccount, 'Name' => $sFileName, 'TempName' => $sSavedName)))); } } return $this->FalseResponse($oAccount, __FUNCTION__); }
/** * @param $paper_size * @param $orientation * @param $margins * @param $html * @param $output * @param string $fileName */ public static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $margins, $html, $output, $fileName) { require_once 'packages/snappy/src/autoload.php'; $config = CRM_Core_Config::singleton(); $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath); $snappy->setOption("page-width", $paper_size[2] . "pt"); $snappy->setOption("page-height", $paper_size[3] . "pt"); $snappy->setOption("orientation", $orientation); $snappy->setOption("margin-top", $margins[1] . $margins[0]); $snappy->setOption("margin-right", $margins[2] . $margins[0]); $snappy->setOption("margin-bottom", $margins[3] . $margins[0]); $snappy->setOption("margin-left", $margins[4] . $margins[0]); $pdf = $snappy->getOutputFromHtml($html); if ($output) { return $pdf; } else { CRM_Utils_System::setHttpHeader('Content-Type', 'application/pdf'); CRM_Utils_System::setHttpHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"'); echo $pdf; } }
<?php if (!isset($_GET['url'])) { die('No URL specified'); } if (!isset($_GET['filename'])) { die('No filename specified'); } require_once 'vendor/autoload.php'; $url = filter_var($_GET['url'], FILTER_SANITIZE_URL); $filename = $string = preg_replace('/[^ \\w]+/', '', $_GET['filename']); file_put_contents('log/requests.log', PHP_EOL . date('c') . ' - ' . $url . ' - ' . $filename, FILE_APPEND); $root = $_SERVER['DOCUMENT_ROOT'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']); $bin = PHP_INT_SIZE === 8 ? '/vendor/bin/wkhtmltopdf-amd64' : '/vendor/bin/wkhtmltopdf-i386'; $snappy = new \Knp\Snappy\Pdf($root . $bin); $snappy->setOption('print-media-type', true); $snappy->setOption('disable-javascript', true); $snappy->setOption('lowquality', false); // Use $snappy->getOptions() to see all possible options // @see http://wkhtmltopdf.org/usage/wkhtmltopdf.txt // d($snappy->getOptions());exit; header("Content-Type: application/pdf"); header("Content-Disposition: attachment; filename='" . $filename . ".pdf'"); file_put_contents('log/requests.log', ' - OK', FILE_APPEND); echo $snappy->getOutput($url);
<?php error_reporting(E_ERROR & ~E_NOTICE); $root = dirname(__DIR__); $loader = (require $root . '/vendor/autoload.php'); $loader->add('', $root . '/classes/'); $pixie = new \App\Pixie(); $pixie->bootstrap($root); $snappy = new \Knp\Snappy\Pdf($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); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="receipt_' . $order->uid . '_' . date('Y.m.d') . '.pdf"'); echo $snappy->getOutput('http://google.com');
static function _html2pdf_wkhtmltopdf($paper_size, $orientation, $html, $output, $fileName) { require_once 'packages/snappy/src/autoload.php'; $config = CRM_Core_Config::singleton(); $snappy = new Knp\Snappy\Pdf($config->wkhtmltopdfPath); $snappy->setOption("page-width", $paper_size[2] . "pt"); $snappy->setOption("page-height", $paper_size[3] . "pt"); $snappy->setOption("orientation", $orientation); $pdf = $snappy->getOutputFromHtml($html); if ($output) { return $pdf; } else { header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="' . $fileName . '"'); echo $pdf; } }