How to chose Paper Format when printing a PDF File with Python?

僤鯓⒐⒋嵵緔 提交于 2021-02-10 08:26:29

问题


I am trying to print out a PDF file, but I do not know how to specify the Page Format. I want to print all of my PDFs in A5 Format. Can someone please help me?

# this code works and prints the PDF File, but not in the A5 Format
import subprocess
printer='MyPrinter' # name of the printer
pdffile=r"C:\Desktop\pdf_test\pdfFile.pdf" # path to PDF
acroread=r"C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Acrobat.exe" # path to Acrobat Reader

# print the file
cmd='"%s" /N /T "%s" "%s"' % (acroread, pdffile, printer)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr=proc.communicate()
exit_code=proc.wait()

Another way of printing the PDF File, no idea how to specify format here either.

import win32api
pdffile=r"C:\Desktop\pdf_test\pdfFile.pdf" # path to PDF

printer_name = 'MyPrinter' # name of the printer
out = '/d:"%s"' % (printer_name)
### print the PDF to the proper Printer
win32api.ShellExecute(0, "print", pdffile, out, ".", 0)

回答1:


If your restriction is that you want every document printing in A5 regardless of its original size, I'm not seeing a way to do that directly within the sample code that you have shown here, as you're currently passing in only the printer name and filename.

You need to obtain the printer's Device Context in order to specify the page size for the job, which would be A5, and thus if the printer was not loaded with A5 (or did not have it in one of its defined trays; I have to be vague here as different printers would address this different ways), it will pause and show a request for A5 to be loaded.

I'm also guessing at other details here, as I'm not sure if you want to expand smaller documents to A5 size, rotate them for best fit, keep them at 100% but center them on the page, etc. The key is that your process needs to obtain the Device Context of your selected printer, such that you'll know its capabilities, and how to ask for the output to be A5 coming out of that particular printer for your submitted job.

If it's a single-size printer that can only be loaded with one size at a time (A5 or some other single size), then you can either preload it with A5 paper and throw jobs at it without further specification, or be more careful and specify A5 for the print job, on the theory that the printer will stop until A5 is loaded, and not default to printing on, say, A4, either condensed to fit or bleeding off the edge.

If you can control both the job output and the printer in use, then you can configure both with a minimum of setup to generate the output you want. If you have no control over what the user will be selecting for an output device, then you will need to obtain the Device Context for that device in order to dictate the attributes of your print job (including the A5 paper size), and let the printer figure out how it's going to do as instructed.



来源:https://stackoverflow.com/questions/33414372/how-to-chose-paper-format-when-printing-a-pdf-file-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!