تمكنت من القيام بذلك من خلال استدعاء روتين Java من المحول للاتصال بالخدمة الخلفية التي تُرجع PDF كـ a byte[]
. مرة واحدة في byte[]
تم استرجاعها أنا base64
قم بتشفيره في روتين Java ، وترميزه URI في المحول ، ثم قم بالعكس في رمز JS العميل. يستخدم رمز Java Apache HttpClient
. حاولت نوع العودة العادي وكذلك الآخرين دون حظ.
رمز المحول:
var service = new ServiceClient();
var pdf = service.getUnsecureContentBase64(url);
result.pdf = encodeURIComponent(pdf);
return result;
رمز العميل:
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
});
}
كود جافا:
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;
}
}