web-print-service
Version:
Print a website service
244 lines (159 loc) • 6.31 kB
Markdown
# Hướng Dẫn Sử Dụng Print Service
*Note: The user guide in English is located at the bottom of the document*
**Tác giả**: Truongpv (invncur@gmail.com)
**Hỗ trợ dự án**: Nếu bạn thấy dịch vụ hữu ích, hãy ủng hộ để giúp dự án phát triển:
Pham Van Truong - 229016999 - Ngân hàng A CHAU (ACB)
Dịch vụ Print Service là một API REST giúp người dùng chuyển đổi nội dung HTML hoặc URL thành tài liệu PDF và gửi chúng đến máy in. Dịch vụ này có thể được sử dụng để giúp các ứng dụng web gửi đồng thời lệnh in đến nhiều máy in. Hướng dẫn dưới đây đề cập đến các tính năng chính và cách sử dụng API này.
## 1. Cài đặt
### 1.1. Yêu cầu hệ thống
- **Node.js**: Phiên bản v16 hoặc mới hơn.
- **Hệ điều hành**: Windows, macOS, hoặc Linux.
### 1.2. Cài đặt package
```bash
npm install -g web-print-service
```
### 1.3. Cài đặt PM2 để quản lý dịch vụ
**PM2** là một công cụ quản lý các tiến trình Node.js, tự động khởi động lại khi gặp lỗi và đảm bảo dịch vụ bắt đầu khi hệ thống khởi động lại.
#### Cài đặt PM2:
```bash
npm install -g pm2
```
## 2. Khởi tạo dịch vụ:
1. Khởi động dịch vụ với lệnh sau:
```bash
pm2 start printservice --name "PrintService" -- start [port]
```
2. Lưu cấu hình PM2 để bật tự động khởi động lại:
```bash
pm2 save
pm2 startup
```
Mặc định, dịch vụ sẽ chạy tại **http://localhost:8081**.
## 3. API Đầu Vào
### 3.1. **GET /web-print**
In nội dung từ URL được cung cấp qua query.
#### Tham số:
- **url** (bắt buộc): URL của trang web.
- **printer** (tuỳ chọn): Tên máy in.
- **landscape** (tuỳ chọn): Định dạng ngang (1 hoặc 0). Mặc định là 0.
- **page_size** (tuỳ chọn): Khổ giấy (A4, A3, letter,...).
#### Cách gọi:
```bash
curl --location 'http://localhost:8081/web-print?url=https://example.com&printer=MyPrinter&page_size=A4'
```
### 3.2. **POST /html-print**
In nội dung HTML được cung cấp qua body request.
#### Body (JSON):
- **html_content** (bắt buộc): Nội dung HTML.
#### Tham số Query (tuỳ chọn):
- **printer** (tuỳ chọn): Tên máy in.
- **landscape** (tuỳ chọn): Định dạng ngang (1 hoặc 0).
- **page_size** (tuỳ chọn): Khổ giấy.
#### Cách gọi:
```bash
curl --location 'http://localhost:8081/html-print' \
--header 'Content-Type: application/json' \
--data '{
"html_content": "<div>Test Print</div>"
}'
```
## 4. Quản lý Máy In
### 4.1. Lệnh GET /printers
Kiểm tra danh sách máy in.
#### Cách gọi:
```bash
curl --location 'http://localhost:8081/printers'
```
Kết quả trả về là danh sách tên máy in và trạng thái của chúng.
## 5. Bảo trì và Dọn dẹp
Dịch vụ tự động xóa các file PDF đã lớn hơn 7 ngày trong thư mục `/reports` bằng cron.
- Cron được thiết lập chạy vào 0h hàng ngày.
## 6. Quản lý Vấn Đề
### 6.1. Lỗi đọc/ghi file
Kiểm tra quyền truy cập của thư mục `/reports`.
### 6.2. Lỗi máy in offline
Kiểm tra trạng thái của máy in trong danh sách truy vấn từ `/printers`.
# Print Service User Guide
**Author**: Truongpv (invncur@gmail.com)
**Donate**: Pham Van Truong - 229016999 - A CHAU Bank (ACB)
Print Service is a REST API that allows users to convert HTML content or URLs into PDF documents and send them to a printer. This service can be used to enable web applications to send print commands to multiple printers simultaneously. The guide below covers the main features and how to use this API.
## 1. Installation
### 1.1. System Requirements
- **Node.js**: Version v16 or later.
- **Operating System**: Windows, macOS, or Linux.
### 1.2. Install the package
```bash
npm install -g web-print-service
```
### 1.3. Install PM2 to manage the service
**PM2** is a tool for managing Node.js processes, restarting on errors, and ensuring the service starts after system reboots.
#### Install PM2:
```bash
npm install -g pm2
```
## 2. Starting the Service
1. Start the service with the following command:
```bash
pm2 start printservice --name "PrintService" -- start [port]
```
2. Save the PM2 configuration to enable auto-start on reboot:
```bash
pm2 save
pm2 startup
```
By default, the service will run at **http://localhost:8081**.
## 3. Input APIs
### 3.1. **GET /web-print**
Print content from a URL provided via query parameters.
#### Parameters:
- **url** (required): The URL of the webpage.
- **printer** (optional): The printer name.
- **landscape** (optional): Landscape orientation (1 or 0). Default is 0.
- **page_size** (optional): Paper size (A4, A3, letter, etc.).
#### Example Call:
```bash
curl --location 'http://localhost:8081/web-print?url=https://example.com&printer=MyPrinter&page_size=A4'
```
### 3.2. **POST /html-print**
Print HTML content provided in the request body.
#### Body (JSON):
- **html_content** (required): The HTML content.
#### Query Parameters (optional):
- **printer**: The printer name.
- **landscape**: Landscape orientation (1 or 0).
- **page_size**: Paper size.
#### Example Call:
```bash
curl --location 'http://localhost:8081/html-print' \
--header 'Content-Type: application/json' \
--data '{
"html_content": "<div>Test Print</div>"
}'
```
## 4. Printer Management
### 4.1. GET /printers Command
Check the list of printers.
#### Example Call:
```bash
curl --location 'http://localhost:8081/printers'
```
The result will return the list of printer names and their statuses.
## 5. Maintenance and Cleanup
The service automatically deletes PDF files older than 7 days in the `/reports` directory using a cron job.
- The cron job is set to run at midnight daily.
## 6. Issue Management
### 6.1. File Read/Write Errors
Check the access permissions for the `/reports` directory.
### 6.2. Printer Offline Errors
Check the printer status in the list queried from `/printers`.