/** * @param array $userOptions See \Knp\Snappy\Pdf->configure * @param array $variables Variables for use in SSViewer * @return string PDF file */ public function generatePDF($userOptions = array(), $variables = array()) { require_once BASE_PATH . '/vendor/autoload.php'; $defaults = array('enable-javascript' => false); $output = $this->forPDF(); $snappy = new \Knp\Snappy\Pdf(static::get_wkhtmltopdf_binary()); return $snappy->getOutputFromHTML($this->forPDF($variables), array_merge($defaults, $userOptions)); }
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; } }
/** * Gets the 'knp_snappy.pdf' service. * * This service is shared. * This method always returns the same instance of the service. * * @return \Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator A Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator instance. */ protected function getKnpSnappy_PdfService() { $a = new \Knp\Snappy\Pdf('C:\\\\wkhtmltopdf\\\\bin\\\\wkhtmltopdf.exe', array('enable-smart-shrinking' => true, 'lowquality' => false, 'zoom' => 1.25), array()); $a->setTemporaryFolder(__DIR__ . '/snappy'); return $this->services['knp_snappy.pdf'] = new \Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator($a, $this->get('monolog.logger.snappy', ContainerInterface::NULL_ON_INVALID_REFERENCE)); }
public function addDocuments($fileInfo) { $pdf = new \Knp\Snappy\Pdf($this->config->item('pdf_binary')); $image = new \Knp\Snappy\Image($this->config->item('image_binary')); $userP = array($fileInfo['formData']['user_permission']); $deptP = array($fileInfo['formData']['department_permission']); if ($this->session->admin && isset($fileInfo['formData']['file_department'])) { $currentUserDept = $fileInfo['formData']['file_department']; } else { $currentUserDept = $this->session->department; } if ($this->session->admin && isset($fileInfo['formData']['file_owner'])) { $ownerId = $fileInfo['formData']['file_owner']; } else { $ownerId = $this->session->id; } foreach ($fileInfo['fileData'] as $file) { $dbData = array('category' => $fileInfo['formData']['category'], 'owner' => $ownerId, 'realname' => $file['originalName'], 'created' => date('Y-m-d H:i:s'), 'description' => strip_tags($fileInfo['formData']['description']), 'comment' => strip_tags($fileInfo['formData']['comment']), 'status' => 0, 'department' => $currentUserDept, 'default_rights' => 0, 'publishable' => 0, 'location' => $file['newName']); $this->db->insert('documents', $dbData); $fileId = $this->db->insert_id(); foreach ($userP as $userO) { foreach ($userO as $u => $p) { $userPermsData = array('fid' => $fileId, 'uid' => $u, 'rights' => $p); $this->db->insert('user_perms', $userPermsData); } } foreach ($deptP as $deptO) { foreach ($deptO as $dId => $p) { $deptData = array('fid' => $fileId, 'rights' => $p, 'dept_id' => $dId); $this->db->insert('dept_perms', $deptData); } } $userName = $this->session->username; // Add a file history entry $data = array('id' => $fileId, 'modified_on' => date('Y-m-d H:i:s'), 'modified_by' => $userName, 'note' => 'Initial Import', 'revision' => 'current'); $this->db->insert('log', $data); Accesslog_Model::addLogEntry($fileId, 'A'); switch ($file['fileExt']) { case '.html': case '.php': case '.htm': //create PDF from HTML $html = $this->load->file(FCPATH . $this->config->item('dataDir') . $file['newName'], true); $newPdf = $this->config->item('dataDir') . 'pdf/' . $file['rawName'] . '.pdf'; $options = array('viewport-size' => '1250', 'user-style-sheet' => FCPATH . 'assets/css/bootstrap.css', 'load-error-handling' => 'skip'); $pdf->generateFromHtml($html, $newPdf, $options, true); //create thumbnail from HTML $options = array('format' => 'jpg', 'user-style-sheet' => FCPATH . 'assets/css/bootstrap.css', 'load-error-handling' => 'skip'); $newImage = $this->config->item('dataDir') . 'thumbnails/' . $file['rawName'] . '.jpg'; $image->generateFromHtml($html, $newImage, $options, true); break; case '.png': case '.jpg': $config['source_image'] = $this->config->item('dataDir') . $file['newName']; $config['new_image'] = $this->config->item('dataDir') . 'thumbnails/' . $file['newName']; $config['maintain_ratio'] = true; $config['height'] = 200; $this->load->library('image_lib', $config); $this->image_lib->resize(); break; default: break; } $this->emailPeople($fileId); } $msg = array('status' => 'success', 'msg' => 'All files were successfully uploaded.'); return json_encode($msg); }