0

Hello I have a Java web app made in JSF...when you press a button it should write a string in a pdf file and download it. Here is the code:

String cont = "DATA CREARE: " + data_creare3 + "\n" + "CIF: " + cif3 + "\n" + "ID SOLICITARE: " + id_solicitare3 + "\n" + "DETALII" + detalii3 + "\n" + "TIP: " + tip3 + "\n" + "ID: " + id3;

startDownload(cont.getBytes(), "id.pdf");

This is the String and the method being called and here is the function that makes the download..

private void startDownload(byte[] continut, String denumire) throws IOException{
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // externalContext.responseReset();
    externalContext.setResponseHeader("Cache-Control", "public");
    externalContext.setResponseHeader("Pragma", "public");
    externalContext.setResponseHeader("Content-Type", "application/pdf");
    externalContext.setResponseHeader("Content-Length",
    Integer.toString(continut.length));
    externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + denumire + "\"");
    externalContext.getResponseOutputStream().write(continut);
    facesContext.responseComplete();
}

When I click the button to download it says:

emptyResponse: An empty response was received from the server.  Check server error logs

The server error logs say:

2020-02-03 09:18:44,438 WARNING [javax.enterprise.resource.webcontainer.jsf.application] (default task-2) JSF1063: WARNING! Setting non-serializable attribute value into HttpSession (key: logic, value class: com.serban.Logic).

What should I do to make it work ? Thanks in advance

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
SECI
  • 103
  • 2
  • 10
  • I tried the suggested anser and i get the same response... – SECI Feb 03 '20 at 07:58
  • Why do you think this simple String is a valid PDF document? – Selaron Feb 03 '20 at 11:57
  • I`m converting it in bytes and giving it as a parameter to the function – SECI Feb 03 '20 at 12:07
  • You are sending that string to the web browser claiming it is a PDF which it is not. It does not match the [PDF file structure](https://en.wikipedia.org/wiki/PDF#File_structure) – Selaron Feb 03 '20 at 12:12

0 Answers0