user808856
user808856

Reputation: 111

Printing a file in python using subprocess or os results in stopped printer job in queue

This problem is for code I am writing relating to this question here. I now have been able to create the pdf file the way I want. However, when I try to print it, I run into issues. Here is the relevant code (Ubuntu 22.04, python 3.10)

        with open(examfile, "wb") as fp:
            writer.write(fp)
            conn.printFile(results.printer, examfile, " ", {"Staple","1"})

            # os.system("lp -d " + results.printer + " -o Staple=1 " + examfile)
            # subprocess.run(["lp","-d",results.printer,"-o","Staple=1",examfile],capture_output=True)

results.printer and examfile are strings with the printer name, and name of the file, respectively. When the code is run, the file is created correctly, and the above code sends it to the printer, but it shows up in the printer queue as "STOPPED". If I run the command by hand in a terminal, it sends the file to the printer and prints it.

As you can see the end comment, I tried using subprocess.run, and I also tried os.system. After reading some posts on stack overflow, I tried pycups. All of them exhibited the same behavior. The files show up in the print queue and are stopped. I can't seem to get them released from there. I could create all of the files, and then send them to the printer separately from the command line, but I am potentially dealing with hundreds of files, and I'd rather print them from python. It also potentially increases my storage requirements by more than I would like. I had planned to print and then delete each file after printing.

I've thought it might be some kind of setup issue on my machine, but as the lp command works on the command line, I don't know what the issue is.

As is usual, there is probably something simple I'm not considering. I would appreciate it if you would let me know what it is. Thanks,

Upvotes: 0

Views: 180

Answers (1)

KamilCuk
KamilCuk

Reputation: 141995

.write is buffered. fp.flush() before expecting the file to have the content. Common mistake when using tempfile.NamedTemporaryFile.

Upvotes: 0

Related Questions