利用js完成打印功能/print/pdf/静默打印

项目中出现的js调用打印机功能
更新于: 2023-11-10 22:23:01

安装

yarn add print-js

使用

import printJS from 'print-js'

cheatsheet

场景代码
打印一个URL
<button type="button" onclick="printJS('docs/printjs.pdf')">Print PDF</button>
打印大文件
<button
  type="button"
  onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})"
>
  Print PDF with Message
</button>
打印 base64
<button
  type="button"
  onclick="printJS({printable: base64, type: 'pdf', base64: true})"
>
  Print PDF with Message
</button>

静默打印

区别于上面的方式,此方式,不会弹出 web 的打印预览界面。

from fastapi import FastAPI
import uvicorn
import subprocess

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/print")
def print_pdf(pdfUrl: str):
    command = ['E:\海辞\静默打印相关\SumatraPDF-3.5.2-64\SumatraPDF-3.5.2-64.exe', '-print-to-default', pdfUrl] 
    # 传入参数-print-to-default -print-settings "设置" "要打印的文件路径"
    subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return {"result": "打印请求已接收"}


if __name__ == '__main__':
    uvicorn.run(app=app,host="127.0.0.1",port=8000)

参考