Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. public String json() throws IOException, TransformerException, SAXException, ParserConfigurationException {
  2. Gson gson = new Gson();
  3.  
  4. String result = gson.toJson(selectedEvidence);
  5. String xml = JSONvXML.readFile(result);
  6.  
  7.  
  8.  
  9. return xml;
  10. }
  11.  
  12. public void download() throws IOException, TransformerException, SAXException, ParserConfigurationException {
  13.  
  14.  
  15. String fileName = "Verzija";
  16. FacesContext fc = FacesContext.getCurrentInstance();
  17. ExternalContext ec = fc.getExternalContext();
  18.  
  19. ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
  20. ec.setResponseContentType("application/pdf");
  21. ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // Shrani z Pop-up window
  22.  
  23. OutputStream output = ec.getResponseOutputStream();
  24.  
  25. String xml = json();
  26. OutputStreamWriter osw = new OutputStreamWriter(output);
  27. PrintWriter writer = new PrintWriter(osw);
  28. writer.write(xml);
  29. writer.flush();
  30. writer.close();
  31.  
  32. XmlVPDF v = new XmlVPDF();
  33.  
  34. v.toPDF(xml, output);
  35.  
  36.  
  37. fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement