Print PDF in Python on Windows: Use an external application

I tried to find a way to print a PDF using Python on Windows.

This time, we will call an external application to print.

Postscript: 2015-07-03: The sample code can be done with python [filename]. Some are different, and no exceptions are included. Please check the code for details.

Use the command line of Adobe Reader and Adobe Acrobat

Command line options for Acrobat and Adobe Reader

Adobe Reader and Adobe Acrobat have command line options. You can start it in another process (instance) or run printing as it is.

The example here uses Adobe Acrobat. (ʻAcrobat.exe) Adobe Reader uses ʻAcroRd32.exe. The installation path also depends on the version. Is the disadvantage that the judgment is troublesome?

Reference: Print PDFs with Python? | GeoNet

Display the print dialog of Adobe Reader and then print

https://gist.github.com/hrsano645/ddd5c111204338576404

# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals

import subprocess
import sys

# ref: https://geonet.esri.com/thread/59446
# ref: https://helpx.adobe.com/jp/acrobat/kb/510705.html

def main(pdffile):
    # acroread = r'C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe'
    acrobat = r'C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe'
    
    # '"%s"'is to wrap double quotes around paths
    # as subprocess will use list2cmdline internally if we pass it a list
    # which escapes double quotes and Adobe Reader doesn't like that
    
    cmd = '"{}" /P "{}"'.format(acrobat, pdffile)
    
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    exit_code = proc.wait()


if __name__ == '__main__':
    pdffile = sys.argv[1]
    main(pdffile)

Print without displaying the print dialog of Adobe Acrobat (silent mode)

This method requires the printer name. (I have not checked if there is none. Please comment somebody)

https://gist.github.com/hrsano645/a16c3f35deac04b2b4d9

# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals

import subprocess
import sys

# ref: https://geonet.esri.com/thread/59446
# ref: https://helpx.adobe.com/jp/acrobat/kb/510705.html


def main(pdffile, printer_name):
    # acroread = r'C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe'
    acrobat = r'C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe'

    # '"%s"'is to wrap double quotes around paths
    # as subprocess will use list2cmdline internally if we pass it a list
    # which escapes double quotes and Adobe Reader doesn't like that

    cmd = '"{}" /N /T "{}" "{}"'.format(acrobat, pdffile, printer_name)

    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    exit_code = proc.wait()


if __name__ == '__main__':
    pdffile = sys.argv[1]
    printer_name = sys.argv[2]
    main(pdffile, printer_name)

Use Pywin32: Win32API.ShellExecute

Use ShellExecute in the Windows API available on Pywin32.

ShellExecute function

With ShellExecute, you can use open to open in the associated application and print to print.

It seems to work with this script, but in my environment there were times when I could print and times when I couldn't.

reference:

https://gist.github.com/hrsano645/b69b071de4b8cb59cbe9

# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals
 
# ref: http://timgolden.me.uk/python/win32_how_do_i/print.html
# ref: http://docs.activestate.com/activepython/2.7/pywin32/win32api__ShellExecute_meth.html
 
import win32api
import sys
 
 
if __name__ == '__main__':
    pdf_file_name = sys.argv[1]
    win32api.ShellExecute(0, "print", pdf_file_name, None, ".", 0)

Use GSPrint

Print using a command line application called GSPrint (included with GSView) that uses Ghostscript.

First, install Ghostscript and GSView. (The one used at the time of verification is the original version, but I think the Japanese version is probably the same)

Ghostscript 9.16 and GSview 5.0 J (Official Site)

After that, use the subprocess module to execute the command.

If you do not specify the -query option, it will be printed as is.

https://gist.github.com/hrsano645/fe73570d0d5d8df4863a

# coding: utf-8
from __future__ import division, print_function, absolute_import, unicode_literals
 
import subprocess
import sys
 
 
# ref: http://stackoverflow.com/questions/4498099/silent-printing-of-a-pdf-in-python
# ref: http://stackoverflow.com/questions/1462842/print-pdf-document-with-pythons-win32print-module
 
def main(pdffile):
    gsprint = r"C:\Program Files\Ghostgum\gsview\gsprint.exe"
 
    # -Call the printer list with the quert option
    cmd = '"{}" -query "{}"'.format(gsprint, pdffile)
 
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    exit_code = proc.wait()
 
 
if __name__ == '__main__':
    pdffile = sys.argv[1]
    main(pdffile)

As an important notice, since the paper size setting at the time of printing cannot be operated, it seems that there is no choice but to set the enlargement / reduction on the printer side after displaying the print dialog with -query. However, the PX-1700F I am using does not work for some reason even if I enlarge it. Is this a problem on the printer side?

(I tried it later, but if the paper size of the printer and the paper size set in the file match, it prints beautifully.)

At the time of printing, the color was monochrome, so it may be necessary to specify it in advance on the GSPrint side. (I haven't verified it in detail, but as far as I can see the options, I wonder if it needs to be set)

reference:

(Maybe) continue

There seems to be another way to do this using the GUI toolkit available in Python. I found something like that in wxpython and pyqt (pyside), but I haven't verified it yet. If I can verify it, I will write the continuation.

In addition, if you know a good printing method, I would appreciate it if you could write it in the comments.

Recommended Posts

Print PDF in Python on Windows: Use an external application
Use Python on Windows (PyCharm)
Use Xming to launch an Ubuntu GUI application on Windows.
Use print in a Python2 lambda expression
Use without installing python 2.x on Windows
Use Python external module on Sakura Internet
I want to use Python in the environment of pyenv + pipenv on Windows 10
Use config.ini in Python
Use dates in Python
Rasterize PDF in Python
Use Valgrind in Python
Use pyvenv on Windows
python basic on windows ②
Install python on windows
Use profiler in Python
Use Ansible on Windows
Use QuTiP on Windows
Use pip on Windows
Until drawing a 3D graph in Python on windows10
Include and use external Kv files in Python Kivy
View images in OpenCV from Python using an external USB camera on your MacBook
Set-enable Python virtualenv on Windows
Let's use def in python
Run Openpose on Python (Windows)
Use matplotlib on Ubuntu 12 & Python
Use let expression in Python
Let's get started with Python ~ Building an environment on Windows 10 ~
Use parameter store in Python
OCR from PDF in Python
How to use VS Code in venv environment on windows
Python + Kivy development on Windows
Execute external command in python
Use MongoDB ODM in Python
Use list-keyed dict in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Sphinx-autobuild (0.5.2) on Windows7, Python 3.5.1, Sphinx 1.3.5
Build Python environment on Windows
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Build python environment on windows
I ran python on windows
External command execution in Python
[Python] [Chainer] [Windows] Install Chainer on Windows
Python install in 2 lines @Windows
Use Linux on Windows 10 (WSL2)
Create a Python environment for professionals in VS Code on Windows
If you get stuck in Cannot load mkl_intel_thread.dll in Python on Windows
Resolved an error when putting pygame in python3 on raspberry pi
A game to go on an adventure in python interactive mode
When I get an error with Pylint in Atom on Windows
How to use python put in pyenv on macOS with PyCall
How to use an external editor for Python development with Grasshopper
[Python] Show multiple windows in Tkinter
Python environment construction memo on Windows 10
Python 3.6 on Windows ... and to Xamarin.
Installing Kivy on Windows10 64bit Python3.5
Check for external commands in python
Hit a command in Python (Windows)
Anaconda python environment construction on Windows 10
Convert markdown to PDF in Python