Invoice out pdf

Home » Snippets » Invoice out pdf
0

Created with:

Visibility: 

public

Creator: Gnanapragasam Gnanaguna

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/*
Plugin Name: WP Invoice Plugin
Plugin URI: https://example.com
Description: A plugin for generating and displaying invoices. Importen Permalink structure Plain
Version: 1.0
Author: Your Name
Author URI: https://example.com
*/

require_once plugin_dir_path(__FILE__) . 'fpdf/fpdf.php';

// Function to generate the invoice PDF
function generate_invoice_pdf($invoiceData)
{
    try {
        // Create a new FPDF instance
        $pdf = new FPDF();

        // Add a new page
        $pdf->AddPage();

        // Set the font
        $pdf->SetFont('Arial', 'B', 16);

        // Add the invoice title
        $pdf->Cell(0, 10, 'Invoice', 0, 1, 'C');

        // Add the invoice details
        foreach ($invoiceData as $key => $value) {
            if (is_array($value)) {
                // Iterate over nested array fields
                foreach ($value as $nestedKey => $nestedValue) {
                    $pdf->Cell(0, 10, ucfirst($nestedKey) . ': ' . $nestedValue, 0, 1);
                }
            } else {
                $pdf->Cell(0, 10, ucfirst($key) . ': ' . $value, 0, 1);
            }
        }

        // Generate the PDF file name
        $directory = './files2023/';
        $pdfFileName = $directory . 'invoice_' . $invoiceData['invoice_number'] . '.pdf';

        // Output the PDF to a file
        $pdf->Output($pdfFileName, 'F');

        // Return the PDF file name
        return $pdfFileName;

    } catch (Exception $e) {
        // Log or display the error
        error_log('Error generating invoice PDF: ' . $e->getMessage());
        return false;
    }
}

// Function to view the invoice PDF
function view_invoice_pdf($atts)
{
    // Get the invoice data from the shortcode attributes
    $invoiceData = shortcode_atts([
        'invoice_number' => 'INV-001',
        'amount' => 1.00,
        'due_date' => '2022-12-31',
        'bank_data' => [
            'name' => 'Hermex Trade GmbH',
            'address' => 'Ebnatring 35, 8200 Schaffhausen',
            'institute' => 'PostFinance, Mingerstrasse 20, CH-3030 Bern, Schweiz',
            'clearing' => '9000',
            'bic' => 'POFICHBEXXX',
            'postkonto' => '88-719365-3',
            'iban' => 'CH98 0900 0000 8871 9365 3'
        ]
        // Add more invoice data fields as needed
    ], $atts);

    try {
        // Generate the invoice PDF
        $pdfFileName = generate_invoice_pdf($invoiceData);

        if ($pdfFileName) {
            // Generate the PDF export link
            $pdfLink = '<a href="' . $pdfFileName . '" target="_blank">Export as PDF</a>';

            // Generate the HTML view for the invoice
            $htmlView = '
                <div>
                    <h2>Invoice Details</h2>
                    <p>Invoice Number: ' . $invoiceData['invoice_number'] . '</p>
                    <p>Amount: ' . $invoiceData['amount'] . '</p>
                    <p>Due Date: ' . $invoiceData['due_date'] . '</p>
                    <p>Bank Name: ' . $invoiceData['bank_data']['name'] . '</p>
                    <p>Bank Address: ' . $invoiceData['bank_data']['address'] . '</p>
                    <p>Bank Institute: ' . $invoiceData['bank_data']['institute'] . '</p>
                    <p>Bank Clearing: ' . $invoiceData['bank_data']['clearing'] . '</p>
                    <p>Bank BIC: ' . $invoiceData['bank_data']['bic'] . '</p>
                    <p>Bank Postkonto: ' . $invoiceData['bank_data']['postkonto'] . '</p>
                    <p>Bank IBAN: ' . $invoiceData['bank_data']['iban'] . '</p>
                    <p>' . $pdfLink . '</p>
                </div>
            ';

            // Return the HTML view for the invoice
            return $htmlView;
        } else {
            // Handle the case where PDF generation fails
            return 'Error generating PDF. Please try again later.';
        }
    } catch (Exception $e) {
        // Log or display the error
        error_log('Error viewing invoice PDF: ' . $e->getMessage());
        return 'Error viewing PDF. Please try again later.';
    }
}
add_shortcode('invoice', 'view_invoice_pdf');
?>
				

A plugin for generating and displaying invoices. Importen Permalink structure Plain

Register an account to save your snippets or go Pro to get more features.