AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 981020
Accepted
Simon
Simon
Asked: 2017-11-29 01:22:50 +0800 CST2017-11-29 01:22:50 +0800 CST 2017-11-29 01:22:50 +0800 CST

使用脚本作为打印机来处理输出 (PDF)

  • 772

我想添加一个“虚拟”打印机,以便从任何软件轻松发送 PDF 输出。打印机应该是一个脚本,然后处理 PDF 输出。用例可以是“打印到云存储”或“打印到存档”等。

到目前为止,我发现的可能性是:

  • CUPS 管道后端(在此处描述)在 Ubuntu 上似乎不可用。
  • tea4cups(来自这个线程lp)在预处理后将文件转发到。但我不想实际打印它。

是否有另一种更简单的方法可以将 PDF 通过管道传输到自定义 bash 脚本中?

scripts
  • 2 2 个回答
  • 2075 Views

2 个回答

  • Voted
  1. Best Answer
    Elder Geek
    2018-03-21T06:50:58+08:002018-03-21T06:50:58+08:00

    线索似乎在您问题的第一个链接中。研究表明后端只是脚本。所以你可以简单地写你自己的。但是,您正在寻找的可用于 OpenSuse 的脚本似乎在您的第一个链接中进行了描述。

    openSUSE RPM 包 cups-backends 提供了 bash 脚本 /usr/lib/cups/backend/pipe 这是一个包装后端,用于打印到任何程序。

    由于 OpenSuse 现在使用 .ymp 一键式安装程序,因此查找此脚本很复杂,您必须深入挖掘才能找到包含该脚本的实际 .rpm。这是一个直接取自 cups-backends-1.1-296.6.noarch.rpm 的示例。

    使用文件滚轮打开包并导航到/./usr/lib/cups/backend/包内并打开pipe会显示以下脚本:

    #! /bin/bash
    # CUPS wrapper backend "pipe" for printing to any program.
    # It forwards the print job data like a pipe into another command.
    # Author: Johannes Meixner <[email protected]>, 2009, 2014
    # For basic information see "man 7 backend" and "man 7 filter".
    # This program is free software; you can redistribute it and/or modify it
    # under the terms of the GNU GENERAL PUBLIC LICENSE Version 2
    # as published by the Free Software Foundation.
    
    # Activate the "set -x" line to get debugging info in /var/log/cups/error_log:
    #set -x
    
    # Output "Device Discovery" information on stdout:
    if test "$#" = "0"
    then
      echo 'direct pipe "Unknown" "Forward print job data like a pipe to another command"'
      exit 0
    fi
    
    # Output usage information in case of wrong number of parameters:
    if test "$#" != "5" -a "$#" != "6"
    then
      echo 'Usage: pipe job-id user title copies options [file]' 1>&2
      echo 'Queue setup example:' 1>&2
      echo 'lpadmin -p queue_name -v "pipe:/path/to/command?option1=value1&-option2&value2" -E' 1>&2
      echo 'The command is called with the specified options as:' 1>&2
      echo '/path/to/command option1=value1 -option2 value2' 1>&2
      echo 'The original command line parameters (... job-id user ...)' 1>&2
      echo 'are provided as environment variables PIPE_BACKEND_ARGV[0-6]' 1>&2
      exit 1
    fi
    
    # Keep the original command line parameters (... job-id user ...)
    # in environment variables to make them available for the command:
    export PIPE_BACKEND_ARGV0="$0"
    export PIPE_BACKEND_ARGV1="$1"
    export PIPE_BACKEND_ARGV2="$2"
    export PIPE_BACKEND_ARGV3="$3"
    export PIPE_BACKEND_ARGV4="$4"
    export PIPE_BACKEND_ARGV5="$5"
    export PIPE_BACKEND_ARGV6="$6"
    
    # Have the input at fd0 (stdin) in any case:
    if test -n "$6"
    then
      exec <"$6"
    fi
    
    # To be on the safe side clip anything after the first space character
    # because spaces are excluded characters in a URI (see RFC 2396):
    URI=${DEVICE_URI%% *}
    # Extract the command to which the job schould be sent from the URI.
    # Clip the URI scheme (anything up to the first ':' character) and
    # clip anything after the first '?' character
    # because anything after the first '?' character are options:
    COMMAND=${URI#*:}
    COMMAND=${COMMAND%%\?*}
    # Extract the options (e.g. from 'pipe:/path/to/command?option1=value1&-option2&value2'
    # clip anything up to the first '?' character and
    # replace the options separator '&' character by a space:
    OPTIONS=${URI#*\?}
    if test "$OPTIONS" = "$URI"
    then
      OPTIONS=""
    fi
    OPTIONS=$( echo "$OPTIONS" | tr '&' ' ' )
    # Options could have been specified in a wrong way
    # as addendum to the DeviceURI separated by spaces
    # (spaces are excluded characters in a URI, see RFC 2396)
    # which works by luck at least for some CUPS versions
    # see https://bugzilla.novell.com/show_bug.cgi?id=499735
    # like 'pipe:/path/to/command option1=value1 -option2 value2'
    # nevertheless use additionally those kind of options too:
    MORE_OPTIONS=${DEVICE_URI#* }
    if test "$MORE_OPTIONS" = "$DEVICE_URI"
    then
      MORE_OPTIONS=""
    fi
    OPTIONS=$( echo "$OPTIONS" "$MORE_OPTIONS" | tr -s ' ' )
    
    # Test if the command is executable:
    if ! test -x "$COMMAND"
    then
      echo "Cannot execute $COMMAND" 1>&2
      exit 1
    fi
    
    # Replace this wrapper with the actual command
    # so that it exits with the exit code of the command
    # and that the command gets any signals directly.
    exec "$COMMAND" $OPTIONS
    

    由于这是一个 bash 脚本,您应该能够将它与任何版本的 'nix 一起使用,包括 Ubuntu,使用 bash shell,而不仅仅是 Suse。

    为了向杯子添加后端,您需要将其复制到/usr/lib/cups/backend并设置权限和所有权以匹配。

    chown root:root /usr/lib/cups/backend/pipe

    chmod 755 /usr/lib/cups/backend/pipe

    资料来源:

    https://www.cups.org/doc/man-backend.html

    https://build.opensuse.org/package/view_file/Printing/cups-backends/cups-backends.spec?expand=1

    如何提取 RPM 文件?

    • 6
  2. Denis
    2019-10-26T12:53:03+08:002019-10-26T12:53:03+08:00

    我一直在寻找同样的东西,但发现没有什么容易的。我会在这里写下我是如何解决的,也许其他人会发现这个解决方案很有用。

    我的驱动程序用于手动双面打印,但不是双面打印(在双面打印文件中),您可以实现自己的代码。您那里还有一个安装程序...

    源代码在github上:https ://github.com/dentys03/manual_duplex_linux

    它应该适用于任何 HP 打印机。我猜很容易适应其他打印机。

    • 0

相关问题

  • 如何在 Nautilus 中管理保存的完整网页及其目录(例如 n.html 和 n_files)

  • 如何每 5 秒运行一次脚本?

  • 如何将必须从其自己的目录中运行的程序添加到面板或主菜单?

  • 如何编写 shell 脚本来安装应用程序列表?

  • Mac OS X Automator 的替代品?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve