In the introduction to our documentation for PDF2PRINT, we claim that you can use it to send PDF documents to the printer of your choice, either manually from a command prompt or as part of a batch file. I will occasionally whip up a batch file to cut down on key strokes on the command line, but for something serious, I am more likely to turn to Python. So, I thought I would look into calling PDF2PRINT from Python. It wasn’t quite as straightforward as I thought it would be, so I’m sharing some insights.

Here, I’m using the subprocess module to run the PDF2PRINT executable to print a file from my hoard of documentation to my default printer. The subprocess module is intended to supersede the os.spawn* functionality I was originally thinking I would use. The run() function was introduced in Python v3.5, and for my tests, I’m using Python 3.6.

First test, everything works as expected.

However, things got a bit wonky when I tried to explicitly specify the printer to print from.

What worked was to join the arguments together and use subprocess.Popen rather than subprocess.run:

I suspect the issue might be the quotes around the printer name, but it’s a weird problem. Neither just joining the arguments, nor switching from run to Popen resolved the issue on its own. It had to be both.

Once I got past that, I turned to experimenting with adding JSON to the mix. I created an array of input file paths and ranges. An array with a single element.

Then I added this array as the input-files entry in a control dictionary.

Basically, this is a control structure that says to print the first couple of pages of a given document.

For the next part, I create a temporary file with a json extension, and I write out the control dictionary as JSON. Then, I call PDF2RINT

And, how about that… I now have more scratch paper!

If you’re interested in PDF2PRINT, click here to download a free evaluation or purchase.

Share this post with your friends

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published. Required fields are marked *