SKU-1069 #comment merged in loads of new query stuff from paul to fix PDF export