function generate_and_download_pdf_invoice() { // Get order ID from request $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0; // Verify nonce if (!isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], 'download_pdf_invoice_' . $order_id)) { wp_die('Security check failed'); } // Get order object $order = wc_get_order($order_id); if (!$order || !is_a($order, 'WC_Order')) { wp_die('Invalid order'); } ob_start(); // Start output buffering to prevent header issues // Load DomPDF require plugin_dir_path(__FILE__) . 'dompdf/vendor/autoload.php'; // Set DomPDF Options $options = new \Dompdf\Options(); $options->set('isRemoteEnabled', true); // Allow remote images // Initialize DomPDF with options $dompdf = new \Dompdf\Dompdf($options); // Get order details $order_number = $order->get_order_number(); $order_date = $order->get_date_created(); $order_date = $order_date ? $order_date->date('Y-m-d H:i:s') : date('Y-m-d H:i:s'); $customer_name = trim($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()); $customer_email = $order->get_billing_email(); $customer_phone = $order->get_billing_phone(); $customer_address = $order->get_formatted_billing_address(); $payment_method = $order->get_payment_method_title(); // e.g., "Credit Card", "PayPal" $order_total = $order->get_total(); // Get the site logo URL $logo_url = get_theme_mod('custom_logo'); $logo_src = wp_get_attachment_image_src($logo_url, 'full'); $logo_image = $logo_src ? $logo_src[0] : ''; // Start HTML $html = ' Order Invoice #' . esc_html($order_number) . '
Logo

Invoice

Name: ' . esc_html($customer_name) . '
Email: ' . esc_html($customer_email) . ' Order No: #' . esc_html($order_number) . '
Phone: ' . esc_html($customer_phone) . ' Date: ' . esc_html($order_date) . '
Address: ' . wp_kses_post($customer_address) . ' Payment: ' . esc_html($payment_method) . '
'; // Add order items foreach ($order->get_items() as $item_id => $item) { $product = $item->get_product(); $product_title = esc_html($item->get_name()); $total = wc_price($item->get_total()); $html .= ''; } $html .= '
Product Prescription / Image Total
' . $product_title; // Get custom options $custom_options = $item->get_meta('_custom_options_full', true); $pres_image = isset($custom_options['prescription_image']) ? $custom_options['prescription_image'] : ''; $prescription_table = isset($custom_options['prescription_table']) ? $custom_options['prescription_table'] : ''; if (!empty($custom_options)) { $html .= '
'; if (is_array($custom_options)) { // Direct access to specific keys if (!empty($custom_options['price'])) { $html .= '- Product Price: ' . $custom_options['price'] . '
'; } if (!empty($custom_options['power_type'])) { $html .= '- Power Type: ' . $custom_options['power_type'] . '
'; } if (!empty($custom_options['lense_name'])) { $html .= '- Lense Name: ' . $custom_options['lense_name'] . '
'; } if (!empty($custom_options['lense_price'])) { $html .= '- Lense Price: ' . $custom_options['lense_price'] . '
'; } if (!empty($custom_options['lense_type'])) { $html .= '- Lense Type: ' . $custom_options['lense_type'] . '
'; } if (!empty($custom_options['tint_color'])) { $html .= '- Tint Color: ' . $custom_options['tint_color'] . '
'; } if (!empty($custom_options['tint_color_name'])) { $html .= '- Tint Color Name: ' . $custom_options['tint_color_name'] . '
'; } if (!empty($custom_options['tint_percentage'])) { $html .= '- Tint Percentage: ' . $custom_options['tint_percentage'] . '
'; } if (!empty($custom_options['default_sunglass_color'])) { $html .= '- Sunglasses Color: ' . $custom_options['default_sunglass_color'] . '
'; } } else if (is_string($custom_options)) { $html .= esc_html($custom_options); } $html .= '
'; } $html .= '
'; // Create a better layout for prescription and image $html .= '
'; // Left side - Prescription table $html .= '
'; if (!empty($prescription_table)) { $html .= '
SPH CYL Axis ADD PD
OD ' . esc_html($prescription_table['od_sph']) . ' ' . esc_html($prescription_table['od_cyl']) . ' ' . esc_html($prescription_table['od_axis']) . ' 60 ' . (isset($prescription_table['pd']) ? esc_html($prescription_table['pd']) : '') . '
OS ' . esc_html($prescription_table['os_sph']) . ' ' . esc_html($prescription_table['os_cyl']) . ' ' . esc_html($prescription_table['os_axis']) . ' ' . esc_html($prescription_table['os_add']) . '
'; } $html .= '
'; // Right side - Prescription image $html .= '
'; if (!empty($pres_image)) { $html .= 'Prescription Image'; } $html .= '
'; $html .= '
'; // End prescription-layout $html .= '
' . $total . '

Total: ' . wc_price($order_total) . '

'; // Load HTML and Generate PDF $dompdf->loadHtml($html); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); // Output PDF for download ob_end_clean(); // Clear buffer before sending PDF $dompdf->stream('invoice-' . $order_number . '.pdf', ['Attachment' => true]); exit; }