Hy…
I have an invoice template where i render some data and also show logo, The logo is in the media folder where i upload logo and other company information and when i print invocie i get the logo from logo module,
It is getting logo correctly an di checked its path is also correct. When i don’t open print window then on html template it is showing logo correctly but when i add javascript code for opening print window along with invocie data then it is not showing the logo.
Here is my print button.
<button type="submit" class="btn btn-dark col-sm-4 mb-2" onclick="printInvoiceSingleService('{{ service.id }}')"><i class="fas fa-print text-success me-1" data-bs-toggle="tooltip"
title="Print"></i></button>
and this is my javascript code which renders on view function where all invoice data is fetched and render to the invoice template.
<script>
async function printInvoiceSingleService(transaction_id) {
console.log("Print button clicked");
// Make an AJAX request to get the invoice template content
const response = await fetch(`/printInvoiceSingleService/${transaction_id}/`);
const invoiceTemplate = await response.text();
// Open a new tab for printing
const printWindow = window.open('', '_blank');
// Write the invoice template content to the new tab
printWindow.document.open();
printWindow.document.write(invoiceTemplate);
printWindow.document.close();
// Trigger the print functionality for the new tab
printWindow.print();
// Close the new tab after printing
printWindow.close();
}
// Event listener for beforeprint and afterprint
window.addEventListener('beforeprint', () => {
// Save the current URL before printing
window._originalURLBeforePrint = window.location.href;
});
window.addEventListener('afterprint', () => {
// Return to the original URL after printing is done
window.location.href = window._originalURLBeforePrint;
});
</script>
and this is my view function
def printInvoiceSingleService(request, transaction_id):
# Retrieve the transaction data based on the ID
transaction = get_object_or_404(Transactions, id=transaction_id)
invoice_number = transaction_id
# logoData = CompanyLogo.objects.all()
company_logo_object = CompanyLogo.objects.first() # Example: Retrieve the first logo in the database
if not company_logo_object:
# Handle the case when no company logo is found
company_logo_name = 'static/images/digi.png' # Default logo image path
else:
company_logo_name = company_logo_object.companyLogo.name
print("company_logo_name",company_logo_name)
# You can customize this template as per your needs
context = {
'transaction': transaction,
'invoice_number':invoice_number,
# 'logoData':logoData,
'company_logo_name':company_logo_name,
# 'company_logo_object':company_logo_object,
}
print("context",context)
# Render the HTML template for the invoice
return render(request, 'pages/single_service_invoice.html', context)
and i am showing logo in this way on invoice templates
<img src="{{ request.scheme }}://{{ request.get_host }}/media/{{ company_logo_name }}" alt="Company Logo">