Forum How do I...?

Prince HTTP requests and cookies, headers or certificates

bh213
I would like to convince prince to perform http requests for images and other resource using provided certificate, cookie or header.

Ideally headers and cookie and certificates would be provided on per-resource basis, but even per-document would be sufficient.

Is it possible to do that somehow?

Thanks.
mikeday
There is an undocumented option --ssl-ca-cert for specifying the path to a local certificate bundle to replace the default used by libcurl. However, there is currently no method to specify cookies or custom HTTP headers.
rolandw
I am trying to use Prince to output from a wiki (http://jspwiki.org) and this has an ACL based on cookies. Currently I can easily output the XHTML required for Prince but any images stored as attachments are not included by Prince because Prince can't get to them through lack of cookie support. A work around to this problem would be much appreciated because the only alternative to to curl the images to a local location and modify the XHTML accordingly.
silviobierman
Hello roland,

You could embed the session id inside the URLs inside the XHTML documents if your webserver supports this. Usually this is done by appending something like

;jsessionid=xxxxxxxxxxxx

to the URI part of the URL. Some older HTTP servers use the parameter syntax for the same purpose. Look for 'URL session encoding' in the available documentation.

If you check the thread I recently started on the same subject you can read that the next release of Prince will support session cookies.

Kind regards,

Silvio Bierman
mikeday
Today we have released Prince 6.0 rev 5, which remembers cookies for subsequent HTTP requests.
alex.collectionhq
Hi Mikeday.
I came across this thread since I'm desperately trying to attach cookies to my current page url.
I followed Silvio B. advice above but princeXml converts document without any changes applied.
Is there a way to use cookies as I can't seems to find any help on documentation?

here is some code:
// get cookies from front-end and call Prince
// this called in a Java Servlet
		Cookie[] cookies = request.getCookies();
		
		String name = null, value = null;
		for (int i = 0; i < cookies.length; i++){
			if ( cookies[i].getName().equalsIgnoreCase("JSESSIONID") ){
				name = cookies[i].getName();
				value = cookies[i].getValue();
				break;
			}
		}
		exportToPrince( name+'='+value );
      }
	public void exportToPrince(String cookie) {
		String urlStr = "http://localhost:8080/.../faces/reportPage.xhtml";
		
		if (cookie != null) 
			urlStr +=  ";" + cookie;
		
		Prince p = new Prince("C:\\...\\Prince\\engine\\bin\\prince.exe");
		InputStream in;
		OutputStream out;
		boolean b = false;
		try {
			out = new FileOutputStream( "C:\\...\\...\\Downloads\\report.pdf" );
			URL url = new URL( urlStr );
			in = url.openStream();

			p.addStyleSheet("C:\\...\\...\\...\\resources\\css\\some1.css");
			p.addStyleSheet("C:\\...\\...\\...\\resources\\css\\some2.css");
			p.addStyleSheet("C:\\...\\...\\...\\resources\\css\\some3.css");
			p.setHTML(true);
			b = p.convert( in , out );
			out.flush();
			out.close();
                 ...
         }
mikeday
Currently the only way to use cookies is by creating an external cookie jar file, and referring to it with the --cookiejar command-line option. The JSESSIONID parameter seems like a clever approach, any idea why it isn't working? Does it work if you do it from a clean browser, with no saved cookies?
alex.collectionhq
when i delete all cookies It doesn't work either
mikeday
Okay, so it looks like the server does not accept the JSESSIONID url parameter as a replacement for the cookie. So it will be necessary to use the cookie jar file.