简体   繁体   中英

mPDF - PHP Script inside the html block of code to generate PDF file

hello everyone please,

as follows in the Script, i need to insert PHP Script inside the html code and i didn't find any example in the documentationt that shows how to do that.

Is that possible right?

I use Wordpres
I use Woocommerce
I must print the thankyou Page. On the thankyou page i have a button that i click and download the file.
The PHP file that generate the PDF file must receive dynamic data.
The PHP Script must be included inside the html code as follows.
When i click on the button to download the PDF file, i got the following error message.

Notice: Undefined variable: order in C:\\xampppserver2\\htdocs\\mrdigital\\wp-content\\themes\\astra-child\\testmpdf.php on line 22

Fatal error: Uncaught Error: Call to a member function get_order_number() on null in C:\\xampppserver2\\htdocs\\mrdigital\\wp-content\\themes\\astra-child\\testmpdf.php:22 Stack trace: #0 {main} thrown in C:\\xampppserver2\\htdocs\\mrdigital\\wp-content\\themes\\astra-child\\testmpdf.php on line 22

This is the line 22
$order = $_GET[$order->get_order_number()];

This is the whole testmpdf.php file:

<?php
/**
 * @Author: exame
 * @Last Modified time: 2020-11-05 15:47:01
 */
use Mpdf\Mpdf;

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new Mpdf();

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/custom/temp/dir/path']);

$mpdf = new \Mpdf\Mpdf([
    'mode' => 'utf-8',    
    'orientation' => 'P',
    'format' => 'A4'
]);

$order = $_GET[$order->get_order_number()]; 
$order = $_GET[$order->get_date_created()]; 

$html = '
<div id="pdfprintcontentarea">
                    <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received">Obrigado. Seu pedido foi recebido. Enviamos o comprovante de compra por e-mail para você.</p>
            <p>Tempo de Reserva do Pedido: <b>24 horas</b> - Por favor efetue o pagamento agora antes que você perca a reserva!</p>
            <ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">

            <li class="woocommerce-order-overview__order order">
                
                <strong>$order->get_order_number();</strong>
            </li>
                            
            <li class="woocommerce-order-overview__date date">
                Hora:                   <strong>10:56:01.507644</strong>
            </li>

            <li class="woocommerce-order-overview__date date">                  
                    <strong>wc_format_datetime( $order->get_date_created() );</strong>
            </li>

                
            <li class="woocommerce-order-overview__total total">
                    Total:                  <strong><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">R$</span>5,00</bdi></span></strong>
            </li>

                <li class="woocommerce-order-overview__total total">
                    Status Pedido:                  <strong>Aguardando</strong>
                </li>

                                    <li class="woocommerce-order-overview__payment-method method">
                        Método de pagamento:                        <strong>Transferência Bancária Rápida e Gratuita pelo Aplicativo do Banco</strong>
                    </li>
</div> <!-- <div id="pdfprintcontentarea"> -->
';

//$html = file_get_contents(__DIR__. '/woocommerce/checkout/thankyou.php');
$mpdf->SetProtection(array(),'','MyPassword');
$mpdf->WriteHTML($html,\Mpdf\HTMLParserMode::HTML_BODY);// Load the html
$mpdf->Output('invoice.pdf',"D");
$mpdf->cleanup();

How can i turn around this situation please?

Thank you in Advance!

You need to run it after wc_get_order has been created, and also, find an easy way to get the order id. this is my proposed solution below.

<?php
/**
 * @Author: exame
 * @Last Modified time: 2020-11-05 15:47:01
 */
use Mpdf\Mpdf;

require_once __DIR__ . '/vendor/autoload.php';



function pekky_print_pdf( $order_id ) {
$mpdf = new Mpdf();

$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/custom/temp/dir/path']);

$mpdf = new \Mpdf\Mpdf([
    'mode' => 'utf-8',    
    'orientation' => 'P',
    'format' => 'A4'
]);

$order = wc_get_order( $order_id ); 

$html = '
<div id="pdfprintcontentarea">
                    <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received">Obrigado. Seu pedido foi recebido. Enviamos o comprovante de compra por e-mail para você.</p>
            <p>Tempo de Reserva do Pedido: <b>24 horas</b> - Por favor efetue o pagamento agora antes que você perca a reserva!</p>
            <ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">

            <li class="woocommerce-order-overview__order order">
                
                <strong>$order->get_order_number();</strong>
            </li>
                            
            <li class="woocommerce-order-overview__date date">
                Hora:                   <strong>10:56:01.507644</strong>
            </li>

            <li class="woocommerce-order-overview__date date">                  
                    <strong>wc_format_datetime( $order->get_date_created() );</strong>
            </li>

                
            <li class="woocommerce-order-overview__total total">
                    Total:                  <strong><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">R$</span>5,00</bdi></span></strong>
            </li>

                <li class="woocommerce-order-overview__total total">
                    Status Pedido:                  <strong>Aguardando</strong>
                </li>

                                    <li class="woocommerce-order-overview__payment-method method">
                        Método de pagamento:                        <strong>Transferência Bancária Rápida e Gratuita pelo Aplicativo do Banco</strong>
                    </li>
</div> <!-- <div id="pdfprintcontentarea"> -->
';

//$html = file_get_contents(__DIR__. '/woocommerce/checkout/thankyou.php');
$mpdf->SetProtection(array(),'','MyPassword');
$mpdf->WriteHTML($html,\Mpdf\HTMLParserMode::HTML_BODY);// Load the html
$mpdf->Output('invoice.pdf',"D");
$mpdf->cleanup();
}

// Hook to a woo func.
add_action( 'woocommerce_before_thankyou', 'pekky_print_pdf', 10, 1 );

Thank you so much for the help. This is how i have turned around the situation.

You need to know that i customised my mPDF Composer installation inside my Template-Child and NOT inside the Default Root Folder.

I must definitivily to make reference to the Wordpress wp-config.php file, other wise this never would worked because of my customised installation.

I must definitivily to make reference to the Woocommerce Class class-wc-order.php file.

Now as follows in the code, i can receive the order object and manipulate what i need.

Code:

<?php

// Root Directory WP Config
require_once '/xampppserver2/htdocs/mrdigital/wp-config.php';   

// Root Directory CLASS WC ORDER
require_once '../../plugins/woocommerce/includes/class-wc-order.php';   

// Load MPDF Loader
require_once __DIR__ . '/vendor/autoload.php';

use Mpdf\Mpdf;

$mpdf = new Mpdf();
$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/custom/temp/dir/path']);
$order = new WC_Order($_GET['key']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM