2

I have a WebService that returns a PDF URL. The problem is that the CMS is terrible, flakey, and that document may not even exist. (or there may not be a network connection to the CMS server)

I am using Spring 3.2 MVC and my controller is simple enough:

modelAndView.addObject("documentURL", service.getCmswsGetDocumentUrl());

and the view:

<object data="${documentURL}" width="600" height="800" type='application/pdf' >

This all works fine if the document exists.

However, I want to be able to do, say a javascript alert() or something if the document is not there.

Is there a way to embed the pdf in the modelAndView like

modleAndVIew.addObject("pdf",file);

Or some way that I can add the data to the html object through jQuery?

I just don't want to make 2 calls, one to retrieve the document to see if I can, then one to actually retrieve the document in the html object tag.

Any ideas?

Adendum Edit:

Can I do something like this?: Is it possible to put binary image data into html markup and then get the image displayed as usual in any browser?

where I base64 encode the PDF in the controller and pass that to the view?

Community
  • 1
  • 1
mmaceachran
  • 3,178
  • 7
  • 53
  • 102

1 Answers1

0

A combination of the binary image and an object tag. Here is the controller:

    InputStream is = (new URL(service.getCmswsGetDocumentUrl())).openStream();
    byte[] pdfBytes = IOUtils.toByteArray(is);
    modelAndView.addObject("pdf",new String(Base64.encodeBase64(pdfBytes)));

and the view:

    <object  data="data:application/pdf;base64,${pdf}" width="600" height="800" type='application/pdf' >
    </object>

This works in FF and Chrome, but not IE.
How much do I hate IE? I cannot count the ways even with while(1) { waysIHateIE++; }

Looking at the developer tools in IE, the data="" any Idea why?

mmaceachran
  • 3,178
  • 7
  • 53
  • 102