Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <h:form id="detailsDecisionMainForm">
- <h:inputHidden id="docId" value="" />
- <h:commandButton id="downloadAction" action="#{detailsDecisionGridBean.downloadForm()}" style="display: none;" />
- </h:form>
- public String downloadForm() {
- log.fine("downloadForm call");
- PdfService pdfService = new PdfServiceImpl();
- ArmCommonService armCommonService = new ArmCommonServiceImpl();
- String errorMessage = null;
- try {
- Long opUni = new Long(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("detailsDecisionMainForm:docId"));
- log.fine("Document opUni = " + opUni);
- DocXML docXML = armCommonService.getDocXMLById(opUni);
- if (docXML != null) {
- File pdfFile = pdfService.getPdfReport(docXML.getXml(), docXML.getType());
- if (pdfFile != null) {
- DownloadUtils.exportPdf(pdfFile);
- } else {
- log.log(Level.SEVERE, "downloadForm error: pdf generation error");
- errorMessage = "PDF-document generation error.";
- }
- } else {
- log.log(Level.SEVERE, "downloadForm error: xml not found");
- errorMessage = "XML-document not found.";
- }
- } catch (Exception ex) {
- log.log(Level.SEVERE, "downloadForm exception: " + ex.getMessage());
- errorMessage = "File download exception.";
- }
- if (errorMessage != null) {
- FacesContext.getCurrentInstance().addMessage("detailsDecisionMainForm:downloadAction", new FacesMessage(errorMessage));
- }
- return null;
- }
- public static void exportPdf(File file) throws IOException {
- InputStream fileIS = null;
- try {
- log.fine("exportPdf call");
- fileIS = new FileInputStream(file);
- FacesContext fc = FacesContext.getCurrentInstance();
- ExternalContext ec = fc.getExternalContext();
- ec.responseReset();
- ec.setResponseContentType(APPLICATION_PDF_UTF_8);
- byte[] buffer = ByteStreams.toByteArray(fileIS);
- ec.setResponseContentLength(buffer.length);
- ec.setResponseHeader(HttpHeaders.CONTENT_DISPOSITION, String.format(CONTENT_DISPOSITION_VALUE, new String(file.getName().getBytes(StandardCharsets.UTF_8))));
- ec.getResponseOutputStream().write(buffer);
- fc.responseComplete();
- } catch (Exception ex) {
- log.log(Level.SEVERE, "exportPdf exception: " + ex.getMessage());
- } finally {
- if (fileIS != null) {
- fileIS.close();
- log.fine("exportPdf inputstream file closed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement