惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
L
LangChain Blog
C
Check Point Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
Recent Announcements
Recent Announcements
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
The Cloudflare Blog
月光博客
月光博客
有赞技术团队
有赞技术团队
G
Google Developers Blog
Vercel News
Vercel News

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2019-03-23 · via 轶哥博客

解决方案

在Windows平台,使用Node.js实现原生打印PDF通常需要借助C++。例如node-printer,需要安装Python、Visual Studio,同时还需要使用node-pdfium进行PDF转EMF。

也有一些二进制(exe)命令行打印解决方案,但是都有一些缺陷。

例如:

  • 借助 Adobe Reader 或 Acrobat 实现打印,则Windows XP、7、10需要安装不同版本的 DPF 软件,并且在打印时会打开该软件。
  • Sumatra PDF可以静默进行命令行打印,但是在80mm热敏纸打印机中的打印效果较差(不清晰)。

理论上这些方案支持所有能在Windows中安装驱动的打印机,包括非串口的热敏纸打印机。大多数方案都是基于PDFium的实现,如果您有足够的时间精力,可以深入研究PDFium

安装方法

为了方便大家使用Node.js实现PDF文件的打印,特意整理最佳方案上传为NPM模块:

npm i node-pdf-printer

使用方法

const NodePdfPrinter = require('node-pdf-printer')

NodePdfPrinter.listPrinter() // 列出所有打印机名称

const array = ['C:\\abc\\a.pdf', 'D:\\abd\\b.pdf'] // 待打印文件地址列表

NodePdfPrinter.printFiles(array) // 打印文件到Windows默认打印机

NodePdfPrinter.printFiles(array, printerName) // 打印文件到指定打印机

仅需Node.js环境,借助PDFtoPrinter.exe实现。

开源仓库

https://github.com/yi-ge/node-pdf-printer

支持Electron。如果您使用Electron,请参考:https://github.com/sindresorhus/electron-util#fixpathforasarunpackpath

如需支持双面打印,需要事先修改打印机默认配置。

LinuxAndroid平台的打印

可以参考:CUPSGhostScriptFoo2系列工具(foo2zjs、arm2hpdl等)HPijs