One of the biggest frustrations people have with Cognos is that it will use the same window to export a report to PDF. Excel will open a new page, but not PDF. Why? Who knows? (IBMers, please feel free to comment below.)
Fortunately we can use JavaScript to force Cognos to do our bidding.
Since the JavaScript API changed in 10.2 I have two implementations, one for 10.1.1 and lower and one for 10.2 and higher. Simply paste the correct JavaScript into an HTML item, and fix up the button to meet your needs.
10.1.1
<script> var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]); if (!fW || fW == undefined) { fW = (formWarpRequest_THIS_ ? formWarpRequest_THIS_:formWarpRequest_NS_); } var preFix = ""; if (fW.elements["cv.id"]) { preFix = fW.elements["cv.id"].value;} var nameSpace = "oCV" + preFix; CViewerManager.prototype.viewInPDF = function(){ var oReq =new CCognosViewerRequest("render") oReq.addOption("run.outputFormat", "PDF"); this.viewPDFInNewWindow(oReq); } </script> <input type="button" onclick="window[nameSpace].getRV().viewInPDF()" value="Export to PDF"/>
10.2 is a bit easier to work with:
<script> CViewerManager.prototype.runPDF = function () { var oReq = new ViewerDispatcherEntry(this.getCV()); oReq.addFormField("ui.action","render"); oReq.addFormField("run.outputFormat","PDF"); this.viewPDFInNewWindow(oReq); }; </script> <input type="button" onclick="window['oCV'+'_THIS_'].getRV().runPDF()" value="Export to PDF"/>
Remember, use only one of them. The 10.1.1 version won’t work in 10.2, but should work in previous versions. I don’t have access to the 8 versions any more, so I’d appreciate if someone could leave a comment saying if it works or not.
Export to PDF Report XML (856 downloads)EDIT:
Many people have experienced issues with the version posted above on 10.2.1 and above. The following version works perfectly for me on 10.2.1 with IE 8 and Firefox.
Export-to-PDF-10.21.txt (1020 downloads)
Hi Paul,
I am using cognos 10.1
The above script worked for me with minor change.
I have changed the below line
this.viewPDFlnNewWindow(oReq);
with
this.viewlnNewWindow(oReq);
The issue I am facing is that for the new pdf output window the maximize option is disabled. Can you plz help.
Thanks in advance!!!!
Kumar
You mean the new window that’s been opened is missing the minimize, expand, and close buttons? I’ll check that out tomorrow.
Hi Paul,
I tried using this script but nothing happens when i click on the “Export to PDF” button. I am using IE 10. I also tried this Firefox but it does not work there too. Can you please tell me if i need to make any modifications to the script to make it work?
Thanks,
Raj
And mine is Cognos 10.1.1.
Are you getting any JS errors? Press F-12 to open the IE dev toolbar and then Script. With that open press the button, if there are any errors it will appear in the box on the right.
What happens if you switch viewPDFInNewWindow with viewInNewWindow as Kumar?
Hi Paul,
Thanks for the quick response.
I tried changing the “viewPDFInNewWindow” to “viewInNewWindow” too, but didn’t help.
I tried the f12 option, the error i am getting when i click on the button is “‘nameSpace’ is undefined “. Please let me know if i am doing something wrong.
Hi Paul,
Please ignore my previous response. I managed to fix it. Actually it was my bad that i missed to replace one of the “"” in your script. Now it is working with Kumar’s change – “viewInNewWindow”.
Thank you!
Thanks,
Raj
Make sure you copy the entire fW bit. That is used to identify where the report is running (from report studi or cognos connection, for instance). The JS changes based on it.
Sure. Thank you!
Hi Paul,
I am using Cognos 8. I get this JS error:
“הודעה: האובייקט אינו תומך במאפיין או בפעולת שירות אלה. קוד: 0”
Any ideas?
Thanks in advance,
Hagit
Hi Hagit!
Remind me which version you’re using? It may be that the JS in Cognos 8 doesn’t have the functions I’m referencing here. Have you tried using viewInNewWindow instead of viewPDFInNewWindow?
Hi Paul,
I’m using 8.4.1.
I’ve tried using viewInNewWindow. Same error..
An alternative solution, works on all versions, is to make a drill through to the same report, telling it to use format: PDF
Hello Paul,
I tested your script on cognos 10.2.1 and it is working fine.
The new window with the PDF is opened but I’m not able to maximize this window. The maximize button isn’t usable, minimize and close buttons are ok.
Do you know a solution?
Thanks in advance,
Alex
The window opened is indeed not resizable (10.2.1 here). The quickest solution is to go to the system file viewer.standalone.core.js in \webcontent\rv, and find the row _30a=window.open(_30d,_30c,”rv”); and change that to _30a=window.open(_30d,_30c,”rv,resizable=1″);
It’s a brute hack, but it would work.
Wow, that’s annoying. Instead of modifying the core JS files, it would be better to simply recreate the entire function. That way it will still work after Cognos is patched or upgraded.
Hi Paul,
I am need of utilizing this solution, but i am unable to get it to work in IE8 using 10.2.1
The error with just the script alone is – Expected ‘;’
I saw that others were experiencing some issues with this IE and version of cognos when trying to apply your solution.
Any suggestions?
Thanks
Yeah, that’s what I ended up doing. You mashed the two solution (10.1 and 10.2) together there, but the intention is clear.
Hi Paul
Everything works out gr8. But the issue am facing now is windows title is cognos viewer only, inspite of specifying browserHandle.document.title=window.parent.document.title;
Please let me know if you have any solution for this.
Thanks
createHiddenFormField function throwing error in IE 7 & 8. Please let me know whether it is a user defined function or cognos defined one.
Hi Paul,
I’m getting object doesn’t support this property in oReq.getFormField. I’m using cognos 10.1.1 and browser version IE7 & 8
Hi Paul, this code doesn’t work in IE9 or Firefox. I’m using Cognos 10.2.1.
Please let us know if you have any solution.
Thanks,
Hi Paul,
This code works perfectly but not able to maximize the window. can you help me with this please?
Hi Paul,
Do you have a script for opening XML report in new window. This is for 10.2. If you have it, can you please put that here.
Thanks,
Rajmohan
Hi Raj,
Exporting to XML is a simple matter of taking the CViewerManager.prototype.runPDF function and replacing both instances of PDF to XML:
Leave the rest of the script as is.
Hi Paul,
I did the same before posting it here, but it did not work for me. But then one of our off-shore developers had the same code working in his machine. That’s when i figured out that it is a browser/caching issue. Once i cleared that, it started working in my machine too. Thanks for taking time to respond.
Thanks,
Hi Paul,
Thanks for the js … I have your code working perfectly in a report, however when attempting to view the report through a Cognos portal page (in html), it does not render. The hour glass keeps ticking over.
Take out the script, the portal page works perfectly. Add it back in and the hour glass comes back.
Any idea why that would be? (Cognos 10.2 … all browsers)
Cheers,
Ryan.
Fixpacks! This was the very first problem I ran into when I installed 10.2 when it first came out. Make sure your fixpacks are up to date and try again.
Hello, not sure if this thread is still active.
someone had asked if the window title can be changed. just add “ui.name” to oReq
CViewerManager.prototype.runPDF = function ()
{
var oReq = new ViewerDispatcherEntry(this.getCV());
oReq.addFormField(“ui.action”,”render”);
oReq.addFormField(“ui.name”,this.getCV().envParams[“ui.name”]);
oReq.addFormField(“run.outputFormat”,”PDF”);
this.viewInNewWindowWithResize(oReq);
};
If, in IE10, it doesn’t work again (it was my case), i suggest to add a metadata tag in your HTML file, like this :
Hi Paul,
Great article.
Do you have any information around how can we export a Cognos Analytics 11 report in PDF/Excel/CSV format using a javascript ?
I am running a report in an iframe, with top bar with export options disabled.
– I can use a hyperlink to rerun the same report in different formats to allow download from the report page, but that generates output of the report without any prompt filters applied.
– I am looking for something similar to
Any suggestions ?