Can I download file as pdf without any libraries?
I tried somelike
public void download() {
HttpServletResponse res = HttpJSFUtil.getResponse();
res.setContentType("application/pdf");
String filename = "my_document_";
filename += CalendarUtil.formatDateValueMerged(new Date());
filename += ".pdf";
res.setHeader("Content-disposition", "attachment; filename=" + filename);
try {
ServletOutputStream out = res.getOutputStream();
if(subCategoryId != null){
articleDTO = action.getArticleById(subCategoryId);
out.write(getBytes(articleDTO.getContent()));//articleDTO.getContent() is String
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
FacesContext faces = FacesContext.getCurrentInstance();
faces.responseComplete();
}
public static byte[] getBytes(Object obj) throws java.io.IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte[] data = bos.toByteArray();
return data;
}
<h:commandLink id="download" action="#{CategoriesBean.download}" value="Download">
<f:param name="subCategoryId" value="#{CategoriesBean.subCategoryId}"/>
</h:commandLink>
but it does not work