how to download pdf file from external site via actionrequesthow to download pdf file from external site via actionrequesthttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=1214231402024-03-28T17:58:42Z2024-03-28T17:58:42ZRE: RE: how to download pdf file from external site via actionrequestScarletake Bwihttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1214284972022-05-24T02:42:49Z2022-05-24T02:42:48Z<p>hi Mohammed</p>
<p>thank you, thank you very much for reply. you really open my eyes.</p>
<p>i don't know this before and i tried. but unfortunately, it seems
like download from external site to server first, and display to user.</p>
<p>i do not want to save it. the origional code is</p>
<pre>
<code class="language-java"><a href="http://xxx/?doc=<%=docNo%>" target="_blank"></code></pre>
<p>so i make it as</p>
<pre>
<code class="language-java"><a href="http://xxx/doc?=<%=docNo%>" target="_blank"><liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" /></a></code></pre>
<p>it works now.</p>Scarletake Bwi2022-05-24T02:42:48ZRE: how to download pdf file from external site via actionrequestMohammed Yasinhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1214249152022-05-19T08:15:29Z2022-05-19T08:15:28Z<p>Hi, </p>
<p>You can use below Utility for fetching the document url and use it in
anchor tag with target="blank".</p>
<pre>
<code class="language-java">DLURLHelperUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), themeDisplay, StringPool.BLANK)</code></pre>
<p> </p>Mohammed Yasin2022-05-19T08:15:28ZRE: RE: how to download pdf file from external site via actionrequestScarletake Bwihttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1214247562022-05-19T06:52:44Z2022-05-19T06:52:43Z<p>hi Dominik</p>
<p>thank you for reply. </p>
<p>yes, you are right.</p>
<p>but i want the pdf directly be opened on a new window, such like target='_blank". </p>
<p>i do not know how to do it.</p>Scarletake Bwi2022-05-19T06:52:43ZRE: how to download pdf file from external site via actionrequestDominik Markshttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1214238712022-05-17T08:06:31Z2022-05-17T08:06:30Z<p>At first you should have a look at ResourceRequests. Downloading of
files is typically done with ResourceRequests and not with ActionRequests...</p>Dominik Marks2022-05-17T08:06:30Zhow to download pdf file from external site via actionrequestScarletake Bwihttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1214231392022-05-16T04:33:33Z2022-05-16T04:33:32Z<p>hi </p>
<p>i'd like to let user read pdf when they click icon</p>
<pre>
<code class="language-java"><liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" url="http://external/?doc=xxx /></code></pre>
<p>it works.</p>
<p>but i'd like to know, how to do it via actionrequest. i changed my code</p>
<pre>
<code class="language-java"><portlet:actionURL var="efgpExportDocFunctionURL" name="exportDoc">
<portlet:param name="<%=Constant.EFGP_EXPORT_DOC_NO%>" value="<%=docInfoVO.getDocNo()%>" />
</portlet:actionURL>
<liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" url="<%=efgpExportDocFunctionURL.toString() %>" /></code></pre>
<pre>
<code class="language-java"> public void exportDoc(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
StringBuffer messageSB = new StringBuffer();
User currentUser = (User) actionRequest.getAttribute(WebKeys.USER);
String docNo = ParamUtil.getString(actionRequest, Constant.EFGP_EXPORT_DOC_NO);
try {
StringBuffer docLink = new StringBuffer();
docLink.append("http://").append(Constant.EFGP_DOC_SERVER).append("/?docNumber=").append(docNo);
LOGGER.info(docLink.toString());
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(docLink.toString());
HttpResponse responset = httpClient.execute(httpGet);
HttpEntity entity = responset.getEntity();
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
response.setContentType(Constant.HTTP_HEADERS_CONTENT_TYPE_APPLICATION_PDF);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, Constant.HTTP_HEADERS_CONTENT_DISPOSITION_CONTENT_PREFIX_ATTACHMENT_FILENAME + StringPool.QUOTE + docNo + ".PDF" + StringPool.QUOTE);
OutputStream out = response.getOutputStream();
entity.writeTo(out);
out.flush();
out.close();
} catch (Exception ex) {
LOGGER.equals(ex);
messageSB.append("hit exception:").append(ex.getClass().getName()).append(" message:").append(ex.getMessage()).append(Constant.HTML_SNAP);
SessionErrors.add(actionRequest, ex.getClass().getName(), ex);
}
}</code></pre>
<p>but it doesn't work, it seens get the file already, but in borwser,
it shows</p>
<p>
<img alt="" height="191"
src="