/** * @inheritdoc */ protected function convertInternal($html, $outputFileName, $options) { $pageSize = ArrayHelper::remove($options, 'pageSize', 'A4'); $orientation = ArrayHelper::remove($options, 'orientation', 'portrait'); if (empty($options)) { $dompdfOptions = null; } else { $dompdfOptions = new \Dompdf\Options(); foreach ($options as $name => $value) { $dompdfOptions->set($name, $value); } } $dompdf = new \Dompdf\Dompdf($dompdfOptions); $dompdf->setPaper($pageSize, $orientation); $dompdf->loadHtml($html); $dompdf->render(); file_put_contents($outputFileName, $dompdf->output()); }
public function __invoke(Request $req, Response $res) { $school = $req->getAttribute('school'); $appForm = $this->appFormService->findSchoolApplicationForm($school->id); if (null === $appForm) { return $res->withStatus(404); } $html = $this->view->fetch('application_form/pdf.twig', ['school' => $school, 'appForm' => $appForm, 'logo' => base64_encode(file_get_contents(__DIR__ . '/../../public/img/application_form/minedu_logo.jpg')), 'style' => file_get_contents(__DIR__ . '/../../public/css/application_form/pdf.css')]); $pdf = new \Dompdf\Dompdf(['default_paper_size' => 'A4', 'default_font' => 'DejaVu Sans', 'isHtml5ParserEnabled' => true, 'is_remote_enabled' => false]); $pdf->loadHtml($html); $pdf->render(); $filename = 'edulabs_app_form_' . $appForm['id'] . '.pdf'; $str = $pdf->output(); $length = mb_strlen($str, '8bit'); return $res->withHeader('Cache-Control', 'private')->withHeader('Content-type', 'application/pdf')->withHeader('Content-Length', $length)->withHeader('Content-Disposition', 'attachment; filename=' . $filename)->withHeader('Accept-Ranges', $length)->write($str); }
/** * Format a recordset * * @param Garp_Model $model * @param array $rowset * @return string */ public function format(Garp_Model $model, array $rowset) { $html = parent::format($model, $rowset); $dompdf = new Dompdf\Dompdf(); $dompdf->load_html($html); $dompdf->set_paper('a4', 'portrait'); $dompdf->render(); $out = $dompdf->output(); return $out; }
function pdf_create($html, $filename = '') { // need to enable magic quotes for the $magic_quotes_enabled = get_magic_quotes_runtime(); if (!$magic_quotes_enabled) { ini_set('magic_quotes_runtime', TRUE); } $dompdf = new Dompdf\Dompdf(); $dompdf->loadHtml($html); $dompdf->render(); if (!$magic_quotes_enabled) { ini_set('magic_quotes_runtime', $magic_quotes_enabled); } if ($filename != '') { $dompdf->stream($filename . '.pdf'); } else { return $dompdf->output(); } }
/** * Handles the PDF Conversion */ function _print_pdf($html) { global $wp_query; if (isset($wp_query->query_vars['pdf'])) { // convert to PDF $title = wp_strip_all_tags(get_the_title()); $filename = $title . '.pdf'; $cached = PDF_CACHE_DIRECTORY . $title . '-' . substr(md5(get_the_modified_time()), -6) . '.pdf'; // check if we need to generate PDF against cache if (defined('DISABLE_PDF_CACHE') && DISABLE_PDF_CACHE || isset($_SERVER['HTTP_PRAGMA']) && $_SERVER['HTTP_PRAGMA'] == 'no-cache' || !file_exists($cached)) { // we may need more than 30 seconds execution time //set_time_limit(60); // include the library require_once 'vendor/autoload.php'; // html to pdf conversion $dompdf = new Dompdf\Dompdf(); $dompdf->setPaper(defined('DOMPDF_PAPER_SIZE') ? DOMPDF_PAPER_SIZE : DOMPDF_DEFAULT_PAPER_SIZE, defined('DOMPDF_PAPER_ORIENTATION') ? DOMPDF_PAPER_ORIENTATION : 'portrait'); $options = $dompdf->getOptions(); $options->set(array('fontDir' => DOMPDF_FONT_DIR, 'fontCache' => DOMPDF_FONT_CACHE, 'isHtml5ParserEnabled' => DOMPDF_ENABLE_HTML5, 'isRemoteEnabled' => DOMPDF_ENABLE_REMOTE)); // allow setting a different DPI value if (defined('DOMPDF_DPI')) { $options->set(array('dpi' => DOMPDF_DPI)); } $dompdf->setOptions($options); // allow other plugins to filter the html before passing it to dompdf $html = apply_filters('pdf_html_to_dompdf', $html); $dompdf->loadHtml($html); //$dompdf->setBasePath(get_stylesheet_directory_uri()); $dompdf->render(); if (defined('DISABLE_PDF_CACHE') && DISABLE_PDF_CACHE) { //just stream the PDF to user if caches are disabled return $dompdf->stream($filename, array("Attachment" => false)); } // create PDF cache if one doesn't yet exist if (!is_dir(PDF_CACHE_DIRECTORY)) { @mkdir(PDF_CACHE_DIRECTORY); } //save the pdf file to cache file_put_contents($cached, $dompdf->output()); } //read and display the cached file header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="' . $filename . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($cached)); header('Accept-Ranges: bytes'); readfile($cached); } else { // print the HTML raw echo $html; } // kill php after output is complete die; }