WordPress

woocommerce offtopic – PHPWord i DOMPDF: PHP Błąd krytyczny: Nieprzechwycony błąd: Nie znaleziono klasy „Dompdf\Dompdf”

  • 1 kwietnia, 2024
  • 4 min read
woocommerce offtopic – PHPWord i DOMPDF: PHP Błąd krytyczny: Nieprzechwycony błąd: Nie znaleziono klasy „Dompdf\Dompdf”


Próbuję przekonwertować dokument docx na plik PDF na haku woocommerce woocommerce_checkout_update_order_meta i próbowałem utworzyć plik .docx z szablonu i zadziałało dobrze, ale teraz muszę przekonwertować plik docx na format pdf, ponieważ klient chce zapobiec edycji dokumentów. Używam phpworda z innej witryny i przesłałem go do /wp-content/themes/mytheme/word/vendor/phpoffice/phpword/ i dostałem błąd: Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'PDF rendering library or library path has not been defined.'

Próbowałem więc naprawić pobieranie biblioteki DOMPDF do /wp-content/tehems/mytheme/word/vendor/dompdf/dompdf i zaktualizowałem moją funkcję za pomocą tego fragmentu kodu:

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

Cała moja funkcja wygląda zatem następująco:

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.com/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);

Ale to nadal nie pomaga, ale myślę, że jestem o krok do przodu. Teraz otrzymuję to w moim dzienniku debugowania:

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

Teraz już nie wiem jak to dalej zrobić poprawnie, dlatego zwracam się do Was z prośbą o poradę.

Warto przeczytać!  Rozpocznij (lub awansuj) swoją podróż współtwórcy – Wiadomości WordPress


Źródło