Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Tauri Plugin Printer WKHTML-BIN (Tauri V2)
A powerful Tauri V2 printer plugin
Supports PDF/HTML printing, printer management, print job control, and more.
Installation • Usage • API Documentation • Examples
🚀 Quick Start
5-Minute Getting Started Guide
Important: Make sure to place the wkhtmltopdf.exe binary inside the src-tauri/bin folder of your project. The plugin will automatically look for it there.
-
Install the plugin
-
Register the plugin
// src-tauri/src/lib.rs use init; -
Configure permissions
// src-tauri/capabilities/default.json -
Start using
import from 'tauri-plugin-printer-wkhtml-bin'; // Get printers const printers = JSON.; console.log; // Print PDF await ;
🎉 Congratulations! You have successfully integrated the printer plugin and can now start printing documents.
Acknowledgements
This project is based on the following open source projects:
Thanks to the original authors for their contributions and open source spirit.
✨ Features
- 📦 Looks for
wkhtmltopdfinbinfolder insidesrc-tauri - 🛠️ Fixed TypeScript types
- 📝 Updated comments from Chinese to English
📦 Installation
Important: Make sure to place the wkhtmltopdf.exe binary inside the src-tauri/bin folder of your project. The plugin will automatically look for it there.
Method 1: Using crates.io (Recommended)
# Add Rust dependency
# Install frontend API
Method 2: Using Tauri CLI
Method 3: Manual Installation
- Add dependency in
Cargo.toml:
[]
= "0.1.3"
# Or use Git version
# tauri-plugin-printer-wkhtml-bin = { git = "https://github.com/FrancoJFerreyra/tauri-plugin-printer" }
- Register the plugin in
src-tauri/src/lib.rs:
use init;
- Add permissions in
src-tauri/capabilities/default.json:
- Install frontend dependency:
🚀 Usage
Basic Example
import from 'tauri-plugin-printer-wkhtml-bin';
// Test plugin connection
const response = await ;
console.log;
// Get printer list
const printers = await ;
console.log;
Full API Example
import from 'tauri-plugin-printer-wkhtml-bin';
// 1. Get all printers
const allPrinters = await ;
// 2. Get specific printer by name
const specificPrinter = await ;
// 3. Print PDF file
const printResult = await ;
// 4. Print HTML content
const htmlPrintResult = await ;
// 5. Get print jobs
const jobs = await ;
// 6. Manage print jobs
const jobId = '123';
const printerName = 'Microsoft Print to PDF';
// Pause job
await ;
// Resume job
await ;
// Restart job
await ;
// Remove job
await ;
📋 Examples
Real-world Use Cases
1. Print Invoice or Report
import from 'tauri-plugin-printer-wkhtml-bin';
// Generate invoice HTML
const invoiceHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Invoice</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.header { text-align: center; margin-bottom: 30px; }
.invoice-details { margin-bottom: 20px; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<div class="header">
<h1>Invoice</h1>
<p>Invoice No: INV-2024-001</p>
</div>
<div class="invoice-details">
<p><strong>Customer:</strong> Zhang San</p>
<p><strong>Date:</strong> 2024-01-15</p>
</div>
<table>
<tr><th>Item</th><th>Qty</th><th>Price</th><th>Total</th></tr>
<tr><td>Product A</td><td>2</td><td>¥100</td><td>¥200</td></tr>
<tr><td>Product B</td><td>1</td><td>¥150</td><td>¥150</td></tr>
</table>
<p style="text-align: right; margin-top: 20px;"><strong>total: ¥350</strong></p>
</body>
</html>
`;
// Print invoice
try catch
2. Batch Print PDF Files
import from 'tauri-plugin-printer-wkhtml-bin';
const pdfFiles = ;
// Get default printer
const printers = JSON.;
const defaultPrinter = printers.?. || printers?.;
// Batch print
3. Printer Status Monitoring
import from 'tauri-plugin-printer-wkhtml-bin';
// Monitor printer status
// Check every 30 seconds
setInterval;
📚 API Documentation
ping(request: PingRequest): Promise<PingResponse>
Test plugin connection status.
getPrinters(): Promise<string>
Get all available printers in the system, returned as a JSON string.
getPrintersByName(name: string): Promise<string>
Get info of a specific printer by name.
printPdf(options: PrintOptions): Promise<string>
Print PDF files.
PrintOptions parameters:
path: PDF file pathprinter: Printer namepages: Page range (optional)subset: Page subset (optional)
printHtml(options: HtmlPrintOptions): Promise<string>
Print HTML content.
HtmlPrintOptions parameters:
html: HTML content stringprinter: Printer name
Print Job Management
getJobs(printer: string): Promise<string>- Get all jobs for a printergetJobsById(printer: string, jobId: string): Promise<string>- Get info for a specific jobpauseJob(printer: string, jobId: string): Promise<string>- Pause a print jobresumeJob(printer: string, jobId: string): Promise<string>- Resume a print jobrestartJob(printer: string, jobId: string): Promise<string>- Restart a print jobremoveJob(printer: string, jobId: string): Promise<string>- Delete a print job
🛠️ Development
Run Example Application
# Clone repository
# Build plugin
# Run example app
Project Structure
tauri-plugin-printer/
├── src/ # Rust source code
│ ├── lib.rs # Main plugin entrypoint
│ ├── commands.rs # Tauri command definitions
│ ├── desktop.rs # Desktop implementation
│ ├── windows.rs # Windows specific implementation
│ └── ...
├── guest-js/ # JavaScript API
│ └── index.ts # Frontend API definition
├── permissions/ # Permission config
├── examples/ # Sample apps
│ └── tauri-app/ # Vue.js example
└── dist-js/ # Built JS files
🔧 Permission Configuration
The plugin uses the following permissions:
[]
= "Default permissions for the plugin"
= [
"allow-ping",
"allow-create-temp-file",
"allow-remove-temp-file",
"allow-get-printers",
"allow-get-printers-by-name",
"allow-print-pdf",
"allow-print-html",
"allow-get-jobs",
"allow-get-jobs-by-id",
"allow-resume-job",
"allow-restart-job",
"allow-pause-job",
"allow-remove-job"
]
🐛 Known Issues
- Currently supports Windows systems mainly
- Some printer drivers may not be fully compatible
- Printing large files may need extra memory management
- HTML printing may have formatting issues with complex layouts
🔧 Troubleshooting
Common Issues
Q: Cannot retrieve printer list
A: Check:
- Ensure a printer is installed on your system
- Ensure the printer service is running correctly
- Confirm the app has printer access permissions
Q: PDF printing fails
A: Possible solutions:
- Ensure the PDF file path is correct and file exists
- Check if the PDF file is corrupted
- Ensure a PDF reader (like Adobe Reader) is installed
- Try using absolute paths rather than relative
Q: HTML print formatting issues
A: Suggestions:
- Use simple CSS styles, avoid complex layouts
- Set appropriate page size and margins
- Preview by printing to PDF first
- Avoid JavaScript and external resources
Q: Permission configuration issues
A: Ensure configuration in src-tauri/capabilities/default.json:
Debugging Tips
-
Enable detailed logs:
RUST_LOG=debug -
Check printer status:
const printers = JSON.; console.log; -
Test connection:
try catch
⚡ Performance Optimization
Best Practices
1. Optimize large file printing
// For large PDF files, suggest printing by page batches
const largePdfPath = '/path/to/large-document.pdf';
// Print in batches, 10 pages each time
2. HTML print optimization
// Optimized HTML structure
const optimizedHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* Print styles */
@media print {
body { margin: 0; padding: 20px; }
.no-print { display: none; }
.page-break { page-break-before: always; }
}
/* Basic style */
body {
font-family: 'Arial', sans-serif;
font-size: 12pt;
line-height: 1.4;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #000;
padding: 8px;
text-align: left;
}
</style>
</head>
<body>
<!-- Content -->
</body>
</html>
`;
3. Error Handling and Retries
// Print function with retry logic
// Usage
try catch
4. Print Queue Management
// Using the print queue
const printQueue = ; // up to 2 concurrent print jobs
// Add print tasks
const files = ;
Memory Management
- Avoid storing large amounts of HTML in memory
- Clean up print job references promptly
- Use streaming for large file handling
- Regularly monitor and clear the print queue
🤝 Contributing
Issues and Pull Requests are welcome!
📄 License
MIT License
🙏 Acknowledgements
This project is based on:
Thanks to the original authors!
📝 Changelog
v0.1.4 (current)
- 🛠️ Hide console on print
v0.1.3 (current)
- 🛠️ Update README.md file
v0.1.2
- 🛠️ Update README.md file
v0.1.1
- 🔧 Fixes TypeScript types in the API
v0.1.0
- 🎉 Initial release of this fork
- 🔧 Fixes TypeScript types in the API
- 🛠️ Updates comments from Chinese to English
- 📦 Looks for
wkhtmltopdfin thebinfolder insidesrc-tauri