Overview
The BridgeClient class provides a low-level interface for communicating with the print bridge service. It handles HTTP requests to the bridge API and provides methods for checking availability, fetching printers, and sending print jobs.
Import
import { BridgeClient } from 'vue-print-it'
Constructor
new BridgeClient ( baseUrl ?: string )
baseUrl
string
default: "'http://localhost:8765'"
Base URL of the bridge service
Properties
Getter that returns the current availability status of the bridge
true - Bridge is available
false - Bridge is not available
null - Status not yet checked
Methods
checkAvailability()
Checks if the bridge service is available and responding.
checkAvailability (): Promise < boolean >
Returns: Promise that resolves to true if bridge is available, false otherwise
Example:
const client = new BridgeClient ()
const isAvailable = await client . checkAvailability ()
if ( isAvailable ) {
console . log ( 'Bridge is ready' )
} else {
console . log ( 'Bridge is not available' )
}
getHealth()
Gets the health status of the bridge service.
getHealth (): Promise < BridgeHealthResponse | null >
Returns: Promise that resolves to bridge health information or null if unavailable
Response Type:
interface BridgeHealthResponse {
status : string
version : string
uptime : number
}
Example:
const client = new BridgeClient ()
const health = await client . getHealth ()
if ( health ) {
console . log ( `Bridge version: ${ health . version } ` )
console . log ( `Uptime: ${ health . uptime } s` )
console . log ( `Status: ${ health . status } ` )
}
getPrinters()
Gets list of available printers from the bridge.
getPrinters (): Promise < BridgePrinter [] >
Returns: Promise that resolves to array of available printers
Response Type:
interface BridgePrinter {
name : string
is_default : boolean
status : string
}
Example:
const client = new BridgeClient ()
const printers = await client . getPrinters ()
printers . forEach ( printer => {
console . log ( ` ${ printer . name } - ${ printer . status } ` )
if ( printer . is_default ) {
console . log ( ' (Default)' )
}
})
print()
Sends content directly to a printer via the bridge.
print ( request : BridgePrintRequest ): Promise < BridgePrintResponse >
Parameters:
request
BridgePrintRequest
required
Print request configuration Name of the printer to use. If not provided, uses system default
Content to print (Base64 encoded for HTML)
Type of content being printed
Number of copies to print (default: 1)
Additional printer-specific options
Returns: Promise that resolves to print response with job information
Response Type:
interface BridgePrintResponse {
success : boolean
job_id : string
message : string
}
Example:
const client = new BridgeClient ()
try {
const response = await client . print ({
printer_name: 'HP LaserJet Pro' ,
content: client . htmlToBase64 ( '<h1>Hello World</h1>' ),
content_type: 'html' ,
copies: 2
})
console . log ( `Print job ${ response . job_id } : ${ response . message } ` )
} catch ( error ) {
console . error ( 'Print failed:' , error )
}
htmlToBase64()
Converts HTML string to Base64 encoding for bridge transmission.
htmlToBase64 ( html : string ): string
Parameters:
Returns: Base64 encoded string
Example:
const client = new BridgeClient ()
const html = '<div><h1>Invoice</h1><p>Total: $100</p></div>'
const encoded = client . htmlToBase64 ( html )
await client . print ({
content: encoded ,
content_type: 'html'
})
Usage Examples
Basic Setup
import { BridgeClient } from 'vue-print-it'
const client = new BridgeClient ( 'http://localhost:8765' )
// Check if bridge is available
const available = await client . checkAvailability ()
console . log ( 'Bridge available:' , available )
Custom Base URL
const client = new BridgeClient ( 'http://192.168.1.100:8765' )
const health = await client . getHealth ()
if ( health ) {
console . log ( `Connected to bridge v ${ health . version } ` )
}
Getting Available Printers
const client = new BridgeClient ()
if ( await client . checkAvailability ()) {
const printers = await client . getPrinters ()
console . log ( 'Available printers:' )
printers . forEach ( printer => {
console . log ( `- ${ printer . name } [ ${ printer . status } ]` )
})
}
Printing HTML Content
const client = new BridgeClient ()
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial; }
h1 { color: #333; }
</style>
</head>
<body>
<h1>Invoice #12345</h1>
<p>Date: ${ new Date (). toLocaleDateString () } </p>
<p>Total: $250.00</p>
</body>
</html>
`
try {
const response = await client . print ({
content: client . htmlToBase64 ( html ),
content_type: 'html' ,
copies: 1
})
if ( response . success ) {
console . log ( 'Print successful:' , response . job_id )
}
} catch ( error ) {
console . error ( 'Print error:' , error . message )
}
Printing to Specific Printer
const client = new BridgeClient ()
// Get available printers
const printers = await client . getPrinters ()
// Find specific printer
const targetPrinter = printers . find ( p => p . name . includes ( 'LaserJet' ))
if ( targetPrinter ) {
const html = '<h1>Test Print</h1>'
await client . print ({
printer_name: targetPrinter . name ,
content: client . htmlToBase64 ( html ),
content_type: 'html'
})
}
Error Handling
const client = new BridgeClient ()
try {
// Check availability first
const available = await client . checkAvailability ()
if ( ! available ) {
throw new Error ( 'Bridge service is not available' )
}
// Get printers
const printers = await client . getPrinters ()
if ( printers . length === 0 ) {
throw new Error ( 'No printers available' )
}
// Print content
const response = await client . print ({
content: client . htmlToBase64 ( '<h1>Document</h1>' ),
content_type: 'html'
})
console . log ( 'Print job submitted:' , response . job_id )
} catch ( error ) {
if ( error . message . includes ( 'Bridge service' )) {
console . error ( 'Bridge connection error:' , error . message )
} else if ( error . message . includes ( 'No printers' )) {
console . error ( 'Printer error:' , error . message )
} else {
console . error ( 'Unknown error:' , error )
}
}
With Retry Logic
const client = new BridgeClient ()
async function printWithRetry (
request : BridgePrintRequest ,
maxRetries : number = 3
) : Promise < BridgePrintResponse > {
let lastError : Error | null = null
for ( let i = 0 ; i < maxRetries ; i ++ ) {
try {
// Check if bridge is available before printing
const available = await client . checkAvailability ()
if ( ! available ) {
throw new Error ( 'Bridge not available' )
}
return await client . print ( request )
} catch ( error ) {
lastError = error as Error
console . log ( `Attempt ${ i + 1 } failed, retrying...` )
// Wait before retrying (exponential backoff)
await new Promise ( resolve => setTimeout ( resolve , 1000 * ( i + 1 )))
}
}
throw lastError || new Error ( 'Print failed after retries' )
}
// Usage
try {
const response = await printWithRetry ({
content: client . htmlToBase64 ( '<h1>Important Document</h1>' ),
content_type: 'html' ,
copies: 1
})
console . log ( 'Print succeeded:' , response . job_id )
} catch ( error ) {
console . error ( 'Print failed after all retries:' , error )
}
Monitoring Bridge Health
const client = new BridgeClient ()
async function monitorBridge () {
setInterval ( async () => {
const health = await client . getHealth ()
if ( health ) {
console . log ( `Bridge Status: ${ health . status } ` )
console . log ( `Uptime: ${ Math . floor ( health . uptime / 60 ) } minutes` )
} else {
console . warn ( 'Bridge is offline' )
}
}, 30000 ) // Check every 30 seconds
}
monitorBridge ()
Complete Print Workflow
import { BridgeClient } from 'vue-print-it'
import type { BridgePrinter , BridgePrintResponse } from 'vue-print-it'
class PrintManager {
private client : BridgeClient
constructor ( baseUrl : string = 'http://localhost:8765' ) {
this . client = new BridgeClient ( baseUrl )
}
async initialize () : Promise < boolean > {
return await this . client . checkAvailability ()
}
async getAvailablePrinters () : Promise < BridgePrinter []> {
return await this . client . getPrinters ()
}
async printHTML (
html : string ,
printerName ?: string ,
copies : number = 1
) : Promise < BridgePrintResponse > {
const content = this . client . htmlToBase64 ( html )
return await this . client . print ({
printer_name: printerName ,
content ,
content_type: 'html' ,
copies
})
}
async printToDefault ( html : string ) : Promise < BridgePrintResponse > {
const printers = await this . getAvailablePrinters ()
const defaultPrinter = printers . find ( p => p . is_default )
return await this . printHTML (
html ,
defaultPrinter ?. name ,
1
)
}
}
// Usage
const manager = new PrintManager ()
if ( await manager . initialize ()) {
const printers = await manager . getAvailablePrinters ()
console . log ( 'Available printers:' , printers . length )
const response = await manager . printToDefault ( '<h1>Test</h1>' )
console . log ( 'Print job:' , response . job_id )
}
Type Definitions
interface BridgeHealthResponse {
status : string
version : string
uptime : number
}
interface BridgePrinter {
name : string
is_default : boolean
status : string
}
interface BridgePrintRequest {
printer_name ?: string
content : string
content_type : 'html' | 'pdf'
copies ?: number
options ?: Record < string , any >
}
interface BridgePrintResponse {
success : boolean
job_id : string
message : string
}
Error Handling
The BridgeClient methods throw errors in the following cases:
Network errors : When the bridge service is unreachable
HTTP errors : When the bridge returns a non-OK status
Timeout errors : When requests exceed the timeout (2 seconds for checkAvailability)
Always wrap calls in try-catch blocks:
try {
const response = await client . print ( request )
} catch ( error ) {
console . error ( 'Print failed:' , error . message )
}
See Also