Jump to content

Automating "Printing to PDF (using CutePDF or Acrobat)" with AutoIT


SnYder
 Share

Recommended Posts

On a monthly basis I print close to fifty (50) reports from a variety of applications to PDF using either CutePDF or Acrobat (standard version). I'm trying to automate this function but was unable to find a suitable AutoIT function to do so. Could somebody help please. Thanks.

Documents are spread across several sub-directories ...

@MyDocumentsDir\<Dir#1>

@MyDocumentsDir\<Dir#2>

@MyDocumentsDir\<Dir#3>

@MyDocumentsDir\<Dir#4>

@MyDocumentsDir\<Dir#5> ... so on and forth.

There are four or five specific files that I need for my reports from each of these directories.

Adobe "Convert to PDF" is available in the context menu (when I right click on the documents).

Both Adobe PDF printer as well as CutePDF printers are available from the Print dialogue.

Certainly appreciate all the help.

:whistle:

Link to comment
Share on other sites

I dont have a code as such. I do have a few examples that I can tailor all the way up to what I want finally achieve. I still don't see a way to print to a printer.

Printing to a printer is not a problem. You have to accept that AutoIt is like building blocks. While it may not contain all of the functionality that you need, it contains all of the functions that you need in order to build the functionality yourself. I'm trying to get you to think outside the box. If AutoIt doesn't have native printer functionality, what do you do? Posting here first is not the answer. You Google, you look on MSDN, you search the forums. Then, you post code here showing that you've at least tried on your own to solve the problem. I'll help somebody who shows that they've tried any time. I feel reluctant to answer posts that lean toward "write this for me".

I could give you a fish, but then you'd be asking me everyday for one. If I teach you how to fish, then you can feed yourself.

Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

Printing to a printer is not a problem. You have to accept that AutoIt is like building blocks. While it may not contain all of the functionality that you need, it contains all of the functions that you need in order to build the functionality yourself. I'm trying to get you to think outside the box. If AutoIt doesn't have native printer functionality, what do you do? Posting here first is not the answer. You Google, you look on MSDN, you search the forums. Then, you post code here showing that you've at least tried on your own to solve the problem. I'll help somebody who shows that they've tried any time. I feel reluctant to answer posts that lean toward "write this for me".

I could give you a fish, but then you'd be asking me everyday for one. If I teach you how to fish, then you can feed yourself.

If you can't help and unwilling just don't do it. Don't tell me what life is. I have lived many moons on this "third rock from sun" and I have embraced computers with great adoration and admiration. Just don't think you are a know it all. Remember you were a toddler one day and grew up to be what you are.

Sometimes we all need a little help, some more than others. I sincerely hope that dont find yourself in a situation where you would need help. As for the coding I'm perfectly capable of doing it myself if I put myself to it. Well thanks for your FISH and sweet nothings! May god bless you!

:whistle:

Link to comment
Share on other sites

Class is now in session... ;)

If you can't help and unwilling just don't do it

If I didn't want to help, I wouldn't have posted to you in the first place. I'm still willing to help you even now, which is the difference between the two of us.

I have lived many moons on this "third rock from sun" and I have embraced computers with great adoration and admiration.

Then try embracing Google, MSDN and the search feature on this forum. You would have found your answer easily in any one of these readily available resources.

Remember you were a toddler one day and grew up to be what you are.

Still toddling along. And one of the first things I was "taught" here is RTFM and STFW. Some members were nice, others weren't, but I thank them all for the lesson.

Sometimes we all need a little help, some more than others. I sincerely hope that dont find yourself in a situation where you would need help.

Your new around here. Look around and you'll see I ask for assistance from time to time. The difference is, 1) I try to work the problem out myself first, 2) I spend time search the web and this forum if I can't and 3) I try to post some code to show that I've I'm not asking people to do my grunt work for me.

As for the coding I'm perfectly capable of doing it myself if I put myself to it

So what you're really saying is you're just plain lazy.

Class dismissed... :whistle:

Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

Class is now in session... ;)

If I didn't want to help, I wouldn't have posted to you in the first place. I'm still willing to help you even now, which is the difference between the two of us.

