/** * @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()); }
/** * 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; }