无法从xhtml(xhtmlrenderer-1.0.0.jar)生成pdf

时间:2017-07-15 00:34:34

标签: eclipse maven jsf primefaces itext

我想使用命令按钮从xhtml页面创建pdf,但是在浏览器中,resault是一个黑色的空白页面,我已经搜索了网页,但没有找到像我的情况。 我正在使用:xhtmlrenderer-1.0.0.jar - Maven projet

PrintBean.java

PUT

test.xhtml

axios.post('https://projectname.firebaseio.com/padres/user_one.json', padre)

要打印的页面,print.xhtml

    package com.ismo.gestionabsence.presentation;

import java.io.File;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Date;

import javax.annotation.PostConstruct;
//import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; 

import org.docx4j.org.xhtmlrenderer.pdf.ITextRenderer;

@ManagedBean(name = "printBean")
@SessionScoped
public class PrintBean implements Serializable  {



    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private int     code;
    private String client;


    public PrintBean()
    {}

        @PostConstruct
        public void init()
        {

            code=1;
            client="teste de client;";
        }








    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }


    public String getClient() {
        return client;
    }

    public void setClient(String client) {
        this.client = client;
    }

    public void createPDF()  {

        System.out.println("in");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpSession session = (HttpSession) externalContext.getSession(true);
        String url = "http://localhost:8080/simple-webapp/faces/print.xhtml;jsessionid="+session.getId()+"?pdf=true";
        System.out.println("url");
        try {

            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(new URL(url).toString());

        //  renderer.setDocument(new File(url));
            renderer.layout();
            HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
            response.reset();
            response.setContentType("application/pdf");
            response.setHeader("content-Disposition", "inline: filename=\"facture.pdf\"");
            OutputStream outputStream = response.getOutputStream();
            renderer.createPDF(outputStream);

        } catch (Exception e) {
            e.printStackTrace();
        }
        facesContext.responseComplete();
    }
    public void test()
    {System.out.println("test");}

}

堆栈错误:

<!DOCTYPE html>
<html  xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui"
            xmlns:b="http://bootsfaces.net/ui"
>
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>



<title>test</title>
</h:head>
<h:body>
<h:form id="form" >
button <h:commandButton value="print" action="#{printBean.createPDF}"></h:commandButton>
</h:form>



</html>

如果有人可以谴责如何解决这个问题,我将不胜感激! 问候,原谅我的英语!!  一张图片来解释更多情况:the pdf is empty

0 个答案:

没有答案