Then try embracing Google, MSDN and the search feature on this forum. You would have found your answer easily in any one of these readily available resources.

Still toddling along. And one of the first things I was "taught" here is RTFM and STFW. Some members were nice, others weren't, but I thank them all for the lesson.

Your new around here. Look around and you'll see I ask for assistance from time to time. The difference is, 1) I try to work the problem out myself first, 2) I spend time search the web and this forum if I can't and 3) I try to post some code to show that I've I'm not asking people to do my grunt work for me.

So what you're really saying is you're just plain lazy.

Class dismissed... :whistle:

Mr IA: You really tickle me :P. It is amazing to see how people draw conclusions without even a clue what the other person could really be. I'll set aside all this for a moment and post the code here and see what you can come up with.

[code=auto:0]Dim $SelUpldCas = ""
Dim $dir0int = ""
Dim $orgdeflpt = ""
Dim $altdeflpt = "Acrobat PDFWriter,winspool,LPT1:"

WinMinimizeAll ()

Func OrgDefLpt ()
    $orgdeflpt = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device")
EndFunc

Func AltDefLpt ()
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device","REG_SZ",$altdeflpt)
EndFunc

Func ResOrgLpt ()
    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device","REG_SZ",$orgdeflpt)
EndFunc


Func MyWinExplr ()
    $winexplr = ObjCreate("InternetExplorer.Application")
    with $winexplr
        .Visible = 1
        .Toolbar = 1
        .Navigate($dir0int)
    EndWith
EndFunc

Func MySelCasDlg ()
    $SelUpldCas = InputBox("What Case ??", "To continue please enter the case number to create report" &@LF&@LF& "Valid numbers are from 1 thro 5","", " M1")
    If @error = 1 Then
        MsgBox(0, "Terminated", "The report generation script was terminated by you.")
        Exit
    EndIf
    Select
        case $SelUpldCas = "1"
            $dir0int = @MyDocumentsDir&"\UpLoads#1\"
            MyWinExplr ()
        case $SelUpldCas = "2"
            $dir0int = @MyDocumentsDir&"\UpLoads#2"
            MyWinExplr ()
        case $SelUpldCas = "3"
            $dir0int = @MyDocumentsDir&"\UpLoads#3"
            MyWinExplr ()
        case $SelUpldCas = "4"
            $dir0int = @MyDocumentsDir&"\UpLoads#4"
            MyWinExplr ()
        case $SelUpldCas = "5"
            $dir0int = @MyDocumentsDir&"\UpLoads#5"
            MyWinExplr ()
        Case $SelUpldCas = "0"
            MsgBox (0,"STOP!", "You entered a case that is invalid. At this time only cases #1 thro' #5 are possible. Please reenter the corrct case #")
            MySelCasDlg ()
        Case $SelUpldCas >= "6"
            MsgBox (0,"STOP!", "You entered a case that is invalid. At this time only cases #1 thro' #5 are possible. Please reenter the corrct case #")
            MySelCasDlg ()
    EndSelect
EndFunc

Func MyFilSel ()
    Send("{HOME}")
    Sleep(3000)
    Send("{p}") ; This would be the first character of the filename
    Send("{r}") ; This would be the second character of the filename
    Send("{e}") ; This would be the third character of the filename
    Send("{x}") ; This would be the fourth character of the filename
    Send("{x}") ; This would be the fifth character of the filename
    Send("{X}") ; This would be the sixth character of the filename
    Send("{X}") ; This would be the seventh character of the filename
    Send("{ENTER}") ; This will open the document with the associated application
    Sleep(3000)
    Send("{CTRLDOWN}{P}") ; Print Dialogue
    Send("{CTRLUP}")
;~  An alternate method would be to use "Shift + F10" for context menu and select function, in my case it would be "convert to PDF".  The lines will change depending on the methodology employed.
    Sleep(2000)
    Send("{ESC}") ; Print dialogue quit
    Send("{CTRLDOWN}{q}")
    Send("{CTRLUP}") ; Application & document quit
    Sleep(1000)
    Send("{ALTDOWN}{F4}")
    Send("{ALTUP}") ; Explorer window quit
