Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silent printing of a PDF in Python

I'm trying to print a PDF with Python, without opening the PDF viewer application (Adobe, Foxit etc.). I need also to know when printing has finished (to delete the file).

Here I found this implementation:

import win32ui, dde, os.path, time
from win32api import FindExecutable
from os import spawnl, P_NOWAIT
...
pd = "C:\\temp\\test.pdf"
pdbits = os.path.split(pd)
readerexe = FindExecutable(pdbits[1],pdbits[0])

spawnl(P_NOWAIT,readerexe[1],"DUMMY") #I added "DUMMY" to avoid a weird error

time.sleep(2)

s = dde.CreateServer()
s.Create('')
c = dde.CreateConversation(s)
c.ConnectTo('acroview', 'control')

c.Exec('[FilePrintSilent("%s")]' % (pd,))

s.Destroy()

But it throws this exception at the ConnectTo line:

dde.error: ConnectTo failed

Someone knows how to solve it? Or has a different solution for silent printing? Or at list can give a link to a reference for ConnectTo? Could find nothing on the web about it.

Working with: Python 2.7, Windows 7, Acrobat Reader 10.0

like image 877
bluish Avatar asked Dec 21 '10 10:12

bluish


People also ask

Can you prevent a PDF from being printed?

To prevent printing of a PDF file, uncheck the "Allow the document to be printed" option. You must always enter a master password when security options are enabled or when a user password is set.

Can you scrape a PDF with Python?

With the help of python libraries, we can save time and money by automating this process of scraping data from PDF files and converting unstructured data into panel data.


1 Answers

I suggest you install GSView and GSPrint and shell out to gsprint.exe to print the pdf.

p = subprocess.Popen([r"p:\ath\to\gsprint.exe", "test.pdf"], 
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr

I have used this in a industrial label printing solution, works great.

When the gsprint.exe program exits (i.e. after the call to communicate), you can delete the pdf file.

like image 112
codeape Avatar answered Oct 19 '22 19:10

codeape