admin管理员组

文章数量:1122846

I'm trying to convert docx to PDF on woocommerce hook woocommerce_checkout_update_order_meta and I've tried to make .docx from template and it worked well, but now I have to convert the docx to pdf because client wants to prevent edits in the documents. I'm using phpword from another website and uploaded it into /wp-content/themes/mytheme/word/vendor/phpoffice/phpword/ and I was getting error: Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'PDF rendering library or library path has not been defined.'

So I've tried to fix download DOMPDF library into /wp-content/tehems/mytheme/word/vendor/dompdf/dompdf and upgraded my function by this code snippet:

$rendererName = 'DomPDF';
$dompdfcesta = '/www/domains/example/wp-content/themes/mytheme/word/vendor/dompdf';
Settings::setPdfRenderer($rendererName, $dompdfcesta);

So my whole function looks like this:

use \PhpOffice\PhpWord\IOFactory;
use \PhpOffice\PhpWord\Settings;
add_action('woocommerce_checkout_update_order_meta', function($order_id) {
    error_reporting(E_ALL);
    global $wp, $wpdb, $woocommerce, $post; 
    require __DIR__ . '/word/vendor/autoload.php';
    
    $rendererName = 'DomPDF';
    $dompdfcesta = '/www/domains/example/wp-content/themes/mytheme/word/vendor/dompdf';
    Settings::setPdfRenderer($rendererName, $dompdfcesta);
    
    $order = wc_get_order($order_id);
    
    $formatted_billing_address = $order->get_formatted_billing_address();
    $shipping_country = $order->get_shipping_state();
    $currency = $order->get_currency();
      
    $email               = $order->get_billing_email();  
    $phone               = $order->get_billing_phone();
      
    $shipping_address_1  = $order->get_billing_address_1(); 
    $shipping_address_2  = $order->get_billing_address_2();    
    $shipping_city       = $order->get_billing_city();
    $shipping_state      = $order->get_billing_state();
    $shipping_postcode   = $order->get_billing_postcode();
    $shipping_country    = $order->get_billing_country();
    $dateofpurchase = date('j. n. Y', strtotime( $order->get_date_created()));
    
    $result = $wpdb->get_row ( "SELECT order_item_name FROM  {$wpdb->prefix}woocommerce_order_items WHERE order_id = $order_id AND order_item_type = 'line_item'" );
    $ordered_product = $result->order_item_name;
    $fullname = $order->get_billing_first_name() . ' '. $order->get_billing_last_name();
    $fullnamenospace = $order->get_billing_first_name().''.$order->get_billing_last_name();
    $upload_dir = wp_upload_dir();
    $filename = $upload_dir['basedir'] . '/smlouvy/' . $fullnamenospace.'.docx';
    $pdfname = $upload_dir['basedir'] . '/smlouvy/' . $fullnamenospace.'.pdf';
    $template_gold = $upload_dir['basedir'] . '/smlouvy/Potvrzeni-o-uzavreni-smlouvy-gold.docx';
    $template_silver = $upload_dir['basedir'] . '/smlouvy/Potvrzeni-o-uzavreni-smlouvy-silver.docx';
    $template_bronze = $upload_dir['basedir'] . '/smlouvy/Potvrzeni-o-uzavreni-smlouvy-bronze.docx';
    $template_tiket3plus = $upload_dir['basedir'] . '/smlouvy/Potvrzeni-o-uzavreni-smlouvy-tiket3plus.docx';
    $template_tiket10plus = $upload_dir['basedir'] . '/smlouvy/Potvrzeni-o-uzavreni-smlouvy-tiket10plus.docx';
    //var_dump($filename);
    if($ordered_product == 'GOLD') {
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_gold);
        $templateProcessor->setValue('nameorcompany', $fullname);
        $templateProcessor->setValue('street', $shipping_address_1);
        $templateProcessor->setValue('city', $shipping_city);
        $templateProcessor->setValue('zip', $shipping_postcode);
        $templateProcessor->setValue('datepurchase', $dateofpurchase);
        $templateProcessor->saveAs($filename);
        $phpWord = IOFactory::load($filename);
        $pdfWriter = IOFactory::createWriter($phpWord, 'PDF');
        $pdfWriter->save($pdfname);
    } elseif($ordered_product == 'SILVER') {
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_silver);
        $templateProcessor->setValue('nameorcompany', $fullname);
        $templateProcessor->setValue('street', $shipping_address_1);
        $templateProcessor->setValue('city', $shipping_city);
        $templateProcessor->setValue('zip', $shipping_postcode);
        $templateProcessor->setValue('datepurchase', $dateofpurchase);
        $templateProcessor->saveAs($filename);
        $phpWord = IOFactory::load($filename);
        $pdfWriter = IOFactory::createWriter($phpWord, 'PDF');
        $pdfWriter->save($pdfname);
    } elseif($ordered_product == 'BRONZE') {
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_bronze);
        $templateProcessor->setValue('nameorcompany', $fullname);
        $templateProcessor->setValue('street', $shipping_address_1);
        $templateProcessor->setValue('city', $shipping_city);
        $templateProcessor->setValue('zip', $shipping_postcode);
        $templateProcessor->setValue('datepurchase', $dateofpurchase);
        $templateProcessor->saveAs($filename);
        $phpWord = IOFactory::load($filename);
        $pdfWriter = IOFactory::createWriter($phpWord, 'PDF');
        $pdfWriter->save($pdfname);
    } elseif($ordered_product == '3+KURZ') {
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_tiket3plus);
        $templateProcessor->setValue('nameorcompany', $fullname);
        $templateProcessor->setValue('street', $shipping_address_1);
        $templateProcessor->setValue('city', $shipping_city);
        $templateProcessor->setValue('zip', $shipping_postcode);
        $templateProcessor->setValue('datepurchase', $dateofpurchase);
        $templateProcessor->saveAs($filename);
        $phpWord = IOFactory::load($filename);
        $pdfWriter = IOFactory::createWriter($phpWord, 'PDF');
        $pdfWriter->save($pdfname);
    } elseif($ordered_product == '10+KURZ') {
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_tiket10plus);
        $templateProcessor->setValue('nameorcompany', $fullname);
        $templateProcessor->setValue('street', $shipping_address_1);
        $templateProcessor->setValue('city', $shipping_city);
        $templateProcessor->setValue('zip', $shipping_postcode);
        $templateProcessor->setValue('datepurchase', $dateofpurchase);
        $templateProcessor->saveAs($filename);
        $phpWord = IOFactory::load($filename);
        $pdfWriter = IOFactory::createWriter($phpWord, 'PDF');
        $pdfWriter->save($pdfname);
    }
          
      //}

 
}, 999);

But this still not helps, but I think I'm one step forward. Now I getting this in my debug log:

PHP Fatal error:  Uncaught Error: Class "Dompdf\Dompdf" not found in /www/domains/example/wp-content/themes/mytheme/word/vendor/phpoffice/phpword/src/PhpWord/Writer/PDF/DomPDF.php:52

Now I don't know how to continue to do it correctly, so I'm here and looking for any advices.

本文标签: woocommerce offtopicPHPWord and DOMPDF PHP Fatal error Uncaught Error Class quotDompdfDompdfquot not found