Jump to content

Rename PDF Generate for Proforma Invoice


Mas-J

Recommended Posts

Hello Everyone,

I activate the proforma invoice feature for invoice.

I want rename the PDF being sent to client if the invoice status is Unpaid become "Proforma Invoice-1900001" instead of the standard name "Invoice-1900001"

So, it will make a differenciation between Unpaid invoice and Paid invoice PDF when client recieved it.

Do you know what file and script should I edit to make it possible ?

Link to comment
Share on other sites

  • 11 months later...
  • 1 month later...

Changing/Translating Filename

The default filename for invoices is Invoice-xxx. For quotes, this is Quote-xxx. For both, xxx is the ID of the invoice or quote. It may be desirable to change the filenames the clients see or translate into other languages. Achieve this in the same way as any text within WHMCS, by using the Language Files. Look for these strings:

$_LANG['invoicefilename'] = "Invoice-";
$_LANG['quotefilename'] = "Quote-";
Link to comment
Share on other sites

On 8/1/2020 at 7:17 PM, zimbahost said:

Changing/Translating Filename

The default filename for invoices is Invoice-xxx. For quotes, this is Quote-xxx. For both, xxx is the ID of the invoice or quote. It may be desirable to change the filenames the clients see or translate into other languages. Achieve this in the same way as any text within WHMCS, by using the Language Files. Look for these strings:


$_LANG['invoicefilename'] = "Invoice-";
$_LANG['quotefilename'] = "Quote-";

Thanks for the suggestion, but this is not the solution for my question actually 🙂

Link to comment
Share on other sites

2 hours ago, zimbahost said:

Sorry I got it wrong, you need two names in the files, one name when the invoice is open and one when the invoice is paid.

this would be so easy for WHMCS to do. themselves.

2 hours ago, zimbahost said:

Cool this idea also had thinking about it before.

I assume the feature request below was from Mas-J ?

https://requests.whmcs.com/topic/change-proforma-file-name-without-eu-vat-addon

I can think of one potential way to do it with a hook (untested - so it's only theoretical) - but doesn't Kian's Billing Extension rename the filename ?? if so, he'll know how to do this.

Link to comment
Share on other sites

There's no way to do that with standard WHMCS. The first solution that comes to mind is boring but should work.

  1. Use EmailPreSend to detect invoice-related emails and abort sending (you can't handle attachments via API)
  2. Re-create the same email directly including phpMailer class
  3. Download the invoice from dl.php file (Admin authentication required) and store it in a PHP variable (eg. $output = curl_exec($ch);)
  4. Query db to retreive invoice status and anything you want
  5. Name the PDF as you wish like for example "I am Sexy 2020 this invoice is Unpaid You are client ID 120 with USD currency.pdf") bold words come from database
  6. echo $output in that file setting haders (application/pdf)
  7. Attach the newly generated file in th email and send it

As I said it is boring.

Edited by Kian
Link to comment
Share on other sites

14 hours ago, zimbahost said:

Sorry I got it wrong, you need two names in the files, one name when the invoice is open and one when the invoice is paid. Cool this idea also had thinking about it before.

No problem, hehe..

 

10 hours ago, brian! said:

this would be so easy for WHMCS to do. themselves.

I assume the feature request below was from Mas-J ?

https://requests.whmcs.com/topic/change-proforma-file-name-without-eu-vat-addon

I can think of one potential way to do it with a hook (untested - so it's only theoretical) - but doesn't Kian's Billing Extension rename the filename ?? if so, he'll know how to do this.

It's not me, but I've vote for it but I think WHMCS didn't consider it since 2 years ago LoL

 

9 hours ago, Kian said:

There's no way to do that with standard WHMCS. The first solution that comes to mind is boring but should work.

  1. Use EmailPreSend to detect invoice-related emails and abort sending (you can't handle attachments via API)
  2. Re-create the same email directly including phpMailer class
  3. Download the invoice from dl.php file (Admin authentication required) and store it in a PHP variable (eg. $output = curl_exec($ch);)
  4. Query db to retreive invoice status and anything you want
  5. Name the PDF as you wish like for example "I am Sexy 2020 this invoice is Unpaid You are client ID 120 with USD currency.pdf") bold words come from database
  6. echo $output in that file setting haders (application/pdf)
  7. Attach the newly generated file in th email and send it

As I said it is boring.

Cool ! Thanks for the insight, however I can't create the script but I'll give this clue to our dev on other custom modules.

Link to comment
Share on other sites

  • 4 years later...

To implement the renaming of the PDF based on the invoice status in WHMCS, you would need to customize the process by using a hook and some custom PHP code. Here's a summary of the steps you can follow based on the suggestions shared in the community:

Use the EmailPreSend Hook:

This hook can be used to detect email-related events before the email is sent. You can check the invoice status in this hook.

Abort Sending the Default Email:

You'll need to stop the default email from being sent (as attachments can't be handled via the API).

Recreate the Email with phpMailer:

After stopping the default email, create a new email using the phpMailer class, which allows you to control email content and attachments.

Download the Invoice Using dl.php:

Use dl.php (with admin authentication) to fetch the invoice PDF. This step requires using cURL to download the invoice content and store it in a PHP variable.

Query the Database to Retrieve Invoice Status:

You'll need to query the database to check the status of the invoice (whether it’s paid or unpaid) and extract other necessary details (such as client ID, currency, etc.).

Rename the PDF Based on the Invoice Status:

After downloading the invoice, rename it based on the status. For example, if the status is "Unpaid," rename the file to "Proforma Invoice-1900001" instead of "Invoice-1900001."

Set Appropriate Headers:

Set headers to indicate the content type as application/pdf and ensure the email contains the newly generated PDF with the custom filename.

Send the Custom Email:

Attach the renamed PDF to the email and send it to the client.

Example Code Outline (Theoretical):
php
Copy
Edit
function customRenameInvoicePDF($vars) {
    $invoice_id = $vars['invoiceid'];
    $invoice_status = getInvoiceStatus($invoice_id);  // Fetch the invoice status
    $client_id = getClientId($invoice_id);  // Fetch the client ID

    $filename = "Invoice-" . $invoice_id;
    if ($invoice_status == "Unpaid") {
        $filename = "Proforma Invoice-" . $invoice_id;
    }

    // Download the invoice using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "path/to/dl.php?invoiceid=$invoice_id");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);

    // Send the email with phpMailer and the renamed file
    sendCustomEmail($client_id, $filename, $output);
}

function sendCustomEmail($client_id, $filename, $pdf_content) {
    // Set up phpMailer and send the email
    $mail = new PHPMailer(true);
    $mail->setFrom('your-email@example.com');
    $mail->addAddress(getClientEmail($client_id));
    $mail->Subject = "Your Invoice: $filename";
    $mail->Body = "Please find your invoice attached.";
    
    // Attach the renamed PDF
    $mail->addStringAttachment($pdf_content, $filename . ".pdf", 'base64', 'application/pdf');
    
    // Send the email
    $mail->send();
}

add_hook('EmailPreSend', 1, 'customRenameInvoicePDF');
Notes:
This solution is theoretical and may require customization based on your WHMCS setup.

You'll need to have a developer implement this solution or modify the code as per your system requirements.

Make sure you have permission to use cURL and phpMailer in your WHMCS environment.

This approach should allow you to differentiate between unpaid and paid invoices by renaming the PDFs appropriately when sending them to clients.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated