Thread: FoxPro Some General Questions/Using Amyuni PDFConverter With VFP

Using Amyuni PDFConverter With VFP
Using Amyuni PDFConverter With VFP

*-----------------------------
* Program.....: PrintToPDF.prg
*
* Author......: Rick Borup
*
* Date Written: 14-Aug-2000
*
* Parameters..: tcReportName - the name of the VFP report to be run
* tcFileName - the name of the PDF file to be created
*
* Returns.....: Logical
*
* Abstract....: Print a report to PDF format using the Amyuni
* PDF compatible printer driver via a DLL interface.
*
* NOTE: CDINTF.DLL must be registered on your machine
* (REGSVR32 CDINTF.DLL)
*  or CDINTF210.DLL if you're using Amyuni 2.10
*
#DEFINE pdf_NoPrompt 1 && do not prompt for file name
#DEFINE pdf_UseFileName 2 && use file name set by SetDefaultFileName else use document name
#DEFINE pdf_Concatenate 4 && concatenate files, do not overwrite
#DEFINE pdf_DisableCompression 8 && disable page content compression
#DEFINE pdf_EmbedFonts 16 && embed fonts used in the input document
#DEFINE pdf_BroadcastMessages 32 && enable broadcasting of PDF events
* For Amyuni 2.10...
#DEFINE pdf_AmyuniLicenseCompany "Company Name" && name of company Amyuni is licensed to
#DEFINE pdf_AmyuniLicenseCode "ABCD1234..." && license code issued by Amyuni

lParameters tcReportName, tcFileName
* The tcReportName paremeter is required.
if vartype( tcReportName) <> "C"
   ASSERT .F. MESSAGE "Parameter tcReportName is missing or invalid"
   RETURN .F.
endif
* The tcFileName parameter is optional and defaults to tcReportName if omitted.
if vartype( tcFileName) <> "C"
   tcFileName = ALLTRIM( tcReportName) + ".PDF"
endif
* Append a .PDF extension if it's not already there.
if UPPER( SUBSTR( tcFileName, LEN( ALLTRIM( tcFileName)) - 3)) <> ".PDF"
   tcFileName = tcFileName + ".PDF"
endif

* Create an instance of the Amyuni PDF Compatible Printer Driver control.
oPDFPrinter = CREATEOBJECT( "CDINTF.CDINTF")
*!*oPDFPrinter = CREATEOBJECT( "CDINTFEX.CDINTFEX") && for Amyuni 2.10
if vartype( oPDFPrinter) <> "O"
   RETURN .F.
endif

* Define memvars.
private pnErrorCount
pnErrorCount = 0
local lcOldError, lcPrinterName
lcOldError = on("error")
lcPrinterName = set("Printer", 3) && Save the current VFP printer name.

* Set up our error handler, which will display LASTERRORMSG() from the PDF driver.
on error do ErrHand

* Initiate driver with name "PDF Compatible Printer Driver".
* ("PDF Compatible Printer Driver" will now appear in the list of printers.)
oPDFPrinter.PDFDriverInit( "PDF Compatible Printer Driver")
* If Amyuni PDF printer is permanently installed use
* oPDFPrinter.DriverInit( "PDF Compatible Printer Driver")
*!* Set the destination file name.
* If path is included, use it (user may want file in a diffent folder)
if tcfilename=justfname(tcfilename)
   oPDFPrinter.DefaultFileName = SYS(5) + ADDBS( SYS(2003)) + tcFileName
else
   * leave the path as the user set it...
   oPDFPrinter.DefaultFileName = tcFileName
endif

*!* set resolution to 1200 for the best quality
oPDFPrinter.resolution = 1200

*!* update driver info with resolution info
oPDFPrinter.SetDefaultConfig()

* Note: Message broadcasting should be enabled in order to insert bookmarks from VFP.
* But see the notes in the AddBookmark function below!
oPDFPrinter.FileNameOptions = pdf_NoPrompt + pdf_UseFileName + pdf_BroadcastMessages

* Save the current Windows default printer so we can restore it later.
oPDFPrinter.SetDefaultPrinter()

* Set the VFP printer name to the PDF printer, and print the report.
set printer to name "PDF Compatible Printer Driver"
* For version 2.10 we need to call EnablePrinter() and pass the license information.
oPDFPrinter.EnablePrinter( pdf_AmyuniLicenseCompany, pdf_AmyuniLicenseCode)
REPORT FORM ( tcReportName) NOEJECT NOCONSOLE TO PRINTER

* Restore the Windows default printer.
oPDFPrinter.RestoreDefaultPrinter()

* Restore the previous error handler.
on error &lcOldError

* Restore the VFP printer name to the previous name.
set printer to name "&lcPrinterName"

* Uninstall the printer driver we installed with PDFDriverInit()
oPDFPrinter.DriverEnd()

RETURN pnErrorCount = 0


* Adds a bookmark to the report at the current print location.
FUNCTION AddBookmark
PARAMETERS PageNo
*!* THIS WILL NOT WORK unless we have the actual device context (hDC)
*!* to pass as the first parameter to SetBookmark. Use the FLL interface
*!* if we need to insert bookmarks.
oPDFPrinter.SetBookmark( 0, "Test" + STR(PageNo))
RETURN ""

* error handling: displays last error message
PROCEDURE ErrHand
*!*? "Error message: " + LASTERRORMSG() && This syntax is only for the FLL.
? "Error message: " + oPDFPrinter.GetLastErrorMsg() && This syntax is for the DLL.
pnErrorCount = pnErrorCount + 1
RETURN

-----------------------

*** PRINT EXCEL SPREADSHEET
oXl = CREATEOBJECT('Excel.Application')

oWorkbook = oXl.Workbooks.OPEN(BookName)

oSheet = oWorkbook.Worksheets["Sheet1"]

lcSavedPrinter = oXl.ActivePrinter

oXl.ActivePrinter = LocalPdfPrinter + " on LPT1:" && "PDF Compatible Printer Driver on LPT1:"

*** PrintOut(From,To,Copies,Preview,ActivePrinter,PrintToFile,Collate,PrToFileName)
oSheet.Printout(,,,,,,,LocalPdfFile)

oXl.ActivePrinter = lcSavedPrinter

oWorkbook.CLOSE(.F.)



Converter to MS EXCEL FoxPro reports very interesting price - 750 USD...