关闭

利用Websocket实现网页调用打印功能

24人阅读 评论(0) 收藏 举报

最近项目遇到打印问题,情景是一台机子连着多个打印机,需要打印小样、条码、A4等等,原本以为调用window.print直接了事。可是客户不买帐啊,每次打印不同的文件都要设置不同的打印机,客户直接不干了,没办法,只好给他整。网上搜了好几个打印控件,感觉都不顺心,于是干脆就自己写一个,便想用websocket+pyqt做个打印控件。

直接撸代码吧,python丰富的库直接省了很多事情,使用tornado的websocket+pyqt的QPrinter,几行代码就搞定了

#-*- coding:utf8 -*-
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
import sys

from tornado.options import define,options
from tornado.websocket import WebSocketHandler

import json

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtPrintSupport import QPrinterInfo,QPrinter


define("port",default=8889,type=int)
def printList():
    printerInfo = QPrinterInfo()
    return printerInfo.availablePrinterNames()

def printContent(printer,context,params):
    printerInfo = QPrinterInfo()
    p = QPrinter()
    for item in printerInfo.availablePrinters():
        if printer == item.printerName():
            print(printer)
            p = QPrinter(item,QPrinter.PrinterResolution)
            break
    p.setPageMargins(0, 0, 0, 0, QPrinter.Pica)

    p.setFullPage(True)
    docWidth = params['width'];
    docHeight = params['height'];
    orit = params['orientation']
    # if orit == "landscape":
    #     p.setOrientation(QPrinter.Landscape)
    # else:
    #     p.setOrientation(QPrinter.Portrait)
    print(docWidth * 35)
    docSize = QSizeF(docWidth * 35,docHeight*35)
    doc = QTextDocument()
    doc.setDocumentMargin(5)
    doc.setPageSize(docSize)
    #doc.setDefaultFont(QFont("Microsoft YaHei", 4, QFont.Normal))
    #doc.setIndentWidth(80)

    doc.setHtml(u'%s' % context)
    doc.print(p)

def printed(state):
    print(state)

class PrinterHandler(WebSocketHandler):
    def open(self):
        pass

    def on_message(self, message):
        command = json.loads(message)
        printype = command['type']
        if printype == "print":
            printer = command['printer']
            content = command['content']
            params = command['params']
            print(printer)
            try:
                printContent(printer, content,params)
                ret = {"code": 200, "msg": "打印成功"}
                self.write_message(json.dumps(ret))
            except Exception as e:
                ret = {"code":500,"msg":"打印失败,错误信息:" + e}
                self.write_message(json.dumps(ret))
        elif printype == "list":
            ## TODO:获取打印机列表
            ls = printList()
            ret = {"code":204,"msg":"获取打印机列表成功","data":ls}
            self.write_message(json.dumps(ret))

    def on_close(self):
        pass

    def check_origin(self, origin):
        return True  # 允许WebSocket的跨域请求

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = QApplication(sys.argv)
    wapp = tornado.web.Application([(r"/printer",PrinterHandler)])
    http_server = tornado.httpserver.HTTPServer(wapp)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
    app.exec_()


前端也可以封装一成了Printer类就可以了

贴个git地址^-^

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档