Forum News

Prince PHP interface released

Cong
Today we have released an initial version of a PHP interface for Prince:

Prince-php.zip

This has a php class for calling Prince from php.
Ben
Any chance on getting a coldfusion example up? I wish I could use php as I am more familiar with it, but on the servers over here they limit us to coldfusion (bah!). Anyway, I'm having a hard time running an xml through the command-line interface using coldfusion, any help?
I want to do something similar to the menu example on the examples page of http://www.princexml.com, using coldfusion.
My web knowledge isn't the greatest, but I here's what I am trying to do:
I want the user to be able to generate an xml from a database, which is then sent to Prince and output as pdf. I don't know if it can be done with a single mouse click on some web form, but that is my goal. I have generated the xml, although I'm not sure if I have to save a copy to the user's machine in order for Prince to be able to accept it?? Anyway, any help would be appreciated.
Thanks
Ben
mikeday
Hi Ben,

We will take a look at Coldfusion and see what is the easiest way to call Prince from that environment.

With regards to your web form, if you are generating the XML from the database on the server, there should be no need to send the XML back to the client, you can simply pass the XML to Prince on the server and send the resulting PDF file back to the client.

Cheers,

Michael
Ben
Thanks Michael,
That would be of great benefit to those of us stuck with ColdFusion. I can't get it to work!
Ben
Cong
Hi Ben,

The easiest way to call Prince from ColdFusion is using the Prince COM interface. The Prince COM interface is an ActiveX DLL file that can be downloaded from http://yeslogic.com/prince/download/Prince-ActiveX-DLL-5.0.zip.

The DLL file needs to be registered in the Windows registry using REGSVR32.EXE.

In order to call Prince from ColdFusion, we need to create a COM object using the CreateObject function that is available in CFML scripting (You can use the <cfobject ...> tag if you prefer). Once the com object is created, you can use the COM interface methods to perform the tasks.

The following is some sample code for illustration:

<cfscript>
	pr = CreateObject("Com", "PrinceCom.Prince", "local");
	
	pr.SetPrincePath("C:\Prince\engine\bin\prince.exe");
	pr.AddStyleSheet("C:\Prince\test\test1.css");
	pr.AddStyleSheet("C:\Prince\test\test2.css");
	pr.SetEncryptInfo(128, "secretPassword1", "secretPassword2", True,  
                                   True, True, True);
	
	if (pr.Convert("C:\Prince\examples\webarch.html",
			"C:\Prince\output\output.pdf") eq 1)
		WriteOutput("Successful");
	else
		WriteOutput("Unsuccessful");
		
	ReleaseComObject(pr);
</cfscript>

Please read the README.TXT file that comes with the DLL file for more details of the COM interface methods. And don't forget to release the COM object once you are done with it.
Ben
Thanks for the solution, Conq. Sorry I am not more knowledgeable in these things... I need to take some more computer classes to become more literate.
The problem I'm still facing is that all the Coldfusion servers here run off of Unix machines, so (based my knowledge, which is probably inaccurate) I cannot use COM objects (as well as ActiveX, etc.). Is there an example of doing this same procedure for a Unix machine? Or am I just confusing myself?
Ben
Cong
Hi Ben,

I will do more investigation and see if it's possible to put together a ColdFusion solution for Unix.
Cong
Hi Ben,

I understand that your ColdFusion runs on Unix therefore using COM is not the right solution. Perhaps you can try to use our Java interface to do the job. We have just released a repackaged Java interface for Prince which can be downloaded from:

http://yeslogic.com/prince/download/prince-java-2005-09-05.zip

ColdFusion has very good support for Java so using the Java interface to call Prince seems to be the right choice for you.

Unzip the file you downloaded and you should get two files: Prince.jar and README.txt.

The README.txt file lists the Java class methods and explains how to use them.

The Prince.jar file is a directory structure that leads to the Java interface file (com\princexml\Prince.class).

Place the Prince.jar file in a directory of your choice then start ColdFusion Administrator.

Under 'Server Settings > Java and JVM', you should find 'ColdFusion Class Path'. Here you can tell ColdFusion where to look for Java classes. So type in the full path of the Prince.jar file. It should be something like:
PATH\Prince.jar, where PATH is the path of the directory that contains the Prince.jar.

If all is done correctly, ColdFusion should know where to find the Java interface Prince.class. The following is some sample CFML code:

<cfscript>
	pr = CreateObject("java", "com.princexml.Prince");
	
	pr.init("C:\Prince\engine\bin\prince.exe");
	pr.addStyleSheet("c:\Prince\test\test1.css");
	pr.addStyleSheet("c:\Prince\test\test2.css");
	pr.setEncryptInfo(128, "secretPassword1", "secretPassword2", True, True, True, True);       
	
	if (pr.Convert("C:\Prince\examples\webarch.html",
			"C:\Prince\output\output.pdf"))
		WriteOutput("Successful");
	else
		WriteOutput("Unsuccessful");
</cfscript>


You should substitute the paths with the appropriate Unix style paths.
I hope this can be of some help to you. And I look forward to hearing your feedback and comments.
Ben
Hi Conq,
Okay I'm getting about halfway here. I simplified the issue (got rid of style sheets, etc.) as much as possible so the problem could be more easily seen... This is what I have:

<cfset princeEXE = #ExpandPath('Prince/engine/bin/prince.exe')#>
<cfset pdfOutput = #ExpandPath('Output.pdf')#>
<cfset xmlOutput = #ExpandPath('Output.xml')#>
<cffile action="write" file="#xmlOutput#" nameconflict="makeunique" output="#courseXml#">
<cfscript>
   pr = CreateObject("java", "com.princexml.Prince"); 

   pr.init(princeEXE);
    
   if (pr.convert(xmlOutput, pdfOutput))
      WriteOutput("Successful"); 
   else 
      WriteOutput("Unsuccessful"); 
</cfscript>


The .cfm file with this code is contained in a directory that also contains the Prince folder (so the pathnames are correct).
Now, my first concern was that the XML file wasn't getting written before the cfscript prince code started going, but I ran it through after writing the xml to file and checking it was there, and it still wasn't working (output: unsuccessful).
Now, I know the xml is valid, I can run the output xml through prince in the GUI environment with the css file and it comes through fine. Is there some way I can debug this? I know the class is loading correctly also, as I have done a <cfdump> and it shows that the pr class is loaded....
Thanks for any help you may be able to provide.
Ben
Cong
Hi Ben,

Are you running ColdFusion on Linux or Windows? If you are running on Linux, then this path is not correct: Prince/engine/bin/prince.exe is the Windows path and will not work on Linux.

If you installed Prince on Linux in a directory such as /usr/local then the correct path would be: /usr/local/bin/prince (note that there is no .exe suffix). Prince needs to be installed on the server machine where ColdFusion and the Java class are installed.

Besides that we can't quite pinpoint the problem that you described, but there are some things that you could check:

* Check that you are giving the path to the correct version of Prince, preferably the recently released Prince 5.0 and not an earlier beta version. It would also help to print the full path after calling ExpandPath() to verify that it is really referring to the correct file.

* Check that the ColdFusion user has write permission for the PDF output directory. If in doubt, try writing the PDF to /tmp or some other directory that is known to be world-writable.

* Check that you are not creating the XML by calling ToString(), as this does not appear to work very well and can leave invalid UTF-8 characters in the XML.

In the future we will release an updated Java interface to Prince which returns more explicit error messages for these conditions.