EndFunc

OrgDefLpt ()
AltDefLpt ()
MySelCasDlg ()
MyFilSel ()
ResOrgLpt ()

Exit


            
                


    Edited  by SnYder
    
    

            
        

        

        
    

    

    




    Link to comment
    
        
    
    
    

    
    Share on other sites
    

    
        
            

    

        
            

    

        
            

    

        
            

    

        
    


    
    More sharing options...

    


    

                    
                    
                    
                

                    

                    
                    





    

    

    
        
            
                


    
        
    

                
                
                    
                        

                    
                
            
        
        
            
                


MHz
            
            
                Posted 
                
            
        
    
    
        


MHz
            
        
        
            
                
                    


    
        
    

                    
                    
                        

                    
                
            
            
                MVPs
                
                    
                
            
            
                
                    
                        
                            
                                
                            
                                 5.6k
                            
                                
                            
                        
                        
                            
                                
                                    
                                        
                                        30
                                
                                    
                                
                            
                        
                    
                
            
            
                

    
    
        
Just simple
    
    

            
        
    
    
        



    
        
            
                
                    
                    
                    
                    
                    
                
            
            
                
                    
                    
                        
                        
                            Share
                        
                        
                        
                        
                        
                            
                                
                            
                            
                            
                            
                            
                            
                        
                    
                
                
            
        

        
            Posted 
            
            
                
                
            
        
    

    

    

    
        
        
            IMHO, you should have a look at using PDFCreator. PDFCreator supports COM which you can script using it's objects with good results expected.

A sample script (others available) from the install directory that does a testpage to PDF.
CODE
ScriptBaseName = fso.GetBaseName(Wscript.ScriptFullname)

AppTitle = "PDFCreator - " & ScriptBaseName

If CDbl(Replace(WScript.Version,".",",")) < 5.1 then
 MsgBox "You need the ""Windows Scripting Host version 5.1"" or greater!", vbCritical + vbSystemModal, AppTitle
 Wscript.Quit
End if

Set WshShell = WScript.CreateObject("WScript.Shell")

Set PDFCreator = Wscript.CreateObject("PDFCreator.clsPDFCreator", "PDFCreator_")
PDFCreator.cStart "/NoProcessingAtStartup"

ReadyState = 0
With PDFCreator
 .cOption("UseAutosave") = 1
 .cOption("UseAutosaveDirectory") = 1
 .cOption("AutosaveDirectory") = fso.GetParentFolderName(Wscript.ScriptFullname)
 .cOption("AutosaveFilename") = "Testpage - PDFCreator"
 .cOption("AutosaveFormat") = 0                            ' 0 = PDF
 DefaultPrinter = .cDefaultprinter
 .cDefaultprinter = "PDFCreator"
 .cClearcache
 .cPrintPDFCreatorTestpage
 .cPrinterStop = false
End With

c = 0

Do While (ReadyState = 0) and (c < (maxTime * 1000 / sleepTime))
 c = c + 1
 Wscript.Sleep sleepTime
Loop

With PDFCreator
 .cDefaultprinter = DefaultPrinter
 WScript.Sleep 200
 .cClose
End With

If ReadyState = 0 then
 MsgBox "Creating test page as pdf." & vbcrlf & vbcrlf & _
  "An error is occured: Time is up!", vbExclamation + vbSystemModal, AppTitle
End If

'--- PDFCreator events ---

Public Sub PDFCreator_eReady()
 ReadyState = 1
End Sub

Public Sub PDFCreator_eError()
 MsgBox "An error is occured!" & vbcrlf & vbcrlf & _
  "Error [" & PDFCreator.cErrorDetail("Number") & "]: " & PDFcreator.cErrorDetail("Description"), vbCritical + vbSystemModal, AppTitle
 Wscript.Quit
End Sub

Code like this can be converted to AutoIt's COM ability and used.


            
        

        

        
    

    

    




    Link to comment
    
        
    
    
    

    
    Share on other sites
    

    
        
            

    

        
            

    

        
            

    

        
            

    

        
    


    
    More sharing options...

    


    

                    
                    
                    
                

                    

                    
                    





    

    

    
        
            
                


    
        
    

                
                
                    
                        

                    
                
            
        
        
            
                


