rafaelcecchin/cups-printer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rafaelcecchin/cups-printer

Composer 安装命令:

composer require rafaelcecchin/cups-printer

包简介

Simple CUPS printer

README 文档

README

After spending a lot of time searching for a simple solution to print files directly from PHP, I decided to create my own Composer library. 🚀

composer require rafaelcecchin/cups-printer

Initially, I thought it would be easier to find a solution that fits different environments, but I was mistaken. Many of the existing solutions for printing with CUPS + PHP are outdated, involve a lot of code, and overlook the fact that many developers use Windows for development and Linux for production environments. 🖥️➡️🐧

This project utilizes the CUPS command:

lp -h [printerServer] -d [printerName] [fileLocation]

"Okay, but what about the cases you mentioned, where I work with a Windows development environment and a Linux production environment?" 🌐

It will work too, and the solution is simpler than you might think. I'll explain more about this below. 👇

How to Use

Here’s a code snippet demonstrating the functions of the library. 📜

use RafaelCecchin\CupsPrinter\CupsPrinter;

$printerServer = '192.168.0.100';
$printerName = 'L8360CDW-Rafael';

$printer = new CupsPrinter($printerServer, $printerName);

/* Print an existing file */
$fileLocation = '/var/docs/test.pdf';
$printer->printFile($fileLocation);

/* Save the content to a temporary file before printing */
$output = $pdf->output();
$printer->printData($output);

/* Save the buffer content to a temporary file before printing */
$print = $printer->obPrint(function () use ($pdf) {
    $pdf->stream("dompdf_out.pdf", array("Attachment" => false));
});

The only requirement is that the host must have cups-client installed. 🛠️

On Linux, you can run:

sudo apt-get update
sudo apt-get install cups-client

On Windows, one solution is to download Linux from the Windows Store. Yes, this is now possible through WSL technology. Just access the Windows Store, search for the Linux distribution you want, and install cups-client inside it.

In your project, you should create a class that extends CupsPrinter and, using polymorphism principles, override the cmdPrintCommand function. Here’s an example of how I did it. 🧩

use RafaelCecchin\CupsPrinter\CupsPrinter;

class Printer extends CupsPrinter
{
    function __construct(String $printerServer, String $printerName)
    {
        parent::__construct($printerServer, $printerName);
    }

    public function cmdPrintCommand(String $fileLocation)
    {
        $env = getenv('APP_ENV');
        $cmd = '';

        if ($env == 'DEV') {
            $cmd .= "ubuntu run ";
            $fileLocation = $this->convertWindowsPathToUnix($fileLocation);
        }

        $cmd .= "lp -h $this->printerServer -d $this->printerName $fileLocation";
        
        return $cmd;
    }

    private function convertWindowsPathToUnix(String $fileLocation)
    {
        $fileLocation = str_replace('\\', '/', $fileLocation);
        $fileLocation = preg_replace_callback(
            '/^([a-zA-Z]):/',
            function ($matches) {
                return '/mnt/' . strtolower($matches[1]);
            },
            $fileLocation
        );

        return $fileLocation;
    }
}

In this case, I downloaded Ubuntu from the Windows Store. To run a command inside it, I prepend ubuntu run before the lp command and also adjust the file path to the Unix format, adding /mnt/ before the drive letter.

Simple, right? 😎

I hope this solution makes sense to you! 😊

rafaelcecchin/cups-printer 适用场景与选型建议

rafaelcecchin/cups-printer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 111 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 rafaelcecchin/cups-printer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rafaelcecchin/cups-printer 我们能提供哪些服务?
定制开发 / 二次开发

基于 rafaelcecchin/cups-printer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 111
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-07-26