0

I want to show pdf in new tab after generated it, im using JQuery 1.12.3 and Spring MVC 4.

When i clic to my link, i generate pdf and my ajax method show success but my pdf isnt showed in new tab, what i've forgot ?

i've follewed this posts :

Display PDF in a webpage

Open ResponseEntity PDF in new browser tab

Spring - display PDF-file in browser instead of downloading

I've tried to remove ResponseBody annotation, but i've the same result

Here is my code :

HTML :

<a id="3676274" class="bulletinLink" target="_blank" href="#">Bulletin du 2015-04-30</a>

JQuery :

$(".bulletinLink").click(function(e){
    e.preventDefault();
    var id = $(this).attr('id');

    var oAjax;
    var sUrl = sUrlBase + '/generatePdf/'+id;

    oAjax = $.ajax({
            url: sUrl,
            type: 'GET',
            cache: false,
            data : '',
            async : false
    });

    oAjax.done(function(transport) {
        alert("success");
    });
    oAjax.fail(function(transport) {
        alert("fail");
    });
});

Java :

@RequestMapping(value = "/generatePdf/{id}", method = RequestMethod.GET)
@ResponseBody
public final ResponseEntity<byte[]> generateWithResponseBody(@PathVariable("id") final int idBulletin
            ,final HttpServletRequest httpRequete, final HttpServletResponse httpReponse) throws ApplicationException
{
...
HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.parseMediaType( "application/pdf" ) );
String filename = "spring_tutorial.pdf";
headers.setContentDispositionFormData( filename, filename );
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.OK);
return response;
}
Community
  • 1
  • 1
Pasja95
  • 93
  • 1
  • 15

2 Answers2

0

The problem is you need to add the file to the HttpServletResponse, and no need to return any value in your method, so use void, adding the file to the response, the browser will handle it correctly

@RequestMapping(value = "/generatePdf/{id}", method = RequestMethod.GET)
@ResponseBody
public final void generateWithResponseBody(@PathVariable("id") final int idBulletin
            ,final HttpServletRequest httpRequete, final HttpServletResponse reponse) throws ApplicationException
{
    //Here retrieve your PDF file
    if(file != null) {
        String mimeType = URLConnection.guessContentTypeFromName(file.getName());
        if (mimeType == null) {
            logger.debug("mimetype is not detectable, will take default");
            mimeType = "application/pdf";
        }
        logger.debug("mimetype : {}", mimeType);
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
        response.setContentLength((int) file.length());
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    }
}
cralfaro
  • 5,822
  • 3
  • 20
  • 30
0

I've change my jquery function without Ajax like this and it works know :

window.open(sUrl);

Also, for displaying my pdf in the tab and dont download it, i remove this instruction in controller :

headers.setContentDispositionFormData( filename, filename );
Pasja95
  • 93
  • 1
  • 15