DennisHermannsen Posted April 16, 2020 Share Posted April 16, 2020 I'm trying to setup a way to download invoices to a directory as soon as they're generated (and there's no functionality in the API for this), but I'm just stuck. I haven't figured out a way to log in as an admin yet. I need to authenticate as an admin to have access to all invoices, and then call dl.php to get the specific invoice. Has anyone had any success with this before? 0 Quote Link to comment Share on other sites More sharing options...
web2008 Posted April 16, 2020 Share Posted April 16, 2020 Can you use a report with a link to each Invoice? 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted April 16, 2020 Author Share Posted April 16, 2020 If that could've been done automatically, yes. We need something that exports every invoice automatically once it's generated. We managed to get it working using curl with COOKIEJAR option set, though. 0 Quote Link to comment Share on other sites More sharing options...
web2008 Posted April 16, 2020 Share Posted April 16, 2020 No, there is no automation in the report. Can I ask why you need to download each invoice? If it's related to accounting, then you don't need it. Â 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted April 16, 2020 Author Share Posted April 16, 2020 It is related to accounting. We could just export them manually but a) it's manual work and b) all of the invoices are exported in the same file. 0 Quote Link to comment Share on other sites More sharing options...
web2008 Posted April 16, 2020 Share Posted April 16, 2020 I have added a "copy to" address in the email template "Invoice Generated", but a report listing all invoices in increasing order, with links to the invoice itself, is more than enough for the accounts, so you can print a copy on request. 0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted April 16, 2020 Author Share Posted April 16, 2020 We thought about that as well. We just need the exported invoice in PDF, not an email with the PDF attached. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted April 16, 2020 Share Posted April 16, 2020 2 hours ago, DennisHermannsen said: We managed to get it working using curl with COOKIEJAR option set, though. I managed to get it too. It seems that I can curl() any WHMCS as admin but there's something strange that is going on with dl.php. At the moment I think I'm seeing the source code of the PDF on screen (see below - I removed streams). The download should start automatically 🤔 See you later. %PDF-1.7 6 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20200416230906+02'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 7 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 5 0 R ] /PZ 1 >> endobj 7 0 obj <> stream endstream endobj 1 0 obj << /Type /Pages /Kids [ 6 0 R ] /Count 1 >> endobj 3 0 obj <> endobj 4 0 obj <> endobj 8 0 obj <> /Length 2532 >> stream endstream endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 4 0 R >> /XObject << /I0 8 0 R /I1 9 0 R >> >> endobj 5 0 obj <> /H /I>> endobj 10 0 obj << /Author (��MyCompany Srl) /Creator (��WHMCS) /Producer (��TCPDF 6.2.17 \(http://www.tcpdf.org\)) /CreationDate (D:20200416230906+02'00') /ModDate (D:20200416230906+02'00') /Trapped /False >> endobj 11 0 obj << /Type /Metadata /Subtype /XML /Length 4242 >> stream endstream endobj 12 0 obj << /Type /Catalog /Version /1.7 /Pages 1 0 R /Names << >> /ViewerPreferences << /Direction /L2R >> /PageLayout /SinglePage /PageMode /UseNone /OpenAction [6 0 R /FitH null] /Metadata 11 0 R /Lang (��en) >> endobj xref 0 13 0000000000 65535 f 0000002648 00000 n 0000013229 00000 n 0000002707 00000 n 0000002813 00000 n 0000013363 00000 n 0000000015 00000 n 0000000483 00000 n 0000002924 00000 n 0000005701 00000 n 0000013579 00000 n 0000013842 00000 n 0000018167 00000 n trailer << /Size 13 /Root 12 0 R /Info 10 0 R /ID [ <940028b904aa9eb0ed56ccf7e798b580> <940028b904aa9eb0ed56ccf7e798b580> ] >> startxref 18391 %%EOF  0 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted April 16, 2020 Author Share Posted April 16, 2020 11 minutes ago, Kian said: At the moment I think I'm seeing the source code of the PDF on screen file_put_contents into a file and save as PDF - that's what we do 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted April 16, 2020 Share Posted April 16, 2020 (edited) Nah my bad, I was printing it instead of echo(). Anyway this works for me: <?php $WHMCSURL = 'http://example.com'; // WHMCS URL (no trailing slash) $adminPath = '/admin'; // Admin Path (no trailing slash) $username = 'admin'; // WHMCS Username $password = '******'; // WHMCS Password (it should work also with chars like `@/\ since I use decode) $invoiceID = '1908'; // The ID of the invoice (tblinvoices.id) you want to download // Login $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $WHMCSURL . $adminPath . '/dologin.php'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . $username . '&password=' . htmlspecialchars_decode($password)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/this/is/not/important/tmp'); $output = curl_exec($ch); // Download PDF curl_setopt($ch, CURLOPT_URL, $WHMCSURL . '/dl.php?' . http_build_query(array('type' => 'i', 'id' => $invoiceID))); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_POSTFIELDS, ''); $output = curl_exec($ch); header('Cache-Control: public'); header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $invoiceID . '.pdf"'); header('Content-Length: '.strlen($output)); echo $output; Edit: I forgot to insert invoiceID variable. Anyway nice idea, I'm going to include it somewhere in my scripts 😋 Edited April 16, 2020 by Kian 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.