Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-9355-fixed-1688147944694.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Fixed
---

Only request logo once for invoice pdf ([#9355](https://github.com/linode/manager/pull/9355))
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,15 @@ export const BillingActivityPanel = (props: Props) => {
pdfLoading.add(id);

getAllInvoiceItems(invoiceId)
.then((invoiceItems) => {
.then(async (invoiceItems) => {
pdfLoading.delete(id);

const result = printInvoice(account!, invoice, invoiceItems, taxes);
const result = await printInvoice(
account!,
invoice,
invoiceItems,
taxes
);

if (result.status === 'error') {
pdfErrors.add(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ export const InvoiceDetail = () => {
requestData();
}, []);

const printInvoicePDF = (
const printInvoicePDF = async (
account: Account,
invoice: Invoice,
items: InvoiceItem[]
) => {
const taxes =
flags[getShouldUseAkamaiBilling(invoice.date) ? 'taxes' : 'taxBanner'];
const result = printInvoice(account, invoice, items, taxes);
const result = await printInvoice(account, invoice, items, taxes);

setPDFGenerationError(result.status === 'error' ? result.error : undefined);
};
Expand Down
34 changes: 29 additions & 5 deletions packages/manager/src/features/Billing/PdfGenerator/PdfGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
InvoiceItem,
Payment,
} from '@linode/api-v4/lib/account';
import axios from 'axios';
import jsPDF from 'jspdf';
import { splitEvery } from 'ramda';
import { ADDRESSES } from 'src/constants';
Expand All @@ -13,14 +14,14 @@ import formatDate from 'src/utilities/formatDate';
import { getShouldUseAkamaiBilling } from '../billingUtils';
import AkamaiLogo from './akamai-logo.png';
import {
PdfResult,
createFooter,
createInvoiceItemsTable,
createInvoiceTotalsTable,
createPaymentsTable,
createPaymentsTotalsTable,
dateConversion,
pageMargin,
PdfResult,
} from './utils';

const baseFont = 'helvetica';
Expand Down Expand Up @@ -164,13 +165,25 @@ const addTitle = (doc: jsPDF, y: number, ...textStrings: Title[]) => {
doc.setFont(baseFont, 'normal');
};

export const printInvoice = (
// M3-6177 only make one request to get the logo
const getAkamaiLogo = () => {
return axios
.get(AkamaiLogo, { responseType: 'blob' })
.then((res) => {
return URL.createObjectURL(res.data);
})
.catch(() => {
return AkamaiLogo;
});
};
Comment on lines +169 to +178
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same thing could be done with fetch, which removes the dependency on axios. But this is optional because the entire repo basically depends on Axios

Suggested change
const getAkamaiLogo = () => {
return axios
.get(AkamaiLogo, { responseType: 'blob' })
.then((res) => {
return URL.createObjectURL(res.data);
})
.catch(() => {
return AkamaiLogo;
});
};
const getAkamaiLogo = () => {
return fetch(AkamaiLogo)
.then(async (res) => {
const blob = await res.blob();
return URL.createObjectURL(blob);
})
.catch(() => {
return AkamaiLogo;
});
};


export const printInvoice = async (
account: Account,
invoice: Invoice,
items: InvoiceItem[],
taxes: FlagSet['taxBanner'] | FlagSet['taxes'],
timezone?: string
): PdfResult => {
): Promise<PdfResult> => {
try {
const itemsPerPage = 12;
const date = formatDate(invoice.date, {
Expand Down Expand Up @@ -217,9 +230,20 @@ export const printInvoice = (
? taxes?.provincial_tax_ids?.[account.state]
: undefined;

const AkamaiLogoURL = await getAkamaiLogo();

// Create a separate page for each set of invoice items
itemsChunks.forEach((itemsChunk, index) => {
doc.addImage(AkamaiLogo, 'JPEG', 160, 10, 120, 40, undefined, "MEDIUM");
doc.addImage(
AkamaiLogoURL,
'JPEG',
160,
10,
120,
40,
undefined,
'MEDIUM'
);

const leftHeaderYPosition = addLeftHeader(
doc,
Expand Down Expand Up @@ -277,7 +301,7 @@ export const printPayment = (
});
doc.setFontSize(10);

doc.addImage(AkamaiLogo, 'JPEG', 160, 10, 120, 40, undefined, "MEDIUM");
doc.addImage(AkamaiLogo, 'JPEG', 160, 10, 120, 40, undefined, 'MEDIUM');

const leftHeaderYPosition = addLeftHeader(
doc,
Expand Down