Advertisement
Guest User

Untitled

a guest
May 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. <h:form id="detailsDecisionMainForm">
  2. <h:inputHidden id="docId" value="" />
  3. <h:commandButton id="downloadAction" action="#{detailsDecisionGridBean.downloadForm()}" style="display: none;" />
  4. </h:form>
  5.  
  6. public String downloadForm() {
  7. log.fine("downloadForm call");
  8. PdfService pdfService = new PdfServiceImpl();
  9. ArmCommonService armCommonService = new ArmCommonServiceImpl();
  10. String errorMessage = null;
  11. try {
  12. Long opUni = new Long(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("detailsDecisionMainForm:docId"));
  13. log.fine("Document opUni = " + opUni);
  14. DocXML docXML = armCommonService.getDocXMLById(opUni);
  15. if (docXML != null) {
  16. File pdfFile = pdfService.getPdfReport(docXML.getXml(), docXML.getType());
  17. if (pdfFile != null) {
  18. DownloadUtils.exportPdf(pdfFile);
  19. } else {
  20. log.log(Level.SEVERE, "downloadForm error: pdf generation error");
  21. errorMessage = "PDF-document generation error.";
  22. }
  23. } else {
  24. log.log(Level.SEVERE, "downloadForm error: xml not found");
  25. errorMessage = "XML-document not found.";
  26. }
  27. } catch (Exception ex) {
  28. log.log(Level.SEVERE, "downloadForm exception: " + ex.getMessage());
  29. errorMessage = "File download exception.";
  30. }
  31. if (errorMessage != null) {
  32. FacesContext.getCurrentInstance().addMessage("detailsDecisionMainForm:downloadAction", new FacesMessage(errorMessage));
  33. }
  34. return null;
  35. }
  36.  
  37. public static void exportPdf(File file) throws IOException {
  38. InputStream fileIS = null;
  39. try {
  40. log.fine("exportPdf call");
  41. fileIS = new FileInputStream(file);
  42. FacesContext fc = FacesContext.getCurrentInstance();
  43. ExternalContext ec = fc.getExternalContext();
  44. ec.responseReset();
  45. ec.setResponseContentType(APPLICATION_PDF_UTF_8);
  46. byte[] buffer = ByteStreams.toByteArray(fileIS);
  47. ec.setResponseContentLength(buffer.length);
  48. ec.setResponseHeader(HttpHeaders.CONTENT_DISPOSITION, String.format(CONTENT_DISPOSITION_VALUE, new String(file.getName().getBytes(StandardCharsets.UTF_8))));
  49. ec.getResponseOutputStream().write(buffer);
  50. fc.responseComplete();
  51. } catch (Exception ex) {
  52. log.log(Level.SEVERE, "exportPdf exception: " + ex.getMessage());
  53. } finally {
  54. if (fileIS != null) {
  55. fileIS.close();
  56. log.fine("exportPdf inputstream file closed");
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement