【问题标题】:Print PDF from web without saving to filesystem first从 Web 打印 PDF 而不先保存到文件系统
【发布时间】:2016-07-03 03:22:49
【问题描述】:

在 Python3.4 中,我使用以下代码使用 requests 库从网站打印 PDF:

with open(temp_pdf_file, 'wb') as handle:
   response = requests.get(html.unescape(message['body']), stream=True)
   for block in response.iter_content(1024):
       handle.write(block)
cmd = '/usr/bin/lpr -P {} {}'.format(self.printer_name,temp_pdf_file)
print(cmd)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = proc.communicate()
exit_code = proc.wait()

有没有办法跳过临时文件保存直接流到打印机上?

【问题讨论】:

    标签: python python-3.x pdf printing lpr


    【解决方案1】:

    您可以让子进程从标准输入读取其输入并直接写入标准输入“文件”。

    import requests
    from subprocess import Popen, PIPE
    
    message = ...
    
    cmd = '/usr/bin/lpr -P {}'.format(self.printer_name)
    proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
    response = requests.get(html.unescape(message['body']), stream=True)
    for block in response.iter_content(1024):
        proc.stdin.write(block)
    stdout, stderr = proc.communicate()
    exit_code = proc.wait()
    print exit_code
    

    【讨论】:

    • 太好了,谢谢。现在我看到它太明显了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2017-06-10
    相关资源
    最近更新 更多