Howard Katz
asked on
Problem printing .pdf from Foxpro
Hello Experts,
I need to simply print a .pdf file on a user-selected printer from a VFP9 app. The user doesnt want to view the file, and it gets erased after its printed. Here is the code I wrote. It works when I step through the debugger, but otherwise it doesnt. Even putting in delays doesnt help. Without the debugger the printer queue says Spooling after the PrintAll() call for the entire time of the Sleep() delay. Then it briefly flashes Printing, and then the document is deleted from the queue. It only prints part of the first page and stops. Also, the program hangs. In the debugger everything works OK.
Here is the code:
PROCEDURE PrintPDF
LPARAMETERS tcFileName, tcPrinter
LOCAL loform1, lcOldPrinter
DECLARE INTEGER SetDefaultPrinter IN winspool.drv;
STRING pszPrinter
DECLARE Sleep IN Win32API INTEGER nMilliseconds
** If no file, get out
IF !FILE(tcFileName)
RETURN
ENDIF
WAIT WINDOW 'printing' NOWAIT
lcOldPrinter = SET("PRINTER",2)
** Set the default windows printer
SetDefaultPrinter(tcPrinte r)
loform1=Newobject("form")
WAIT WINDOW 'adding AcroPDF object ...' nowait
loform1.AddObject("olecont rol2","Acr oPDF1")
*=Sleep(3000)
WAIT WINDOW 'loading file ...' nowait
loform1.olecontrol2.loadfi le(tcFileN ame)
=Sleep(3000)
WAIT WINDOW 'printing ...' nowait
loform1.olecontrol2.printa ll()
=Sleep(20000)
loform1.RemoveObject("olec ontrol2")
loform1.Release()
ERASE (tcFileName)
** Now reset the printer
SetDefaultPrinter(lcOldPri nter)
RETURN
Define Class AcroPDF1 As OleControl
OleClass = "AcroPDF.PDF.1"
Height = 0
Width = 0
ENDDEFINE
I need to simply print a .pdf file on a user-selected printer from a VFP9 app. The user doesnt want to view the file, and it gets erased after its printed. Here is the code I wrote. It works when I step through the debugger, but otherwise it doesnt. Even putting in delays doesnt help. Without the debugger the printer queue says Spooling after the PrintAll() call for the entire time of the Sleep() delay. Then it briefly flashes Printing, and then the document is deleted from the queue. It only prints part of the first page and stops. Also, the program hangs. In the debugger everything works OK.
Here is the code:
PROCEDURE PrintPDF
LPARAMETERS tcFileName, tcPrinter
LOCAL loform1, lcOldPrinter
DECLARE INTEGER SetDefaultPrinter IN winspool.drv;
STRING pszPrinter
DECLARE Sleep IN Win32API INTEGER nMilliseconds
** If no file, get out
IF !FILE(tcFileName)
RETURN
ENDIF
WAIT WINDOW 'printing' NOWAIT
lcOldPrinter = SET("PRINTER",2)
** Set the default windows printer
SetDefaultPrinter(tcPrinte
loform1=Newobject("form")
WAIT WINDOW 'adding AcroPDF object ...' nowait
loform1.AddObject("olecont
*=Sleep(3000)
WAIT WINDOW 'loading file ...' nowait
loform1.olecontrol2.loadfi
=Sleep(3000)
WAIT WINDOW 'printing ...' nowait
loform1.olecontrol2.printa
=Sleep(20000)
loform1.RemoveObject("olec
loform1.Release()
ERASE (tcFileName)
** Now reset the printer
SetDefaultPrinter(lcOldPri
RETURN
Define Class AcroPDF1 As OleControl
OleClass = "AcroPDF.PDF.1"
Height = 0
Width = 0
ENDDEFINE
ASKER
This works, but the Adobe Reader pops up on the Task bar (I have Adobe 7.0). Can you suggest code to find it and kill it? What if the client has another version of the Reader?
That ShellExecute procedure uses the default PDF reader on a PC. I only suggested it because most users don't have the commercial version of Abobe (nor do I) to call up and/or print a PDF.
And yes there is a way to kill the Adobe Reader window. I'll get it for you shortly-- I have it on another PC inaccessible to the network this PC is on. The issue as you have found out is utilizing the Sleep function to wait for the process to finish its print job before you actually kill it.
And yes there is a way to kill the Adobe Reader window. I'll get it for you shortly-- I have it on another PC inaccessible to the network this PC is on. The issue as you have found out is utilizing the Sleep function to wait for the process to finish its print job before you actually kill it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. Haven't had a chance to try it but thanks for your effort and quick response. If any other issues appear when I do try, I will re post.
You're welcome.
I just know it all works for me in one old VFP6 application.
I just know it all works for me in one old VFP6 application.
declare integer ShellExecute ;
in shell32.dll;
long hwnd, string cOperation, string cFile, string cParam, string cDir, integer nShow
? shellexecute(0, "print", "d:\temp\onepage.pdf","",f