wordpress mPDF - PHP脚本内的html代码块生成PDF文件

ars1skjm  于 2023-08-03  发布在  WordPress
关注(0)|答案(2)|浏览(128)

如下所示,我需要在html代码中插入PHP脚本,但在文档中没有找到任何演示如何执行此操作的示例。
这可能吗?

  • 我使用WordPress
  • 我使用WooCommerce
  • 我必须打印感谢页。在thankyou页面上,我有一个按钮,我点击并下载文件。
  • 生成PDF文件的PHP文件必须接收动态数据。
  • PHP脚本必须包含在html代码中,如下所示。

当我点击按钮下载PDF文件时,我得到了以下错误消息:

注意:未定义变量:在C:\xamppserver2\htdocs\mrdigital\wp-content\themes\astra-child\testmpdf.phpline 22
致命错误:未捕获错误:在C:\xampppserver 2\htdocs\mrdigital\wp-content\themes\astra-child\testmpdf中调用成员函数get_order_number()on null。#0 {main}抛出C:\xamppserver 2\htdocs\mrdigital\wp-content\themes\astra-child\testmpdf.php在线22

这是第22行:第一个月
这是整个testmpdf.php文件:

<?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();

字符串
我该如何扭转这种局面?

x6yk4ghg

x6yk4ghg1#

您需要在创建wc_get_order之后运行它,并且找到一种简单的方法来获取订单ID。这是我下面提出的解决方案。

<?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 );

字符串

72qzrwbm

72qzrwbm2#

非常感谢你的帮助。我就是这样扭转局面的。

您需要知道,我自定义了我的mPDF Composer安装在我的Template-Child中,而不是在默认根文件夹中。
我必须明确地参考WordPresswp-config.php文件,其他明智的做法,因为我的自定义安装,这永远不会工作.
我必须明确地引用Woocommerce类class-wc-order.php文件。
现在代码如下,我可以接收order对象并操作我需要的东西。
验证码:

<?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']);

个字符

相关问题