Portlet application/pdf is not a supported mime type error

622 Views Asked by At

I am facing this error - "application/pdf" is not a supported mime type. I am trying to save the content of p:editor as pdf to my local machine.

Imports

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;

Function

        public StreamedContent getFile() {
                try {
                     ByteArrayOutputStream os = new ByteArrayOutputStream();

                     Document document = new Document(PageSize.LETTER);
                     PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
                     document.open();
                     document.addCreationDate();
                    HTMLWorker htmlWorker = new HTMLWorker(document);
                     String str = "<html><head></head><body>"+ this.getMessage() +"</body></html>";
                     htmlWorker.parse(new StringReader(str));
                     document.close();

                     InputStream is = new ByteArrayInputStream(os.toByteArray());

                     file = new DefaultStreamedContent(is, "application/pdf", "ohyeah.pdf");
                     return file;
                  }
                  catch (Exception e) {
            return null;
                  }
            }

Following an excellent post at : https://forum.primefaces.org/viewtopic.php?f=3&t=21342&p=170835#p170835

Please help !

EDIT------------------

If i use mime type as image/jpg, then also I receive the same error. Issue looks like something else. Can anyone point it to me please .

javax.portlet.faces.BridgeException: java.lang.IllegalArgumentException: image/jpg is not a supported mime type
1

There are 1 best solutions below

1
On

Thanks Melloware for pointing it out that the error is portlet related.

If anyone is facing the same issue, here is the solution which worked for me. Tweak it as per your needs.

public StreamedContent getFile() throws IOException, DocumentException {
        final PortletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        final HttpServletResponse res = PortalUtil.getHttpServletResponse(portletResponse);
        res.setContentType("application/pdf");
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        //res.setHeader("Content-Disposition", "attachment; filename=\".pdf\"");
        res.setHeader("Content-Disposition", "attachment; filename="+subject+".pdf");
        res.setHeader("Refresh", "1");
        res.flushBuffer();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out=res.getOutputStream();
        Document document = new Document(PageSize.LETTER);
        PdfWriter.getInstance(document, baos);
        document.open();
        document.addCreationDate();
        HTMLWorker htmlWorker = new HTMLWorker(document);
        String str =this.getMessage();
        htmlWorker.parse(new StringReader(str));
        document.close();
        baos.writeTo(out);
        out.flush();
        out.close();
        return null;
    }