### Install pdf-invoice with npm
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Use npm to install the pdf-invoice library. This is the standard package manager for Node.js.
```bash
npm install @h1dd3nsn1p3r/pdf-invoice
```
--------------------------------
### Install pdf-invoice with yarn
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Use yarn to install the pdf-invoice library. Yarn is an alternative package manager for JavaScript.
```bash
yarn add @h1dd3nsn1p3r/pdf-invoice
```
--------------------------------
### Install pdf-invoice with pnpm
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Use pnpm to install the pdf-invoice library. pnpm is a performant package manager for Node.js.
```bash
pnpm add @h1dd3nsn1p3r/pdf-invoice
```
--------------------------------
### Example Payload Data Structure
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
This object defines the data structure required for generating a PDF invoice. It includes company, customer, invoice details, items, and optional QR code and note configurations.
```javascript
const payload = {
company: {
logo: "", // Optional. SVG logo of your company.
name: "Festrol Corp.",
address: "1711 W. El Segundo Blvd, Hawthorne, Canada - 90250",
phone: "Tel: (+11) 245 543 903",
email: "Mail: hello@festrol.io",
website: "Web: https://www.festrolcorp.io",
taxId: "Tax ID: 1234567890", // Optional.
bank: "IBAN: 1234567890AC", // Optional.
},
customer: {
name: "John Doe",
company: "Xero Inc.", // Optional.
address: "1234 Main Street, New York, NY 10001",
phone: "Tel: (555) 555-5555",
email: "Mail: joe@example.com",
taxId: "Tax ID: 1234567890", // Optional.
},
invoice: {
number: 1721, // String or number.
date: "25/12/2023", // Default is current date.
dueDate: "25/12/2023", // Default is current date.
status: "Paid!",
locale: "es-ES", // BCP 47 language tag. Default is "en-US".
currency: "EUR", // ISO 4217 currency code. Default is "USD".
path: "./invoice.pdf", // Required. Path where you would like to generate the PDF file.
fee: 10, // Amount of fee to be added to the invoice (not percentage)
orderDiscount: 10, // Amount of order discount (not percentage)
},
items: [
{
name: "Cloud VPS Server - Starter Plan",
quantity: 1,
price: 400,
discount: 19, // Percentage of discount.
tax: 0, // Tax percentage. Default is 0.
},
{
name: "Domain Registration - example.com",
quantity: 1,
discount: 19, // Percentage of discount.
price: 20,
tax: 0, // Tax percentage. Default is 0.
},
{
name: "Maintenance Charge - Yearly",
quantity: 1,
discount: 0, // Percentage of discount.
price: 300,
tax: 0, // Tax percentage. Default is 0.
},
],
qr: {
data: "https://www.festrolcorp.io",
width: 100, // Default is 50.
},
note: {
text: "Thank you for your business.",
italic: false, // Default is true.
},
};
```
--------------------------------
### Importing TypeScript Types
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Demonstrates how to import TypeScript types from the library's global definition file for use in your project.
```typescript
import type {
CompanyInfo,
CustomerInfo,
InvoiceInfo,
ItemInfo,
QRInfo,
InvoicePayLoad,
} from "@h1dd3nsn1p3r/pdf-invoice/global.d.ts";
```
--------------------------------
### Generate Complete Invoice with PDFInvoice
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
This TypeScript snippet demonstrates how to generate a complete PDF invoice using the PDFInvoice library. It includes setting up company and customer details, itemized lists with taxes and discounts, fees, a QR code, and custom styling. Ensure the output directory exists before running.
```typescript
import { PDFInvoice } from "@h1dd3nsn1p3r/pdf-invoice";
import path from "path";
import fs from "fs";
async function generateInvoice() {
// Ensure output directory exists
const outputDir = path.join(__dirname, "invoices");
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const invoiceNumber = Date.now();
const payload = {
company: {
name: "Digital Solutions Ltd.",
address: "500 Tech Park Boulevard\nAustin, TX 78701",
phone: "Tel: +1 (512) 555-8000",
email: "Mail: accounts@digitalsolutions.com",
website: "Web: https://digitalsolutions.com",
taxId: "Tax ID: 74-5555555",
bank: "Bank: Chase | Account: ****4521"
},
customer: {
name: "Sarah Williams",
company: "Williams Creative Agency",
address: "789 Design District\nMiami, FL 33101",
phone: "Tel: +1 (305) 555-3000",
email: "Mail: sarah@williamscreative.com"
},
invoice: {
number: invoiceNumber,
date: new Date().toLocaleDateString("en-GB"),
dueDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toLocaleDateString("en-GB"),
status: "Due in 30 days",
locale: "en-US",
currency: "USD",
path: path.join(outputDir, `invoice-${invoiceNumber}.pdf`),
fees: [
{ label: "Rush Delivery", amount: 150, operation: "+" }
],
orderDiscount: 75
},
items: [
{ name: "Brand Identity Design Package", quantity: 1, price: 3500, tax: 8.25, discount: 0 },
{ name: "Website Redesign - 10 Pages", quantity: 1, price: 8000, tax: 8.25, discount: 5 },
{ name: "SEO Optimization Setup", quantity: 1, price: 1200, tax: 8.25, discount: 0 },
{ name: "Social Media Templates (Set of 20)", quantity: 2, price: 400, tax: 8.25, discount: 10 }
],
qr: {
data: `https://pay.digitalsolutions.com/invoice/${invoiceNumber}`,
width: 60
},
note: "Thank you for choosing Digital Solutions! Payment can be made via bank transfer or credit card using the QR code above. For questions, contact accounts@digitalsolutions.com"
};
const config = {
style: {
font: "Helvetica",
fontSize: 10,
lineHeight: 1.8,
color: "#1a1a1a"
}
};
try {
const invoice = new PDFInvoice(payload, config);
const generatedPath = await invoice.create();
console.log(`✅ Invoice successfully generated: ${generatedPath}`);
return generatedPath;
} catch (error) {
console.error("❌ Failed to generate invoice:", error.message);
throw error;
}
}
generateInvoice();
```
--------------------------------
### Generate PDF Invoice
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Initializes the PDFInvoice class with a payload and creates the PDF. The create method returns a promise that resolves with the PDF file path.
```javascript
const { PDFInvoice } = require("@h1dd3nsn1p3r/pdf-invoice");
const handleInvoice = async (): Promise => {
const payload = {
// Prepare payload.
};
/**
* Create the invoice.
*/
const invoice = new PDFInvoice(payload);
const pdf = await invoice.create(); // Returns promise, await it.
console.log(pdf); // Full path to the PDF file.
};
handleInvoice();
```
--------------------------------
### Import PDFInvoice using require
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Import the PDFInvoice class using the CommonJS `require` syntax. This is common in Node.js environments.
```javascript
const { PDFInvoice } = require("@h1dd3nsn1p3r/pdf-invoice");
```
--------------------------------
### Create a PDF Invoice with Default Settings
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
Generates a PDF invoice using the PDFInvoice class with a provided payload. Ensure all necessary company, customer, invoice, and item details are included in the payload. The output path is specified within the invoice details.
```typescript
import { PDFInvoice } from "@h1dd3nsn1p3r/pdf-invoice";
import path from "path";
const payload = {
company: {
logo: '', // Optional SVG logo
name: "Acme Corporation",
address: "123 Business Ave, Suite 100\nNew York, NY 10001",
phone: "Tel: (555) 123-4567",
email: "Mail: billing@acme.com",
website: "Web: https://acme.com",
taxId: "Tax ID: 12-3456789",
bank: "IBAN: US12345678901234567890"
},
customer: {
name: "Jane Smith",
company: "Smith Industries",
address: "456 Client Street\nLos Angeles, CA 90001",
phone: "Tel: (555) 987-6543",
email: "Mail: jane@smithindustries.com",
taxId: "Tax ID: 98-7654321"
},
invoice: {
number: 2024001,
date: "15/01/2024",
dueDate: "15/02/2024",
status: "Due",
locale: "en-US",
currency: "USD",
path: path.join(__dirname, "invoices/invoice-2024001.pdf"),
fees: [
{ label: "Shipping", amount: 15.00, operation: "+" },
{ label: "Early Payment Discount", amount: 25.00, operation: "-" }
],
orderDiscount: 50
},
items: [
{
name: "Professional Services - Web Development",
quantity: 40,
price: 150,
tax: 8.25,
discount: 10
},
{
name: "Hosting Package - Annual",
quantity: 1,
price: 299,
tax: 8.25,
discount: 0
},
{
name: "SSL Certificate - Wildcard",
quantity: 1,
price: 199,
tax: 0,
discount: 15
}
],
qr: {
data: "https://pay.acme.com/invoice/2024001",
width: 80
},
note: "Payment is due within 30 days. Please include invoice number with your payment. Thank you for your business!"
};
const invoice = new PDFInvoice(payload);
const pdfPath = await invoice.create();
console.log(`Invoice generated: ${pdfPath}`);
// Output: Invoice generated: /path/to/invoices/invoice-2024001.pdf
```
--------------------------------
### Customize Invoice with Configuration Options
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
Generates a PDF invoice with custom labels, fonts, and styles using the PDFInvoice class and a configuration object. This is useful for localization and branding. Ensure font files are correctly path-ed.
```typescript
import { PDFInvoice } from "@h1dd3nsn1p3r/pdf-invoice";
import path from "path";
const payload = {
company: { name: "Empresa ABC S.L." },
customer: { name: "Cliente Juan García" },
invoice: {
number: "FAC-2024-0042",
date: "15/01/2024",
status: "Pagado",
locale: "es-ES",
currency: "EUR",
path: "./factura.pdf"
},
items: [
{ name: "Consultoría técnica", quantity: 8, price: 75, tax: 21 }
]
};
const config = {
string: {
invoice: "F A C T U R A",
refNumber: "Nº Referencia",
date: "Fecha",
dueDate: "Fecha de vencimiento",
status: "Estado",
billTo: "Facturar a",
item: "Concepto",
quantity: "Cantidad",
price: "Precio",
tax: "IVA",
discount: "Descuento",
total: "Total",
subTotal: "Subtotal",
grandTotal: "Total Final"
},
font: {
CustomFont: {
normal: path.join(__dirname, "fonts/opensans-regular.ttf"),
bold: path.join(__dirname, "fonts/opensans-bold.ttf"),
italics: path.join(__dirname, "fonts/opensans-italic.ttf"),
bolditalics: path.join(__dirname, "fonts/opensans-bolditalic.ttf")
}
},
style: {
font: "CustomFont",
fontSize: 11,
lineHeight: 1.6,
color: "#333333"
}
};
const invoice = new PDFInvoice(payload, config);
const result = await invoice.create();
// Generates Spanish-localized invoice with custom font at ./factura.pdf
```
--------------------------------
### QR Code Configuration
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Defines the structure for QR code data and optional width. The data field is required, and width defaults to 50 pixels.
```javascript
const qr = {
ddata: "https://www.festrolcorp.io/", // Required. The data that you want to encode in the QR code.
width: "100", // Optional. Default is 50.
};
```
--------------------------------
### Generate Invoice File Path
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Constructs a file name for the invoice PDF, incorporating the invoice number and a timestamp. It then generates the full path for saving the PDF, including the directory. If 'path' is not supplied, the PDF defaults to 'invoice.pdf' in the current directory.
```javascript
const file = "invoice" + "-#" + 1729 + "-" + new Date().getTime(); // invoice-#1729-1630480000000
const location = path.join(__dirname, "/invoices/" + file + ".pdf");
const invoice = {
path: location, // Required.
};
```
--------------------------------
### Note Field Configuration
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Sets a string value for a note to be included in the invoice. This field is optional.
```javascript
const note = "Thank you for your business.";
```
--------------------------------
### Define Customer Information for Billing
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
The CustomerInfo interface specifies buyer details for the 'Bill To' section. The 'name' field is mandatory; other fields are optional.
```typescript
import type { CustomerInfo } from "@h1dd3nsn1p3r/pdf-invoice/global.d.ts";
const customer: CustomerInfo = {
name: "Robert Johnson",
company: "Johnson & Associates LLC",
address: "2500 Corporate Plaza\nChicago, IL 60601",
phone: "Tel: +1 (312) 555-0200",
email: "Mail: robert@johnsonassoc.com",
taxId: "Tax ID: 36-9876543"
};
```
--------------------------------
### Define Company Information for Invoice Header
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
Use the CompanyInfo interface to provide seller details. Either a logo or name must be present. All other fields are optional.
```typescript
import type { CompanyInfo } from "@h1dd3nsn1p3r/pdf-invoice/global.d.ts";
const company: CompanyInfo = {
logo: ``,
name: "TechStart Inc.",
address: "100 Innovation Drive\nSan Francisco, CA 94102",
phone: "Tel: +1 (415) 555-0100",
email: "Mail: invoices@techstart.io",
website: "Web: https://techstart.io",
taxId: "Tax ID: 94-1234567",
bank: "IBAN: US89370400440532013000"
};
```
--------------------------------
### Define Customer Object Structure
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Defines the structure for a customer object, specifying the required 'name' field and several optional fields such as company, address, phone, email, and tax ID.
```javascript
const customer = {
name: "John Doe", // Required.
company: "Xero Inc.", // Optional.
address: "1234 Main Street, New York,
NY 10001", // Optional.
phone: "Tel: (555) 555-5555", // Optional.
email: "joedeo@example.com", // Optional.
taxId: "Tax ID: 1234567890", // Optional.
};
```
--------------------------------
### Custom Invoice String Configuration
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Allows customization of text strings used in the invoice, such as titles and labels. This is done via the 'string' property within the configuration object.
```javascript
const { PDFInvoice } = require("@h1dd3nsn1p3r/pdf-invoice");
const create = async (): Promise => {
const payload = {
// ....
};
const config = {
// Custom labels.
string: {
invoice: "F A C T U A",
refNumber: "Referencia",
date: "Fecha",
dueDate: "Fecha de vencimiento",
status: "Estado",
billTo: "Facturar a",
item: "Artículo",
quantity: "Cantidad",
price: "Precio",
tax: "Impuesto",
total: "Total",
subTotal: "Subtotal",
grandTotal: "Grand Total", // Added in v1.0.11
},
};
// Create the invoice.
const invoice = new PDFInvoice(payload, config);
const pdf = await invoice.create();
console.log(pdf);
};
```
--------------------------------
### Import PDFInvoice using ES6 import
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Import the PDFInvoice class using ES6 import syntax. This is suitable for modern JavaScript environments and bundlers.
```javascript
import { PDFInvoice } from "@h1dd3nsn1p3r/pdf-invoice";
```
--------------------------------
### Define Invoice Metadata and Settings
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
The InvoiceInfo interface is used for invoice number, dates, payment status, currency, fees, and output path. 'number' and 'path' are required.
```typescript
import type { InvoiceInfo, Fee } from "@h1dd3nsn1p3r/pdf-invoice/global.d.ts";
import path from "path";
const fees: Fee[] = [
{ label: "Express Shipping", amount: 25.00, operation: "+" },
{ label: "Volume Discount", amount: 100.00, operation: "-" },
{ label: "Processing Fee", amount: 2.50, operation: "+" }
];
const invoice: InvoiceInfo = {
label: "INVOICE",
number: "INV-2024-0099",
date: "20/01/2024",
dueDate: "20/02/2024",
status: "Pending Payment",
locale: "en-GB",
currency: "GBP",
path: path.join(process.cwd(), "output", "invoice-0099.pdf"),
fees: fees,
orderDiscount: 50
};
```
--------------------------------
### Payload Address with Line Break
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Demonstrates how to include line breaks within the address field of the payload using the `\n` character. This is useful for formatting multi-line addresses.
```javascript
const payload = {
company: {
name: "Festrol Corp.",
address: "1711 W. El Segundo Blvd, Hawthorne, \n Canada - 90250",
phone: "Tel: (+11) 245 543 903",
email: "Mail: email@yourcompany.com",
},
};
```
--------------------------------
### Invoice Style Configuration
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Configures the visual style of the invoice, including font, font size, line height, and text color. Default font is Helvetica with size 10.
```javascript
const config = {
// ....
style: {
font: "Helvetica", // "Helvetica", "Times", "Courier"
fontSize: 10, // Optional. Default is 10.
lineHeight: 1.8, // Optional. Default is 1.8.
color: "#000000", // Optional. Default is black.
},
};
```
--------------------------------
### Define Individual Invoice Line Items
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
The ItemInfo interface defines each line item on the invoice, including name, quantity, unit price, tax, and discount. Name, quantity, and price are required.
```typescript
import type { ItemInfo } from "@h1dd3nsn1p3r/pdf-invoice/global.d.ts";
const items: ItemInfo[] = [
{
name: "Software License - Enterprise Edition (Annual)",
quantity: 5,
price: 999.00,
tax: 7.5,
discount: 20
},
{
name: "Implementation & Training Package",
quantity: 1,
price: 2500.00,
tax: 7.5,
discount: 0
},
{
name: "Premium Support - 12 Months",
quantity: 5,
price: 199.00,
tax: 7.5,
discount: 10
},
{
name: "Data Migration Service",
quantity: 1,
price: 1500.00,
tax: 0,
discount: 0
}
];
// Item totals calculated as: (quantity * price) - discount% + tax%
```
--------------------------------
### Define Items Array Structure
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Defines the structure for an array of items, where each item object includes required fields like name, quantity, and price, along with optional tax and discount percentages. A single item can also be passed as an object within the array.
```javascript
const items = [
{
name: "Cloud VPS Server - Starter Plan", // Required.
quantity: 1, // Required.
price: 400, // Required.
tax: 0, // Optional. Tax percentage. Default is 0.
discount: 0, // Optional. Item discount percentage. Default is 0.
},
{
name: "Domain Registration - example.com", // Required.
quantity: 1, // Required.
price: 20, // Required.
tax: 0, // Optional. Tax percentage. Default is 0.
discount: 0, // Optional. Item discount percentage. Default is 0.
},
{
name: "Maintenance Charge - Yearly", // Required.
quantity: 1, // Required.
price: 300, // Required.
tax: 0, // Optional. Tax percentage. Default is 0.
discount: 0, // Optional. Item discount percentage. Default is 0.
},
];
```
```javascript
const items = [
{
name: "Cloud VPS Server - Starter Plan", // Required.
quantity: 1, // Required.
price: 400, // Required.
tax: 0, // Optional. Tax percentage. Default is 0.
discount: 0, // Optional. Item discount percentage. Default is 0.
},
];
```
--------------------------------
### Add QR Code to Invoice
Source: https://context7.com/h1dd3nsn1p3r/pdf-invoice/llms.txt
The QRInfo interface allows embedding a QR code, typically for payment URLs or invoice references. The 'data' field is mandatory; 'width' defaults to 50 pixels.
```typescript
import type { QRInfo } from "@h1dd3nsn1p3r/pdf-invoice/global.d.ts";
const qrCode: QRInfo = {
data: "https://payments.example.com/pay?invoice=INV-2024-0099&amount=5432.50",
width: 75
};
// Alternative: encode JSON data for scanning
const qrWithMetadata: QRInfo = {
data: JSON.stringify({
invoiceId: "INV-2024-0099",
amount: 5432.50,
currency: "USD",
dueDate: "2024-02-20"
}),
width: 100
};
```
--------------------------------
### Custom Font Configuration
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Enables the use of custom TTF fonts for the invoice by providing paths to normal, italic, bold, and bold-italic styles. The 'font' option in the style object should reference the custom font name.
```javascript
const config = {
// ....
font: {
Noto: {
normal: path.join(__dirname, "fonts/noto/regular.ttf"),
italics: path.join(__dirname, "fonts/noto/italic.ttf"),
bold: path.join(__dirname, "fonts/noto/bold.ttf"),
bolditalics: path.join(__dirname, "fonts/noto/bold-italic.ttf"),
},
},
style: {
font: "Noto",
fontSize: 10, // Optional. Default is 10.
lineHeight: 1.8, // Optional. Default is 1.8.
color: "#000000", // Optional. Default is black.
},
};
```
--------------------------------
### Define Invoice Object Structure
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Defines the structure for an invoice object, including required and optional fields like number, date, status, locale, currency, fees, and order discount. Defaults are provided for optional fields.
```javascript
const invoice = {
number: 1721, // Required.
date: "25/12/2023", // Optional. Default is current date.
dueDate: "25/12/2023", // Optional. Default is current date.
status: "Paid!", // Optional. Default is "Due pending!".
locale: "es-ES", // Optional. Default is "en-US".
currency: "EUR", // Optional. Default is "USD".
fees: [
{
label: "Delivery Charge",
amount: 10, // Number (not percentage)
operation: "+",
},
{
label: "Payment Processing",
amount: 3.2, // Number (not percentage)
operation: "-",
},
], // Customer and business fees (Optional)
orderDiscount: 10, // Optional. Total order discount (not percentage).
};
```
--------------------------------
### Company Information Structure
Source: https://github.com/h1dd3nsn1p3r/pdf-invoice/blob/development/README.md
Defines the structure for company details within the invoice payload. Includes optional fields like logo, name, address, contact information, and tax/bank details.
```javascript
const company = {
logo: "", // Optional. SVG logo of your company.
name: "Festrol Corp.", // Optional or required if logo is not supplied.
address: "1711 W. El Segundo Blvd, Hawthorne, \n Canada - 90250", // Optional.
phone: "Tel: (+11) 245 543 903", // Optional.
email: "hello@company.com", // Optional.
website: "Web: https://www.festrolcorp.io" // Optional.
taxId: "Tax ID: 1234567890", // Optional.
bank: "IBAN: 1234567890AC", // Optional.
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.