Wiki Home

Using Amyuni PDFConverter With VFP


Namespace: WIN_COM_API
This code illustrates one way to use the Amyuni PDF Converter with VFP. Adapted from the documentation provided with the product. Tested under VFP6 SP3 using the Amyuni demo product which can be downloaded from www.amyuni.com. -- Rick Borup, 24-Aug-2000

Here is information about the AMYUNI PDF Converter.

Added check for path on tcFileName before prepending local path to filename -- Timothy Yeaney 20-Oct-2000

Updated for use with Amyuni version 2.50 (new code is in green).
*-----------------------------
* 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 CDINTF250.DLL if you're using Amyuni 2.50 
*
#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.50...
#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.50
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.50 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

In this line ? "Error message: " + LASTERRORMSG() where does LASTERRORMSG() come from? Should it be oPDFPrinter.LASTERRORMSG or something? - ?CFK

It turns out that LASTERRORMSG() is only for use with the FLL which is supplied by Amyuni for use with VFP. In this program I'm using the DLL rather than the FLL, so this needs to be a call to GETLASTERRORMSG() which is the corresponding function in the DLL. The corrected code is in blue in Procedure ErrHand above. - Rick Borup

Here's a tip which may save you some gray hair - I was mystified when the report I wanted to print kept going to the network printer instead of to a PDF file, until I remembered something I heard on the UT. You need to open the report as a table, and blank out the EXPR, TAG and TAG2 fields of the first record (the default printer info is stored here). This is not relevant only to the AMYUNI driver, but it's where I first noticed this happening. BTW - amyuni have released a new version of their driver. You can now do watermarks, and apparently hyperlinks as well. - Neil Ryder

And to reduce gray hair from doing that :-), you may not want to just "blank out" the EXPR field. If a report has been set up to print in landscape orientation or duplex or a certain number of copies, etc., you probably will want to leave those directives in EXPR and only remove the printer-specific information. Kelly Conway
FYI… I was recently given the task of figuring out why the SetDefaultFileName and SetFileNameOptions didn’t appear to be working when printing an Excel spreadsheet using the Amyuni PDF Compatible Printer Driver. After turning several hairs gray, it appears that Excel’s Printout method overwrites or overrides the Amyuni SetFileNameOptions.

I had to also use Excel’s 8th parameter for the printout method to get it to work.

*** 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.)

-- Mike Coop 07-Apr-2005
Contributors: Rick Borup
Category PDF Category Code Samples Category Third Party Products
( Topic last updated: 2006.12.01 02:47:22 PM )