PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/专题/打印排障

网页静默打印:不弹预览的做法

浏览器不会让公网页静默选打印机。可行的做法是服务器调用打印接口,而不是藏起 window.print。调用 POST /v1/print-jobs,API Key 只放在服务端。

面向 想去掉打印预览框的前端和后端

其他语言版本: English

说明

window.print 会带上浏览器的预览或对话框。公网页面也不能随便访问 127.0.0.1 上的打印组件。把密钥放进前端更不行。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。打印机由门店电脑上的 Agent 主动连出,不需要公网 IP,也不需要把 9100 端口映射到互联网。

正确的分工是:页面把业务单号交给你的服务器,服务器 POST /v1/print-jobs,门店 Agent 出纸。顾客的电脑上不会弹出对话框,纸出现在门店。

Agent 的 127.0.0.1:17891 只允许 localhost 和 printbase.cloud 来源。你的业务域名会被拒绝。这是英文静默打印页的中文版。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

创建任务使用 POST /v1/print-jobs,请求头 Authorization: Bearer,正文包含 printer_code、content_type(pdf 或 raw)以及 content 或 content_url 二者之一。免费套餐是一台电脑每月 100 个任务。

接入步骤

  1. 01

    不要用 window.print 打后厨或面单

    那个对话框在顾客或店员自己的电脑上。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  2. 02

    页面只提交单号

    密钥留在服务器。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  3. 03

    服务器调用打印接口

    Bearer、printer_code、pdf 或 raw。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

  4. 04

    把失败显示给店员

    顾客页面不必展示后台处理程序细节。请求使用 POST /v1/print-jobs,请求头为 Authorization: Bearer,printer_code 来自 GET /v1/printers。

Node.js

// 页面所调用的后端,再由后端打印
const res = await fetch('https://api.printbase.cloud/v1/print-jobs', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.PRINTBASE_API_KEY}`,
    'Content-Type': 'application/json',
    'Idempotency-Key': idempotencyKey,
  },
  body: JSON.stringify({
    printer_code: Number(process.env.PRINTER_CODE),
    content_type: 'pdf',
    content: pdf.toString('base64'),
    copies: 1,
  }),
});
if (!res.ok) throw new Error(`PrintBase ${res.status}: ${await res.text()}`);
const job = await res.json();
// { id: "job_abc", status: "queued" }

这一套的细节

来源限制

空 Origin、localhost、127.0.0.1、::1 以及 printbase.cloud 主机可以访问本地端口。其他来源返回 403。Agent 只打印你提交的内容,不会替你向平台拉取单据。

控件方案

Lodop、hiprint 一类是另一条本地路。本页不把它们说成浏览器原生能力。Agent 只打印你提交的内容,不会替你向平台拉取单据。Agent 只打印你提交的内容,不会替你向平台拉取单据。

常见问题

用 CSS 能藏起预览吗?

不能。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

内网 HTTP 页面呢?

不要把上线方案押在浏览器对内网的例外上。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

和 Lodop 的直接打印有何差别?

Lodop 仍是页面里的控件。这里是服务器提交任务。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

门店平板上的网页呢?(详见本页)

平板网页同样不要带密钥。由后端打印。调用的仍是 POST /v1/print-jobs,请求头为 Bearer API Key。HTTP 201 只表示已排队,不表示已经出纸。

相关页面

  • 打印组件连接失败和端口冲突

    本地打印组件互相占用端口时,表现为连接失败。PrintBase 的云端接口不使用那些组件端口。Agent 可选的本地端口是 127.0.0.1:17891。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • Lodop 在浏览器里打印,PrintBase 在服务器上打印

    Lodop 从页面里驱动本机或它的云主机。PrintBase 从你的服务器提交任务。选哪边,取决于单据是在页面上设计的,还是在服务器上已经生成了。调用 POST /v1/print-jobs,API Key 只放在服务端。

  • Electron 收银机把本地打印换成远程任务

    收银机是 Electron 时,主进程可以继续打本机小票,也可以把任务 POST 给另一台机器上的打印机。渲染进程不要保存 API Key。调用 POST /v1/print-jobs,API Key 只放在服务端。

免费套餐:一台电脑每月 100 个任务,不需要信用卡。

免费注册查看 API 文档
PrintBase

PrintBase connects your applications to physical printers anywhere - securely, reliably, and in real time.

Product
Receipt PrintingLabel PrintingDashboardDevicesWebhooks
Resources
DocsBlogStatusAPI ReferenceDownloadPrintBase vs PrintNodePrintNode PricingFAQ
Guides
All guidesIntegrationsPrintersCode samplesComparisonsUse casesTroubleshooting
Company
ContactSecurityPrivacy

© 2026 Aveniq Technologies OÜ. All rights reserved. PrintBase is a service of Aveniq Technologies OÜ.

PrivacyTermsRefunds