2

I am trying to access to a Rest Service who exposes a pdf file, but I get this response when invoking the procedure:

{
   "errors": [
      "Failed to parse the payload from backend (procedure: HttpRequest)"
   ],
   "info": [
   ],
   "isSuccessful": false,
   "responseHeaders": {
      "Content-Type": "application\/octet-stream; type=\"application\/xop+xml\"; boundary=\"uuid:****************\"; start=\"<pdf>\"; start-info=\"application\/pdf\"",
      "Date": "Thu, 07 Nov 2013 14:44:54 GMT",
      "Server": "Apache-Coyote\/1.1",
      "Transfer-Encoding": "chunked",
      "X-Powered-By": "Servlet 2.5; **********",
      "content-disposition": "attachment; filename = ********.PDF"
   },
   "responseTime": 5329,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 9923,
   "warnings": [
   ]
}

Can I get a pdf file using a worklight adapter? is there any alternative way?

Maria Borbonés
  • 153
  • 1
  • 1
  • 12

2 Answers2

3

I was able to do this with calling a Java routine from the adapter to call the back-end service that returns the PDF as a byte[]. Once the byte[] is retrieved I base64 encode it in the Java routine, URI encode it in the adapter, then do the reverse in the client js code. The Java code uses the Apache HttpClient. I tried the plain return type as well as others with no luck.

Java example here

Adapter code:

var service = new ServiceClient(); 
var pdf = service.getUnsecureContentBase64(url);
result.pdf = encodeURIComponent(pdf);

return result;

Client code:

onSuccess : function (response){

        var pdfText = decodeURIComponent(response.invocationResult.pdf);

        var pdf = base64DecToArr(pdfText);

        //use pdf byte[] to pass to Mozilla pdf.js
        var pdfText = decodeURIComponent(response.invocationResult.pdf);

        var pdf = base64DecToArr(pdfText);

        PDFJS.disableWorker = false;
        PDFJS.getDocument(pdf).then(function (pdfDoc) {
            //use pdfDoc to render
        });
    }

Java code:

public class ServiceClient {

public String getUnsecureContentBase64(String url)
        throws ClientProtocolException, IOException {

    byte[] result = getUnsecureContent(url);

    return Base64.encodeBase64String(result);
}

public byte[] getUnsecureContent(String url)
        throws ClientProtocolException, IOException {

    byte[] result = null;
    CloseableHttpClient httpclient = null;

    try {
        httpclient = HttpClientBuilder.create()
                .setRedirectStrategy(new LaxRedirectStrategy()).build();

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
            public byte[] handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toByteArray(entity);
                } else {
                    return null;
                }
            }
        };

        result = httpclient.execute(httpget, handler);

    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }

    return result;
}
} 
slava
  • 1,901
  • 6
  • 28
  • 32
Sam Nunnally
  • 2,291
  • 2
  • 19
  • 30
  • After received the pdf as byte[] from successHandler, i passed that to PDFJS based on the following example http://mozilla.github.io/pdf.js/examples/ . Do u have any suggestion on how to show PDF file from byte[]. – sasi Jun 13 '14 at 12:51
  • added some more code to show passing to pdf.js in the Client code section – Sam Nunnally Jun 14 '14 at 14:30
  • I followed the same way....i have included But still it is unable to read. – sasi Jun 15 '14 at 06:19
  • If i include only , it is showing undefined in the following line:- PDFJS.getDocument(pdf).then(function (pdfDoc) – sasi Jun 15 '14 at 06:20
1

You need to change the 'returnedContentType' parameter in your adapter implementation. My guess is right now you have it set to 'xml'. Since the pdf that is being retrieved from your backend is not in XML, you are receiving that error message.

Example:

function getPDF() {

    var input = {
        method : 'get',
        returnedContentType : 'plain',
        path : "/test.pdf"
    };

    return WL.Server.invokeHttp(input);
}
jnortey
  • 1,625
  • 2
  • 10
  • 17
  • I can able to retrieve the PDF with returnContentType : 'plain', but it is unable to call successHandler. It is showing the following error:- [/pdf/apps/services/api/pdf/common/query] exception. TypeError: Cannot read property 'isSuccessful' of undefined worklight.js:4556 Uncaught SyntaxError: Unexpected number worklight.js:850 Uncaught TypeError: Cannot read property 'isSuccessful' of undefined – sasi Jun 12 '14 at 09:10
  • You should post a separate question with your problem. It would be hard to debug your problem without seeing some of your code. – jnortey Jun 12 '14 at 21:17
  • I posted a separate question:- http://stackoverflow.com/questions/24227613/get-pdf-files-from-worklight-server-to-the-client – sasi Jun 16 '14 at 04:20