PDFCreator 2.0 and Visual FoxPro 9.0

I have a client who has been happily using PDFCreator for the past 3 years with her Visual FoxPro 9.0 program. It works like a whistle. Even after PDFCreator updates. No problems. Until now. We have had to revert back to PDFCreator 1.7 so they continue to convert foxpro reports to PDF.

I am adding the PDF conversion code that I have had written. You’ll see that I am now checking to see which version of PDFCreator a person has before moving forward. I have looked over the PDFCreator Manual and looked at the COM Object. But I keep getting error after error message.

FUNCTION PrintPDF
* output report to PDF file
* lcReport = name of the FoxPro report file
* lcName = name of the PDF file
***********************************************************
PARAMETERS lcReport, lcName
LOCAL lcNewFile, lcReportFile, lnCount, lcSeconds
lcSeconds = 0
lcNewFile = 'c:\temp\ewFile.pdf’
lcReportFile = STRTRAN(lcName, ‘"’, “_”)

IF FILE(lcReportFile)   
    DELETE FILE (lcReportFile)
ENDIF

IF FILE(lcNewFile)   
    DELETE FILE (lcNewFile)
ENDIF

WAIT “Preparing PDF Attachments…” WINDOW AT 35, 35 NOWAIT TIMEOUT 5

LOCAL opdfcreator, isrunning, xpdfversion, oPdfCreatorQueue

isrunning = .F.
xPdfVersion = 0

!    *Check to see which version of PDFCreator user is using.
LOCAL xfileName, theString
xfileName = "C:\Program Files\PDFCreator\pdfcreator.exe"

IF FILE(xfilename)
    DIMENSION verArray(1)
    theVal = AGETFILEVERSION(verArray,xfilename)
   
    IF theVal != 0
        theString = ALLTRIM(verArray(4))
        xPdfVersion = VAL(LEFT(theString,1))   
    ENDIF   
ENDIF


IF xpdfVersion >= 2
    *PDFCreator is new version, 2.0 or higher
    opdfcreator = CREATEOBJECT(“PDFCreatorBeta.PDFCreator”)
   
    isrunning = opdfcreator.IsInstanceRunning()
   
    IF isrunning = .F.
        IF FILE(“C:\Program Files\PDFCreator\pdfcreator.exe”)
            RUN /n2 “C:\Program Files\PDFCreator\pdfcreator.exe”
        ENDIF
           
    ENDIF
   
    _screen.MousePointer = 11
   

    oPdfCreatorQueue = CREATEOBJECT(“PDFCreatorBeta.JobQueue”)   
    oPdfCreatorQueue.Initialize()
   
    REPORT FORM (lcReport) TO PRINTER
    SET PRINTER TO default   
       
    * allow up to [LCSECONDS] sec for PDF file to be created
    FOR lnCount = 1 TO lcseconds
        =INKEY(1, ‘H’)
        IF FILE(lcNewFile)
            EXIT
        ENDIF
    ENDFOR
    =INKEY(1, ‘H’)
   
       
    IF FILE(lcNewFile)   
        RENAME (lcNewFile) TO (lcReportFile)
    ELSE   
        MESSAGEBOX(“PDF File was not created. Please inform System Programmer.”)
    ENDIF

    oPdfCreatorQueue.releasecom()
ELSE
    *PDFCreator is a version below 2.0.
    opdfcreator = CREATEOBJECT(“PDFCreator.clsPDFCreator”)   
                   
    isrunning = opdfcreator.cProgramIsRunning()

    IF isrunning = .F.
        IF FILE(“C:\Program Files\PDFCreator\pdfcreator.exe”)
            RUN /n2 “C:\Program Files\PDFCreator\pdfcreator.exe”
        ENDIF           
    endif

    _screen.MousePointer = 11


    lcSeconds = 60
    SET PRINTER TO NAME PDFCreator       

    REPORT FORM (lcReport) TO PRINTER
    SET PRINTER TO default

    * allow up to [LCSECONDS] sec for PDF file to be created
    FOR lnCount = 1 TO lcseconds
        =INKEY(1, ‘H’)
        IF FILE(lcNewFile)
            EXIT
        ENDIF
    ENDFOR
    =INKEY(1, ‘H’)

    IF FILE(lcNewFile)   
        RENAME (lcNewFile) TO (lcReportFile)
    ELSE   
        MESSAGEBOX(“PDF File was not created. Please inform System Programmer.”)
    ENDIF
ENDIF

RELEASE opdfcreator, isrunning, xPdfVersion, opdfcreatorqueue
RELEASE xfileName, theString

_screen.MousePointer = 1
RETURN




I have been trying to get the code to work with FoxPro using PDFCreatorQueue.NextJob to create a job object but it isn’t working.

oJob = oPDFCreatorQueue.NextJob()

That keeps telling me Ole IDispatcher exception code 0 from PDFCreator: Invalid Index. Please check index parameter.

Followed by:

ojob not an object.

Is there anybody out there who has successfully gotten Visual FoxPro to talk to and use PDFCreator 2.0 COM object?

Thank you
Brandy Williams

Hello,


although I’m not familiar with FoxPro, I think I have encountered 2 big problems that lead to exceptions in your program:

1. 
IF isrunning = .F.
        IF FILE(“C:\Program Files\PDFCreator\pdfcreator.exe”)
            RUN /n2 “C:\Program Files\PDFCreator\pdfcreator.exe” 
        ENDIF            
    endif
*some lines below
oPdfCreatorQueue = CREATEOBJECT(“PDFCreatorBeta.JobQueue”)

In these lines, as far as I understood, you check if a PDFCreator instance is already running and if it is not, you start it. But here is the problem: You cannot start a PDFCreator instance and after that trying to get a COM Object of that. So here is what has actually to be done: 
Check if PDFCreator instance is running, if it is wait or close it, that’s up to you, but what is important is that the instance must be closed before calling CREATEOBJECT(PDFCreatorBeta.JobQueue).

2. 
After these lines
REPORT FORM (lcReport) TO PRINTER
    SET PRINTER TO default
Your further workflow with the COM interface is not correct, therefore I would recommend you to take a look at the workflow from the user manual (can be found in PDFCreatorManual/COM Interface/User Manual/Basics/Convert a single file) and additionally take a look at the provided COM Scripts (can be found in PDFCreator directory go to COM Scripts/JS Scripts/Basics/TestPage2PDF.js). Both resources point out the workflow principles.

I hope that this will help you,

Regards,
Sazan 

Thank Sazan. I have removed the COM object calls and ended up making it alot simplier than I had originally made it.

After checking to see if there is an instance of PDFCreator, and if it is not running, run it, I then used my previous code this way (without COM object):

lcSeconds = 60
SET PRINTER TO NAME PDFCreator
   
REPORT FORM (lcReport) TO PRINTER
SET PRINTER TO default

* allow up to [LCSECONDS] sec for PDF file to be created
FOR lnCount = 1 TO lcseconds
    =INKEY(1, ‘H’)
    IF FILE(lcNewFile)
        EXIT
    ENDIF
ENDFOR
=INKEY(1, ‘H’)
   
IF FILE(lcNewFile)   
    RENAME (lcNewFile) TO (lcReportFile)
ELSE   
    MESSAGEBOX(“PDF File was not created. Please inform System Programmer.”)
ENDIF

It is working like a charm! Thank you!

You’re welcome! :slight_smile: