Question

i uso DOJO + Spring MVC, i call to a ExportPDF.html that generates me my pdf but as a result of my xhr function in my script, y have in the console caracthers like this @"x13&"#%" i think that is my pdf. how can i display in a new web page or permit to the client download it?

this is my exporPDF.html Controller

@RequestMapping(value = "/exportarPDF", method = {
        RequestMethod.GET, RequestMethod.POST })
private @ResponseBody
void exportarPDF(@ModelAttribute("someBean") somebean someBean,
        BindingResult result,HttpServletRequest request,HttpServletResponse response) throws Exception {
    response.setContentType("application/pdf");
    JasperReport mainReporte = (JasperReport) JRLoader.loadObject(mainJasper);
    JasperPrint mainPrint = JasperFillManager.fillReport(mainReporte, parameters, new JRBeanCollectionDataSource(SOMELIST));

 ServletOutputStream output = response.getOutputStream(); 
 JasperExportManager.exportReportToPdfStream(mainPrint, output);
 output.close();

}

My pdf generate greats, but i can see in a page or download it, please help me

and this is my script method

                xhr("exportarPDF.html", {
                    query : {
                        someValue: This Value is received well in the Spring Controller
                    },
                    handleAs : "json",
                    method : "post"
                }).then(function(data) {
                    //I thinks this data is my pdf!
                }, function(err) {
                    alert("Error Interno");
                }, function(evt) {
                    // Handle a progress event from the request if the
                    // browser supports XHR2     
                });

This is my debug with firebug

enter image description here

No correct solution

OTHER TIPS

I have also run into this situation. As answered in XMLHttpRequest to open PDF in browser "It is not possible to do via xhr if the URL you are querying actually returns the PDF data." See the link for the explanation. What I did was saving the generate pdf on the server and put its name in a cookie. When receiving the response, I verified that it is a pdf (by checking if resp.headers('Content-Type')=='application/pdf'). Then I called a different url which rendered the pdf by using window.open. The second argument ('_self') is used so that no new tab eill be open.

 if (isPDF(response))
 {
    var url=$cookies['fileurl'].substring(1,$cookies['fileurl'].length-1);
    $window.open('/ws/print?action='+url,'_self');
    delete $cookies['fileurl'];
    return;
 }

You are trying to download file via AJAX, but what you need is to start regular download.

Assuming your download link is "exportarPDF.html" the following will do the job:

HTML:

  <iframe id="invisible" style="display:none;"></iframe>

Javascript:

      function downloadPDF() {
        var iframe = document.getElementById('invisible');
        iframe.src = "exportarPDF.html";
      }

Explanation:

When you set the "src" of the iframe, the browser reacts as if you would click link. So if the content to which "src" points has content type identified as binary file, the standard download dialog is started, as if you'd just click the download link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top