开发者

problem while generating pdf files using iText 5

开发者 https://www.devze.com 2023-04-05 03:34 出处:网络
i\'m trying to generat pdf files using iText, i want those files to be opened directly on my chrome browser so here is my print method\'s code:

i'm trying to generat pdf files using iText, i want those files to be opened directly on my chrome browser so here is my print method's code:

 public void printFicheProjet()
    {
        Integer id=Integer.valueOf((String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id_projet_print"));
        Projets projet=projetsService.getProjets(id);
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            HttpServletResponse hsr = (HttpServletResponse) context.getExternalContext().getResponse();
            hsr.setContentType("application/pdf");
            hsr.setHeader("Content-disposition", "inline; filename=\"enseignants.pdf\"");
            Document pdf = new Document(PageSize.A4, 5, 5, 5, 5);
            pdf.setPageSize(PageSize.A4);
            pdf.addHeader("开发者_运维问答Banque Centrale Populaire", "Fiche Projet");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            PdfWriter.getInstance(pdf, os);
            pdf.open();            
            Font font = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
            Paragraph p = new Paragraph("", font);
            p = new Paragraph("Informations genereaux du projet", font);
            Font gras = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
            p.setIndentationLeft(30);
            pdf.add(p);
            pdf.close();
            byte[] data = os.toByteArray();
            hsr.getOutputStream().write(data);
            context.responseComplete();
        } catch (Exception e) {}
    }

the problem is when i execute this method nothing happens, no error no file on the browser no downloading window...completly nothing. may be something is missing there?


Try replacing

  catch (Exception e) {}

with

    catch (Exception e) {
e.printStackTrace()
}

Which should print the exception being thrown to console.


The problem was that the commandbutton should have ajax="false" attribute and it should work fine. Instead of:

<p:commandButton value="Imprimer Fiche Projet" actionListener="#{projet.printFicheProjet}">
    <f:param name="id_projet_print" value="#{projet.currentChangerStatus.projets.codeProjet}" />
</p:commandButton>

Put:

<p:commandButton value="Imprimer Fiche Projet" actionListener="#{projet.printFicheProjet}" ajax="false">
    <f:param name="id_projet_print" value="#{projet.currentChangerStatus.projets.codeProjet}" />
</p:commandButton>
0

精彩评论

暂无评论...
验证码 换一张
取 消