Jump to content

Hook for creating pdf from link


Recommended Posts


Hello,

I want make hook, after InvoicePaid by hand (bank invoice) i generate some html table page with stamp, whis page available only for customers and require login in clientarea.

Pdf generation work by link:         

If this link in clientarea pdf created blank

$url = 'https://domain.tld/whmcs/index.php?m=receipt&id=6750;

If url for some page as example

$url = 'https://domain.tld/123.php;

it generate good.

<?php
use WHMCS\Database\Capsule;
use TCPDF;

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

add_hook('InvoicePaid', 1, function($vars) {

    logActivity('Starting InvoicePayment hook for Invoice ID: ' . $vars['invoiceid']);

    if ($invoice->paymentmethod == 'bankinvoice') {
        logActivity('Manual payment detected for Invoice ID: ' . $vars['invoiceid']);

        $invoiceId = $vars['invoiceid'];

        $invoice = Capsule::table('tblinvoices')->where('id', $invoiceId)->first();

        if ($invoice) {
            logActivity('Invoice found for Invoice ID: ' . $invoiceId);

            $client = Capsule::table('tblclients')->where('id', $invoice->userid)->first();

            if ($client) {
                logActivity('Client data retrieved for Client ID: ' . $invoice->userid);

                require_once '/var/www/vhosts/domain.tld/whmcs/vendor/tecnickcom/tcpdf/tcpdf.php';


                $pdf = new TCPDF();
                $pdf->SetCreator(PDF_CREATOR);
                $pdf->SetAuthor('Your Company');
                $pdf->SetTitle('Invoice ' . $invoiceId);
                $pdf->SetSubject('Invoice PDF');

                $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Invoice', 'Your Company');

                $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
                $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

                $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

                $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
                $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
                $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

                $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

                $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

                $pdf->AddPage();

                $url = 'https://domain.tld/whmcs/index.php?m=receipt&id=' . $invoiceId;

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
                    'username' => $client->email,
                    'password' => $client->password
                ]));
                curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
                curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
                $html = curl_exec($ch);

                if (curl_errno($ch)) {
                    logActivity('cURL error: ' . curl_error($ch));
                } else {
                    logActivity('cURL request successful for Invoice ID: ' . $invoiceId);
                }

                curl_close($ch);

                $pdf->writeHTML($html, true, false, true, false, '');

                $uploadsDir = __DIR__ . '/../../uploads/';
                $pdfFilePath = $uploadsDir . 'Invoice_' . $invoiceId . '.pdf';

                $pdf->Output($pdfFilePath, 'F');

                logActivity('PDF generated and saved successfully for Invoice ID: ' . $invoiceId);

                echo 'PDF generated successfully. <a href="' . $pdfFilePath . '">Download PDF</a>';
            } else {
                logActivity('Client data not found for Client ID: ' . $invoice->userid);
            }
        } else {
            logActivity('Invoice not found for Invoice ID: ' . $invoiceId);
        }
    } else {
        logActivity('Non-manual payment detected for Invoice ID: ' . $vars['invoiceid']);
    }
});

I mark the invoice as paid manually in the admin panel. Apparently, authorization is not working correctly. How can I make it request data from the user, the account owner?

Link to comment
Share on other sites

Ok, i need to make request by admin (api) user

I try use this link

https://domain.tld/whmcs/admin/addonmodules.php?module=mymodule&action=printpage&id=10310&username=admin&password=MyPass&accesskey=mykey

But it redirect to admin login page. How i can make request to access this page. If i logged in admin area it working, but i need to make request from hook with admin login and password.

&username=admin&password=MyPass&accesskey=mykey

i find it Access Control - WHMCS Developer Documentation but it not working for me.

Link to comment
Share on other sites

In general, I need to launch a link from my personal account or through the admin panel by specifying the user parameters, I did not find examples of how to do this by specifying the username and password to execute the link. I would be very glad if you share

This link 

https://domain.tld/whmcs/admin/addonmodules.php?module=mymodule&action=printpage&id=10310

or this link

https://domain.tld/whmcs/index.php?m=mymodule&id=10310

from an authorized user.  Is it possible to somehow pass these parameters in a link or what options are there?

Link to comment
Share on other sites

You aren't authenticated in your hooks.  The hooks aren't running as you - they're running as the system. WHMCS doesn't offer a native way of logging in as an admin in a hook.

If you just want to download the invoice, you can include /includes/invoicefunctions.php and run the pdfInvoice($invoiceId) function.

 

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