Example #1
0
function mpdf_output($wp_content = '', $do_pdf = false, $outputToBrowser = true, $pdfName = '', $templatePath = '')
{
    global $post;
    $pdf_ofilename = $post->post_name . '.pdf';
    if (!empty($pdfName)) {
        $pdf_filename = $pdfName . '.pdf';
    } else {
        $pdf_filename = $pdf_ofilename;
    }
    /**
     * Allow to override the pdf file name
     */
    $pdf_filename = apply_filters('mpdf_output_pdf_filename', $pdf_filename);
    /**
     * Geshi Support
     */
    if (get_option('mpdf_geshi') == true) {
        require_once dirname(__FILE__) . '/geshi.inc.php';
        $wp_content = ParseGeshi($wp_content);
    }
    /**
     * Run the content default filter
     */
    $wp_content = apply_filters('the_content', $wp_content);
    /**
     * Run the mpdf filter
     */
    $wp_content = mpdf_filter($wp_content, $do_pdf);
    if ($do_pdf === false) {
        echo $wp_content;
    } else {
        $cacheDirectory = mpdf_getcachedir();
        if (!is_dir($cacheDirectory . 'tmp')) {
            @mkdir($cacheDirectory . 'tmp');
        }
        define('_MPDF_PATH', dirname(__FILE__) . '/mpdf/');
        define('_MPDF_TEMP_PATH', $cacheDirectory . 'tmp/');
        define('_MPDF_TTFONTDATAPATH', _MPDF_TEMP_PATH);
        require_once _MPDF_PATH . 'mpdf.php';
        global $pdf_margin_left;
        global $pdf_margin_right;
        global $pdf_margin_top;
        global $pdf_margin_bottom;
        global $pdf_margin_header;
        global $pdf_margin_footer;
        global $pdf_html_header;
        global $pdf_html_footer;
        if ($pdf_margin_left !== 0 && $pdf_margin_left == '') {
            $pdf_margin_left = 15;
        }
        if ($pdf_margin_right !== 0 && $pdf_margin_right == '') {
            $pdf_margin_right = 15;
        }
        if ($pdf_margin_top !== 0 && $pdf_margin_top == '') {
            $pdf_margin_top = 16;
        }
        if ($pdf_margin_bottom !== 0 && $pdf_margin_bottom == '') {
            $pdf_margin_bottom = 16;
        }
        if ($pdf_margin_header !== 0 && $pdf_margin_header == '') {
            $pdf_margin_header = 9;
        }
        if ($pdf_margin_footer !== 0 && $pdf_margin_footer == '') {
            $pdf_margin_footer = 9;
        }
        if (empty($pdf_html_header)) {
            $pdf_html_header = false;
        }
        if (empty($pdf_html_footer)) {
            $pdf_html_footer = false;
        }
        global $pdf_orientation;
        if ($pdf_orientation == '') {
            $pdf_orientation = 'P';
        }
        $cp = 'utf-8';
        if (get_option('mpdf_code_page') != '') {
            $cp = get_option('mpdf_code_page');
        }
        $mpdf = new mPDF($cp, 'A4', '', '', $pdf_margin_left, $pdf_margin_right, $pdf_margin_top, $pdf_margin_bottom, $pdf_margin_header, $pdf_margin_footer, $pdf_orientation);
        $mpdf->SetUserRights();
        $mpdf->title2annots = false;
        //$mpdf->annotMargin = 12;
        $mpdf->use_embeddedfonts_1252 = true;
        // false is default
        $mpdf->SetBasePath($templatePath);
        //Set PDF Template if it's set
        global $pdf_template_pdfpage;
        global $pdf_template_pdfpage_page;
        global $pdf_template_pdfdoc;
        if (isset($pdf_template_pdfdoc) && $pdf_template_pdfdoc != '') {
            $mpdf->SetImportUse();
            $mpdf->SetDocTemplate($templatePath . $pdf_template_pdfdoc, true);
        } else {
            if (isset($pdf_template_pdfpage) && $pdf_template_pdfpage != '' && isset($pdf_template_pdfpage_page) && is_numeric($pdf_template_pdfpage_page)) {
                $mpdf->SetImportUse();
                $pagecount = $mpdf->SetSourceFile($templatePath . $pdf_template_pdfpage);
                if ($pdf_template_pdfpage_page < 1) {
                    $pdf_template_pdfpage_page = 1;
                } else {
                    if ($pdf_template_pdfpage_page > $pagecount) {
                        $pdf_template_pdfpage_page = $pagecount;
                    }
                }
                $tplId = $mpdf->ImportPage($pdf_template_pdfpage_page);
                $mpdf->UseTemplate($tplId);
            }
        }
        $user_info = get_userdata($post->post_author);
        $mpdf->SetAuthor($user_info->first_name . ' ' . $user_info->last_name . ' (' . $user_info->user_login . ')');
        $mpdf->SetCreator('wp-mpdf');
        //The Header and Footer
        global $pdf_footer;
        global $pdf_header;
        $mpdf->startPageNums();
        // Required for TOC use after AddPage(), and to use Headers and Footers
        if ($pdf_html_header) {
            $mpdf->SetHTMLHeader($pdf_header);
        } else {
            $mpdf->setHeader($pdf_header);
        }
        if ($pdf_html_footer) {
            $mpdf->SetHTMLFooter($pdf_footer);
        } else {
            $mpdf->setFooter($pdf_footer);
        }
        if (get_option('mpdf_theme') != '' && file_exists($templatePath . get_option('mpdf_theme') . '.css')) {
            //Read the StyleCSS
            $tmpCSS = file_get_contents($templatePath . get_option('mpdf_theme') . '.css');
            $mpdf->WriteHTML($tmpCSS, 1);
        }
        //My Filters
        require_once dirname(__FILE__) . '/myfilters.inc.php';
        $wp_content = mpdf_myfilters($wp_content);
        if (get_option('mpdf_debug') == true) {
            if (!is_dir(dirname(__FILE__) . '/debug/')) {
                mkdir(dirname(__FILE__) . '/debug/');
            }
            file_put_contents(dirname(__FILE__) . '/debug/' . get_option('mpdf_theme') . '_' . $pdf_ofilename . '.html', $wp_content);
        }
        //die($wp_content);
        $mpdf->WriteHTML($wp_content);
        /**
         * Allow to process the pdf by an 3th party plugin
         */
        do_action('mpdf_output', $mpdf, $pdf_filename);
        if (get_option('mpdf_caching') == true) {
            file_put_contents(mpdf_getcachedir() . get_option('mpdf_theme') . '_' . $pdf_ofilename . '.cache', $post->post_modified_gmt);
            $mpdf->Output(mpdf_getcachedir() . get_option('mpdf_theme') . '_' . $pdf_ofilename, 'F');
            if ($outputToBrowser == true) {
                $mpdf->Output($pdf_filename, 'I');
            }
        } else {
            if ($outputToBrowser == true) {
                $mpdf->Output($pdf_filename, 'I');
            }
        }
    }
}