PaulIA
            
            
                Posted 
                
            
        
    
    
        


PaulIA
            
        
        
            
                
                    


    
        
    

                    
                    
                        

                    
                
            
            
                MVPs
                
                    
                
            
            
                
                    
                        
                            
                                
                            
                                 1.2k
                            
                                
                            
                        
                        
                            
                                
                                    
                                        
                                        1
                                
                                    
                                
                            
                        
                    
                
            
            
                

    
    
        
API Extremist
    
    

            
        
    
    
        



    
        
            
                
                    
                    
                    
                    
                    
                
            
            
                
                    
                    
                        
                        
                            Share
                        
                        
                        
                        
                        
                            
                                
                            
                            
                            
                            
                            
                            
                        
                    
                
                
            
        

        
            Posted 
            
            
                
                    (edited)
                
                
            
        
    

    

    

    
        
        
            
Mr IA: You really tickle me . It is amazing to see how people draw conclusions without even a clue what the other person could really be. I'll set aside all this for a moment and post the code here and see what you can come up with.I have nothing to draw conculsions on except what you have posted here. You would do well to have a change of heart about how you treat people who are trying to help you.  MHZ has posted the best solution, but if you're not a COM programmer you can do what you were trying to do using Auto3Lib and the following code:

CODE#include <A3LListView.au3>
#include <A3LMenu.au3>

Opt("MustDeclareVars", 1)
Opt("SendKeyDelay"   , 1)

; =================================================================================================
; AutoIt Version : 3.2.+
; Author ........: Paul Campbell
; Description ...: Print documents using the context menu
; Notes .........: You need a current copy of Auto3Lib off of the forum for this to work.
; =================================================================================================

; =================================================================================================
; Global Constants
; =================================================================================================

Global Const $sFileName = "File.doc"
Global Const $sPrefix   = "My Documents\Uploads#"

; =================================================================================================
; Global Variables
; =================================================================================================

Global $sTitle = "My Documents"
Global $iCaseID

; =================================================================================================
; Main
; =================================================================================================

GetCaseID()
OpenExplorer()
ChangeDir()
PrintFile()

; =================================================================================================
; Get Case ID
; =================================================================================================
Func GetCaseID()
  while 1
    $iCaseID = InputBox("What Case??", _
                        "To continue please enter the case number to create report" & @LF & _
                        "Valid numbers are from 1 through 5", "1", " M1", 310, 130)
    if @Error then _ShowError("Report generation terminated")
    if ($iCaseID >= 1) and ($iCaseID <= 5) then Return
    _ShowMsg("Invalid case number.  Valid numbers are 1 through 5")
  wend
EndFunc

; =================================================================================================
; Open Explorer
; =================================================================================================
Func OpenExplorer()
  Run("c:\windows\explorer.exe")
  _WinWaitActive($sTitle)
EndFunc

; =================================================================================================
; Change to the correct directory
; =================================================================================================
Func ChangeDir()
  ControlFocus($sTitle, "", "Edit1")
  ControlSend ($sTitle, "", "Edit1", $sPrefix & $iCaseID, 1)
  ControlSend ($sTitle, "", "Edit1", "{Enter}")
  $sTitle = ControlGetText("", "", "Edit1")
EndFunc

; =================================================================================================
; Print the file
; =================================================================================================
Func PrintFile()
  Local $hList, $iIndex

  $hList = ControlGetHandle($sTitle, "", "SysListView321")
  if $hList = 0 then _ShowError("Unable to find ListView")
  $iIndex = _ListView_FindString($hList, $sFileName, -1, False)
  if $iIndex = -1 then _ShowError("Unable to find filename")
  _ListView_ClickItem($hList, $iIndex, "right", True)

  ; I'm going to just print the document using the "Print" context menu item.  You need to change this
  ; to select the correct context menu item for "Convert to PDF".
  ; --------------------------------------------------------------------------------------------------
  _Menu_ClickPopupAccel("P")
EndFunc
Class dismissed... :whistle: Edited by PaulIA
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...