• Hi all,

    I was wondering if you can call wp-mpdf from php?
    I thought on making a ajax callback function to select a (custom)template .

    Is this possible? maybe with mpdf_exec() or mpdf_output()?
    mpdf_output() looks most promising, since you can set the template path. If you can do this you don’t have to change the DB option setting mpdf_theme.

    My first approach works. But it’s ugly: It sets a new value in the DB and then runs the url to get the pdf.

    JS:

    
        <script type="text/javascript">
            jQuery(document).ready(function ($) {
    
                $(".button-pdf-1").click(function (e) {
                    e.preventDefault();
    
                    var data = {
                        'action': 'my_action',
                        'post_id': <?php echo get_the_ID(); ?>,
                        'template': 'rtt_pdf_01'
                    };
    
                    getPdf(data);
    
                });
    
                $(".button-pdf-2").click(function (e) {
                    e.preventDefault();
    
                    var data = {
                        'action': 'my_action',
                        'post_id': <?php echo get_the_ID(); ?>,
                        'template': 'rtt_pdf_02'
                    };
    
                    getPdf(data);
    
                });
    
                function getPdf(data) {
                    jQuery.post(ajaxurl, data, function (response) {
    
                        let url = '<?php echo get_post_permalink( get_the_ID()); ?>?output=pdf';
                        window.location.href = url;
                    });
                }
    
            });
        </script>
    

    php:

    
        add_action('wp_ajax_my_action', 'my_action');
        
        function my_action()
        {
            update_option('mpdf_theme', $_POST['template']);
            die();
        }
    

    ——————-

    Second approach should be less hacky. But I don’t get it to work. This is the current code:
    JS:

    
        <script type="text/javascript">
            jQuery(document).ready(function ($) {
    
                $(".button-pdf-1").click(function (e) {
                    e.preventDefault();
    
                    var data = {
                        'action': 'my_action',
                        'post_id': <?php echo get_the_ID(); ?>,
                        'template': 'rtt_pdf_01'
                    };
    
                    getPdf(data);
    
                });
    
                $(".button-pdf-2").click(function (e) {
                    e.preventDefault();
    
                    var data = {
                        'action': 'my_action',
                        'post_id': <?php echo get_the_ID(); ?>,
                        'template': 'rtt_pdf_02'
                    };
    
                    getPdf(data);
    
                });
    
                function getPdf(data){
                    jQuery.post(ajaxurl, data, function (response) {
    
                        // Should I need this? Plus is this the best way?
                        var blob = new Blob([response]);
                        var link = document.createElement('a');
                        link.href = window.URL.createObjectURL(blob);
                        link.download = "FILENAME_TO_SAVE_WITH_EXTENSION.pdf";
                        link.click();
    
                    });
                }
    
            });
        </script>
    

    php callback:

    
    
        add_action('wp_ajax_my_action', 'my_action');
    
        function my_action()
        {
            global $post, $_GET;
    
            // should I require this? Plus is there an easier way to get the path?
            require_once( rtt_get_current_file_path(__DIR__) ."/../../../../plugins/wp-mpdf/wp-mpdf.php");
    
            //catch current theme to reset it later
            $currentPdfTheme = get_option('mpdf_theme');
    
            // set it, since <code>mpdf_exec()</code> looks for it.
            $_GET['output'] = 'pdf';
    
            // set the desired template
            update_option('mpdf_theme', $_POST['template']);
    
            // to-do, somehow it fetches the first post instead of $_POST['post_id'] value
            $arg = array(
                'include' => [$_POST['post_id']],
            );
            $posts = get_posts( $arg );
    
            foreach ( $posts as $post ) {
    
                query_posts( 'p=' . $_POST['post_id'] );
    
                // Approach 1
                //mpdf_exec( 'false' );
    
                // Approach 2
                $pdf_output = '';
                $outputToBrowser = true;
                $pdfName = 'Export_' . get_the_title(); //could also get slug
                $templatePath = ''; // ? Do I need to set this? Also, I can maybe use this so I dont have to change DB option <code>mpdf_theme</code>?
                mpdf_output( $pdf_output, true, $outputToBrowser, $pdfName, $templatePath );
            }
    
            //reset original theme
            update_option('mpdf_theme', $currentPdfTheme);
    
            die();
        }
    

    Am I on the right track here? How can I successfully get the pdf with php and return/download it client side?

  • The topic ‘How to call wp-mpdf from php? With `mpdf_exec()`?’ is closed to new replies.