
掌握ChatGPT插件與自定義GPT
您的 HTML 模板應包括:
要整理您的發票,請使用基本的 HTML 組件。例如:
def generate_html_invoice(items, customer_info, subtotal, total_vat):
"""
Generates the HTML content for the invoice.
"""
items_html = "".join(
f"<tr><td>{item['description']}</td><td>{item['quantity']}</td><td>${item['unit_price']}</td></tr>"
for item in items
)
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>Invoice</title>
<!-- Styles here -->
</head>
<body>
<h1>Invoice #{customer_info['invoice_number']}</h1>
<p>Date: {customer_info['date']}</p>
<!-- Customer details here -->
<h2>Invoice Details</h2>
<table>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Unit Price</th>
</tr>
{items_html}
</table>
<h3>Summary</h3>
<p>Subtotal: ${subtotal}</p>
<p>VAT: ${total_vat}</p>
<p>Total: ${subtotal + total_vat}</p>
</body>
</html>
"""
return html_content
此模板可根據品牌或格式需求進一步自定義,其簡潔的結構和布局更適合創建專業的賬單。
您可以將其另存為 HTML 文件以便后續使用。
為了確保增值稅稅率的正確應用,請使用 VATLayer API。下面是一個用于計算增值稅的簡單 Python 腳本:
def calculate_vat(amount, country_code):
"""
Calculates the VAT amount using the VATLayer API's 'price' endpoint.
Args:
amount (float): The original amount before VAT.
country_code (str): The country code for which the VAT is calculated.
Returns:
float: The VAT amount.
"""
url = f"http://apilayer.net/api/price?access_key={VATLAYER_API_KEY}&amount={amount}&country_code={country_code}"
try:
response = requests.get(url)
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
data = response.json()
if data.get('success'):
price_excl_vat = data.get('price_excl_vat', 0)
price_incl_vat = data.get('price_incl_vat', 0)
return price_incl_vat - price_excl_vat
else:
raise ValueError("Error calculating VAT. API response unsuccessful.")
except requests.RequestException as e:
print(f"An error occurred: {e}")
raise
此腳本演示了如何將用于增值稅計算的 VATLayer API 包含在 Python 應用程序中。
使用 PDFLayer API 將 HTML 發票轉換為 PDF。以下是在 Python 中實現的方法:
def convert_html_to_pdf(html_content, output_filename='invoice.pdf'):
"""
Converts the given HTML content to a PDF using the PDFLayer API.
"""
url = "http://api.pdflayer.com/api/convert"
params = {
'access_key': PDFLAYER_API_KEY,
}
data = {
'document_html': html_content,
'page_size': 'A4'
}
try:
response = requests.post(url, params=params, data=data)
response.raise_for_status()
if response.headers.get('Content-Type', '').lower() == 'application/pdf':
with open(output_filename, 'wb') as f:
f.write(response.content)
else:
print("Error: Received non-PDF response.")
print(response.content.decode('utf-8')) # For debugging
except requests.RequestException as e:
print(f"An error occurred: {e}")
raise
此示例演示了如何將 HTML 內容發送到 PDFLayer API 并接收 PDF 文件,是應用程序的主要部分:
def update_and_convert_html_to_pdf(items, customer_info, country_code, output_filename='invoice.pdf'):
"""
Updates the invoice HTML with given details, calculates VAT, and converts it to a PDF.
"""
subtotal = sum(item['quantity'] * item['unit_price'] for item in items)
total_amount = calculate_vat(subtotal, country_code)
total_vat = total_amount - subtotal
html_content = generate_html_invoice(items, customer_info, subtotal, total_vat)
convert_html_to_pdf(html_content, output_filename)
# Example usage
items = [
{"description": "Product 1", "quantity": 2, "unit_price": 50},
{"description": "Service 1", "quantity": 1, "unit_price": 80}
]
customer_info = {
"invoice_number": "12345",
"date": "2023-11-29",
"name": "John Doe",
"address": "1234 Street, City, Country"
}
update_and_convert_html_to_pdf(items, customer_info, 'GB')
以下是正在運行的應用程序:
您可以從此?GitHub 存儲庫中找到完整的項目源代碼。此外,歡迎閱讀我們的最新文章,了解如何使用我們強大的 API 服務自動化執行各種業務任務:
將企業的獨特身份融入日常文書工作中,打造一致且令人難忘的客戶體驗,是專業發票不可或缺的一部分。APILayer 的 PDFLayer 和 VATLayer API 提供了超越簡單模板設計的全面定制的可能性,允許企業將其品牌完美融入到每張發票中。
借助PDFLayer API,企業可以輕松地在發票上添加個性化的品牌元素。這包括添加您公司的專屬Logo,使用與您品牌風格相匹配的字體,以及運用品牌特有的色彩方案。這樣的個性化設計不僅讓發票成為交易憑證,更是企業品牌實力和對細節追求的一種展現。
除了視覺上的個性化,這些API還賦予了企業自定義發票樣式和布局的能力。您可以確保發票上的詳細信息(如分項費用、客戶信息、備注等)清晰且一致地呈現,同時與品牌信息保持高度一致。無論是追求簡潔現代的極簡風格,還是需要詳盡信息展示的全面布局,API都能靈活適應各種設計需求。
借助這些強大的定制工具,企業能夠輕松生成既實用又能提升品牌知名度和客戶信任度的發票。以往,這種程度的定制可能需要依賴復雜且昂貴的軟件解決方案,但現在,通過PDFLayer和VATLayer API簡單、通用的接口,這一切都變得觸手可及。
結合 PDFLayer 和 VATLayer API,可以快速、準確地創建定制且專業的賬單。這些工具不僅節省時間,還能提供比人工方法更高的精度和個性化展示。采用這些技術能夠顯著提高公司的運營效率和財務準確性。
原文鏈接:https://blog.apilayer.com/html-to-pdf-api-invoice-generation/