Example usage for com.itextpdf.text Document open

List of usage examples for com.itextpdf.text Document open

Introduction

In this page you can find the example usage for com.itextpdf.text Document open.

Prototype

boolean open

To view the source code for com.itextpdf.text Document open.

Click Source Link

Document

Is the document open or not?

Usage

From source file:com.algoboss.erp.util.report.PDFExporter2.java

License:Apache License

public void export(FacesContext context, DataTable table, String filename, String encodingType)
        throws IOException {
    try {//  w ww.j  a v  a 2 s . c om
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);

        if (!document.isOpen()) {
            document.open();
        }
        document.setMargins(1f, 1f, 1f, 1f);
        document.setPageSize(PageSize.A4);
        document.add(exportPDFTable(context, table, false, false, true, encodingType));
        document.setMargins(1f, 1f, 1f, 1f);
        document.setPageSize(PageSize.A4);

        document.close();

        //writePDFToResponse(context.getExternalContext(), baos, filename);
        writePDFToResponseNew(context.getExternalContext(), baos, filename);

    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.algoboss.erp.util.report.PDFExporter2.java

License:Apache License

public void export(FacesContext context, DataTable table, String filename) throws IOException {
    try {/*from www . j a  v a2s.  c  o m*/
        Document document = new Document(PageSize.A4);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream(1000000);
        FileOutputStream file = new FileOutputStream("D://Documents//testeLink.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(document, file);
        document.open();
        document.addAuthor("Algo Boss");
        document.addCreator("Real's HowTo");
        document.addSubject("Thanks for your support");
        document.addCreationDate();
        document.addTitle("Please read this");
        //XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        DataTableRenderer render = new DataTableRenderer();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();
        ExternalContext externalContext = context.getExternalContext();
        //BufferedWriter writer = new BufferedWriter(baos2,true);
        OutputStreamWriter writer = new OutputStreamWriter(baos2, "iso-8859-1");

        context.setResponseWriter(context.getRenderKit().createResponseWriter(writer, null, "iso-8859-1"));
        render.encodeEnd(context, table);
        //System.out.println(new String(baos2.toByteArray(), "utf-8"));
        /*writer.write(
         "<link href=\"/ERP/f/javax.faces.resource/theme.css?ln=primefaces-cupertino\" rel=\"stylesheet\" type=\"text/css\">\n"
         + "<link href=\"/ERP/f/javax.faces.resource/primefaces.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\">\n"
         + "<link href=\"/ERP/f/javax.faces.resource/schedule/schedule.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\">\n"
         + "<meta content=\"application/pdf; charset=UTF-8\" http-equiv=\"Content-Type\">\n"
         + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/default.css\">\n"
         + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/cssLayout.css\">\n"
         + "<title>Plataforma de Gerenciamento</title>");*/
        /*
         externalContext.setResponseHeader("Expires", "0");
         externalContext.setResponseHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
         externalContext.setResponseHeader("Pragma", "public");
         externalContext.setResponseHeader("Content-disposition", "inline;filename=" + filename + ".pdf");
         externalContext.setResponseContentType("application/pdf");
         externalContext.setResponseContentLength(baos.size());
         externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>());
         OutputStream out = externalContext.getResponseOutputStream();
         baos.writeTo(out);
         externalContext.responseFlushBuffer();     
         */

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;filename=\"" + filename + "\";");

        //String str = "<html><head></head><body>" + new String(baos2.toByteArray()) + "</body></html>";
        String str = "<html><head></head><body>"
                + "<link href=\"/ERP/f/javax.faces.resource/theme.css?ln=primefaces-cupertino\" rel=\"stylesheet\" type=\"text/css\"/>\n"
                + "<link href=\"/ERP/f/javax.faces.resource/primefaces.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\"/>\n"
                + "<link href=\"/ERP/f/javax.faces.resource/schedule/schedule.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\"/>\n"
                + "<meta content=\"application/pdf; charset=UTF-8\" http-equiv=\"Content-Type\"/>\n"
                + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/default.css\"/>\n"
                + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/cssLayout.css\"/>\n"
                + "<title>Plataforma de Gerenciamento</title>"
                + "<div id='tabView:ad_j_id77' class='ui-datatable ui-widget ui-algo-element ui-algo-element-container data-list'><div class='ui-datatable-header ui-widget-header ui-corner-top'><div id='tabView:ad_j_id77:ad_j_id41' class='ui-panel ui-widget ui-widget-content ui-corner-all ui-algo-element ui-algo-element-container'><div id='tabView:ad_j_id77:ad_j_id41_content' class='ui-panel-content ui-widget-content'><label id='tabView:ad_j_id77:ad_j_id39' class='ui-outputlabel ui-algo-element ui-algo-element-value'>Listagem: FUNCION?RIO</label><label id='tabView:ad_j_id77:ad_j_id40' class='ui-outputlabel ui-algo-element ui-algo-element-value' style='float:right;top:-5px;position: relative;'>Pesquisar: <input id='tabView:ad_j_id77:globalFilter' name='tabView:ad_j_id77:globalFilter' type='text' value='' onkeyup='funcionarioDataTable.filter()' class='ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all ui-algo-element  ui-algo-element-value' /><script id='tabView:ad_j_id77:globalFilter_s' type='text/javascript'>PrimeFaces.cw('InputText','widget_tabView_ad_j_id77_globalFilter',{id:'tabView:ad_j_id77:globalFilter'});</script></label></div></div><script id='tabView:ad_j_id77:ad_j_id41_s' type='text/javascript'>PrimeFaces.cw('Panel','widget_tabView_ad_j_id77_ad_j_id41',{id:'tabView:ad_j_id77:ad_j_id41'});</script></div><div id='tabView:ad_j_id77_paginator_top' class='ui-paginator ui-paginator-top ui-widget-header'><span class='ui-paginator-first ui-state-default ui-corner-all ui-state-disabled'><span class='ui-icon ui-icon-seek-first'>p</span></span><span class='ui-paginator-prev ui-state-default ui-corner-all ui-state-disabled'><span class='ui-icon ui-icon-seek-prev'>p</span></span><span class='ui-paginator-pages'><span class='ui-paginator-page ui-state-default ui-state-active ui-corner-all'>1</span><span class='ui-paginator-page ui-state-default ui-corner-all'>2</span></span><span class='ui-paginator-next ui-state-default ui-corner-all'><span class='ui-icon ui-icon-seek-next'>p</span></span><span class='ui-paginator-last ui-state-default ui-corner-all'><span class='ui-icon ui-icon-seek-end'>p</span></span></div><div class='ui-datatable-tablewrapper'><table role='grid'><thead id='tabView:ad_j_id77_head'><tr role='row'><th id='tabView:ad_j_id77:ad_j_id44' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span>Nome</span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id44:filter' name='tabView:ad_j_id77:ad_j_id44:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id48' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id45'>Usurio</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id48:filter' name='tabView:ad_j_id77:ad_j_id48:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id52' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id49'>Admisso</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id52:filter' name='tabView:ad_j_id77:ad_j_id52:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id56' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader' style='text-align:center;'><span><span id='tabView:ad_j_id77:ad_j_id53'>Afastado</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id56:filter' name='tabView:ad_j_id77:ad_j_id56:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id60' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id57'>Veculo</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id60:filter' name='tabView:ad_j_id77:ad_j_id60:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id64' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id61'>MATR?CULA</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id64:filter' name='tabView:ad_j_id77:ad_j_id64:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id68' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id65'>CPF</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id68:filter' name='tabView:ad_j_id77:ad_j_id68:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id72' class='ui-state-default' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id69'>SAL?RIO</span></span></th><th id='tabView:ad_j_id77:ad_j_id76' class='ui-state-default' role='columnheader' style='text-align:center;'><span>Ao</span></th></tr></thead><tfoot></tfoot><tbody id='tabView:ad_j_id77_data' class='ui-datatable-data ui-widget-content'><tr data-ri='0' data-rk='315' class='ui-widget-content ui-datatable-even' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id42'>MARCUS2</span><a id='tabView:ad_j_id77:0:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id46'>SDASA2</span><a id='tabView:ad_j_id77:0:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id50'>2013/08/15</span><a id='tabView:ad_j_id77:0:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:0:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:0:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id58'>JQV-4404 - GM</span><a id='tabView:ad_j_id77:0:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id62'>123124</span><a id='tabView:ad_j_id77:0:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id66'></span><a id='tabView:ad_j_id77:0:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id70'></span><a id='tabView:ad_j_id77:0:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:0:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:0:ad_j_id74'> | </span><a id='tabView:ad_j_id77:0:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='1' data-rk='318' class='ui-widget-content ui-datatable-odd' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id42'>PAULO</span><a id='tabView:ad_j_id77:1:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id46'>PAULO</span><a id='tabView:ad_j_id77:1:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id50'>2013/08/07</span><a id='tabView:ad_j_id77:1:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:1:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:1:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id58'></span><a id='tabView:ad_j_id77:1:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id62'></span><a id='tabView:ad_j_id77:1:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id66'></span><a id='tabView:ad_j_id77:1:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id70'></span><a id='tabView:ad_j_id77:1:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:1:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:1:ad_j_id74'> | </span><a id='tabView:ad_j_id77:1:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='2' data-rk='319' class='ui-widget-content ui-datatable-even' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id42'>MAIZA</span><a id='tabView:ad_j_id77:2:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id46'>MAIZA</span><a id='tabView:ad_j_id77:2:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id50'></span><a id='tabView:ad_j_id77:2:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:2:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:2:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id58'>BMW - BMW-9999</span><a id='tabView:ad_j_id77:2:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id62'>TESTE123123</span><a id='tabView:ad_j_id77:2:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id66'>123.123.123-12</span><a id='tabView:ad_j_id77:2:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id70'>12.345,67</span><a id='tabView:ad_j_id77:2:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:2:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:2:ad_j_id74'> | </span><a id='tabView:ad_j_id77:2:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='3' data-rk='320' class='ui-widget-content ui-datatable-odd' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id42'>ELAINE</span><a id='tabView:ad_j_id77:3:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id46'>ELAINE</span><a id='tabView:ad_j_id77:3:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id50'>2013/08/22</span><a id='tabView:ad_j_id77:3:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:3:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:3:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id58'>JQV-4404 - GM</span><a id='tabView:ad_j_id77:3:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id62'></span><a id='tabView:ad_j_id77:3:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id66'></span><a id='tabView:ad_j_id77:3:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id70'></span><a id='tabView:ad_j_id77:3:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:3:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:3:ad_j_id74'> | </span><a id='tabView:ad_j_id77:3:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='4' data-rk='321' class='ui-widget-content ui-datatable-even' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id42'>CLEIDE2</span><a id='tabView:ad_j_id77:4:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id46'>CLEIDE DA SILVA</span><a id='tabView:ad_j_id77:4:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id50'>2013/08/19</span><a id='tabView:ad_j_id77:4:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:4:ad_j_id54' class=' ui-icon ui-icon-check ' style='display:inline-block;'>true</span><a id='tabView:ad_j_id77:4:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id58'>EEQEWEQE - QWEQ</span><a id='tabView:ad_j_id77:4:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id62'></span><a id='tabView:ad_j_id77:4:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id66'>123.123.123-12</span><a id='tabView:ad_j_id77:4:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id70'>456,70</span><a id='tabView:ad_j_id77:4:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:4:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:4:ad_j_id74'> | </span><a id='tabView:ad_j_id77:4:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='5' data-rk='322' class='ui-widget-content ui-datatable-odd' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id42'>GABRIELA</span><a id='tabView:ad_j_id77:5:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id46'>GABRIELA</span><a id='tabView:ad_j_id77:5:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id50'>2013/08/20</span><a id='tabView:ad_j_id77:5:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:5:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:5:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id58'></span><a id='tabView:ad_j_id77:5:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id62'></span><a id='tabView:ad_j_id77:5:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id66'></span><a id='tabView:ad_j_id77:5:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id70'></span><a id='tabView:ad_j_id77:5:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:5:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:5:ad_j_id74'> | </span><a id='tabView:ad_j_id77:5:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr>"
                + "</tbody></table></div></div>" + "</body></html>";

        //worker.parseXHtml(pdfWriter, document, new StringReader(str));

        response.setContentLength(baos.size());
        response.getOutputStream().write(baos.toByteArray(), 0, baos.size());
        //System.out.println(str);
        document.close();
        pdfWriter.close();
        baos.close();
        baos2.close();
        file.close();
        FacesContext.getCurrentInstance().responseComplete();
        //writePDFToResponse(context.getExternalContext(), baos, filename);
        //writePDFToResponseNew(context.getExternalContext(), baos, filename);

    } catch (IOException e) {
        throw new IOException(e.getMessage());
    } catch (Exception ex) {
        Logger.getLogger(PDFExporter2.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.algoboss.erp.util.report.PDFExporter2.java

License:Apache License

public void export2(FacesContext context, String htmltable, String filename) throws IOException {
    try {//from  w ww . j  a va2  s. c o  m
        Document document = new Document(PageSize.A4);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream baos2 = new ByteArrayOutputStream(1000000);
        FileOutputStream file = new FileOutputStream("D://Documents//testeLink.pdf");
        PdfWriter pdfWriter = PdfWriter.getInstance(document, file);
        document.open();
        document.addAuthor("Algo Boss");
        document.addCreator("Real's HowTo");
        document.addSubject("Thanks for your support");
        document.addCreationDate();
        document.addTitle("Please read this");
        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        DataTableRenderer render = new DataTableRenderer();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();
        ExternalContext externalContext = context.getExternalContext();
        //BufferedWriter writer = new BufferedWriter(baos2,true);
        OutputStreamWriter writer = new OutputStreamWriter(baos2, "iso-8859-1");

        context.setResponseWriter(context.getRenderKit().createResponseWriter(writer, null, "iso-8859-1"));
        //render.encodeEnd(context, table);
        //System.out.println(new String(baos2.toByteArray(), "utf-8"));
        /*writer.write(
         "<link href=\"/ERP/f/javax.faces.resource/theme.css?ln=primefaces-cupertino\" rel=\"stylesheet\" type=\"text/css\">\n"
         + "<link href=\"/ERP/f/javax.faces.resource/primefaces.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\">\n"
         + "<link href=\"/ERP/f/javax.faces.resource/schedule/schedule.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\">\n"
         + "<meta content=\"application/pdf; charset=UTF-8\" http-equiv=\"Content-Type\">\n"
         + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/default.css\">\n"
         + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/cssLayout.css\">\n"
         + "<title>Plataforma de Gerenciamento</title>");*/
        /*
         externalContext.setResponseHeader("Expires", "0");
         externalContext.setResponseHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
         externalContext.setResponseHeader("Pragma", "public");
         externalContext.setResponseHeader("Content-disposition", "inline;filename=" + filename + ".pdf");
         externalContext.setResponseContentType("application/pdf");
         externalContext.setResponseContentLength(baos.size());
         externalContext.addResponseCookie(Constants.DOWNLOAD_COOKIE, "true", new HashMap<String, Object>());
         OutputStream out = externalContext.getResponseOutputStream();
         baos.writeTo(out);
         externalContext.responseFlushBuffer();     
         */

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;filename=\"" + filename + "\";");

        //String str = "<html><head></head><body>" + new String(baos2.toByteArray()) + "</body></html>";
        String str = "<html><head></head><body>"
                + "<link href=\"/ERP/f/javax.faces.resource/theme.css?ln=primefaces-cupertino\" rel=\"stylesheet\" type=\"text/css\"/>\n"
                + "<link href=\"/ERP/f/javax.faces.resource/primefaces.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\"/>\n"
                + "<link href=\"/ERP/f/javax.faces.resource/schedule/schedule.css?ln=primefaces\" rel=\"stylesheet\" type=\"text/css\"/>\n"
                + "<meta content=\"application/pdf; charset=UTF-8\" http-equiv=\"Content-Type\"/>\n"
                + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/default.css\"/>\n"
                + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/ERP/resources/css/cssLayout.css\"/>\n"
                + "<title>Plataforma de Gerenciamento</title>"
                + "<div id='tabView:ad_j_id77' class='ui-datatable ui-widget ui-algo-element ui-algo-element-container data-list'><div class='ui-datatable-header ui-widget-header ui-corner-top'><div id='tabView:ad_j_id77:ad_j_id41' class='ui-panel ui-widget ui-widget-content ui-corner-all ui-algo-element ui-algo-element-container'><div id='tabView:ad_j_id77:ad_j_id41_content' class='ui-panel-content ui-widget-content'><label id='tabView:ad_j_id77:ad_j_id39' class='ui-outputlabel ui-algo-element ui-algo-element-value'>Listagem: FUNCION?RIO</label><label id='tabView:ad_j_id77:ad_j_id40' class='ui-outputlabel ui-algo-element ui-algo-element-value' style='float:right;top:-5px;position: relative;'>Pesquisar: <input id='tabView:ad_j_id77:globalFilter' name='tabView:ad_j_id77:globalFilter' type='text' value='' onkeyup='funcionarioDataTable.filter()' class='ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all ui-algo-element  ui-algo-element-value' /><script id='tabView:ad_j_id77:globalFilter_s' type='text/javascript'>PrimeFaces.cw('InputText','widget_tabView_ad_j_id77_globalFilter',{id:'tabView:ad_j_id77:globalFilter'});</script></label></div></div><script id='tabView:ad_j_id77:ad_j_id41_s' type='text/javascript'>PrimeFaces.cw('Panel','widget_tabView_ad_j_id77_ad_j_id41',{id:'tabView:ad_j_id77:ad_j_id41'});</script></div><div id='tabView:ad_j_id77_paginator_top' class='ui-paginator ui-paginator-top ui-widget-header'><span class='ui-paginator-first ui-state-default ui-corner-all ui-state-disabled'><span class='ui-icon ui-icon-seek-first'>p</span></span><span class='ui-paginator-prev ui-state-default ui-corner-all ui-state-disabled'><span class='ui-icon ui-icon-seek-prev'>p</span></span><span class='ui-paginator-pages'><span class='ui-paginator-page ui-state-default ui-state-active ui-corner-all'>1</span><span class='ui-paginator-page ui-state-default ui-corner-all'>2</span></span><span class='ui-paginator-next ui-state-default ui-corner-all'><span class='ui-icon ui-icon-seek-next'>p</span></span><span class='ui-paginator-last ui-state-default ui-corner-all'><span class='ui-icon ui-icon-seek-end'>p</span></span></div><div class='ui-datatable-tablewrapper'><table role='grid'><thead id='tabView:ad_j_id77_head'><tr role='row'><th id='tabView:ad_j_id77:ad_j_id44' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span>Nome</span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id44:filter' name='tabView:ad_j_id77:ad_j_id44:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id48' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id45'>Usurio</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id48:filter' name='tabView:ad_j_id77:ad_j_id48:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id52' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id49'>Admisso</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id52:filter' name='tabView:ad_j_id77:ad_j_id52:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id56' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader' style='text-align:center;'><span><span id='tabView:ad_j_id77:ad_j_id53'>Afastado</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id56:filter' name='tabView:ad_j_id77:ad_j_id56:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id60' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id57'>Veculo</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id60:filter' name='tabView:ad_j_id77:ad_j_id60:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id64' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id61'>MATR?CULA</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id64:filter' name='tabView:ad_j_id77:ad_j_id64:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id68' class='ui-state-default ui-sortable-column ui-filter-column' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id65'>CPF</span></span><span class='ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s'></span><input id='tabView:ad_j_id77:ad_j_id68:filter' name='tabView:ad_j_id77:ad_j_id68:filter' class='ui-column-filter ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all' value='' autocomplete='off' style='display:none;' /></th><th id='tabView:ad_j_id77:ad_j_id72' class='ui-state-default' role='columnheader'><span><span id='tabView:ad_j_id77:ad_j_id69'>SAL?RIO</span></span></th><th id='tabView:ad_j_id77:ad_j_id76' class='ui-state-default' role='columnheader' style='text-align:center;'><span>Ao</span></th></tr></thead><tfoot></tfoot><tbody id='tabView:ad_j_id77_data' class='ui-datatable-data ui-widget-content'><tr data-ri='0' data-rk='315' class='ui-widget-content ui-datatable-even' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id42'>MARCUS2</span><a id='tabView:ad_j_id77:0:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id46'>SDASA2</span><a id='tabView:ad_j_id77:0:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id50'>2013/08/15</span><a id='tabView:ad_j_id77:0:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:0:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:0:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id58'>JQV-4404 - GM</span><a id='tabView:ad_j_id77:0:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id62'>123124</span><a id='tabView:ad_j_id77:0:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id66'></span><a id='tabView:ad_j_id77:0:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:0:ad_j_id70'></span><a id='tabView:ad_j_id77:0:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:0:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:0:ad_j_id74'> | </span><a id='tabView:ad_j_id77:0:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:0:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='1' data-rk='318' class='ui-widget-content ui-datatable-odd' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id42'>PAULO</span><a id='tabView:ad_j_id77:1:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id46'>PAULO</span><a id='tabView:ad_j_id77:1:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id50'>2013/08/07</span><a id='tabView:ad_j_id77:1:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:1:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:1:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id58'></span><a id='tabView:ad_j_id77:1:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id62'></span><a id='tabView:ad_j_id77:1:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id66'></span><a id='tabView:ad_j_id77:1:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:1:ad_j_id70'></span><a id='tabView:ad_j_id77:1:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:1:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:1:ad_j_id74'> | </span><a id='tabView:ad_j_id77:1:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:1:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='2' data-rk='319' class='ui-widget-content ui-datatable-even' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id42'>MAIZA</span><a id='tabView:ad_j_id77:2:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id46'>MAIZA</span><a id='tabView:ad_j_id77:2:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id50'></span><a id='tabView:ad_j_id77:2:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:2:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:2:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id58'>BMW - BMW-9999</span><a id='tabView:ad_j_id77:2:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id62'>TESTE123123</span><a id='tabView:ad_j_id77:2:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id66'>123.123.123-12</span><a id='tabView:ad_j_id77:2:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:2:ad_j_id70'>12.345,67</span><a id='tabView:ad_j_id77:2:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:2:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:2:ad_j_id74'> | </span><a id='tabView:ad_j_id77:2:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:2:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='3' data-rk='320' class='ui-widget-content ui-datatable-odd' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id42'>ELAINE</span><a id='tabView:ad_j_id77:3:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id46'>ELAINE</span><a id='tabView:ad_j_id77:3:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id50'>2013/08/22</span><a id='tabView:ad_j_id77:3:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:3:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:3:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id58'>JQV-4404 - GM</span><a id='tabView:ad_j_id77:3:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id62'></span><a id='tabView:ad_j_id77:3:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id66'></span><a id='tabView:ad_j_id77:3:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:3:ad_j_id70'></span><a id='tabView:ad_j_id77:3:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:3:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:3:ad_j_id74'> | </span><a id='tabView:ad_j_id77:3:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:3:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='4' data-rk='321' class='ui-widget-content ui-datatable-even' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id42'>CLEIDE2</span><a id='tabView:ad_j_id77:4:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id46'>CLEIDE DA SILVA</span><a id='tabView:ad_j_id77:4:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id50'>2013/08/19</span><a id='tabView:ad_j_id77:4:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:4:ad_j_id54' class=' ui-icon ui-icon-check ' style='display:inline-block;'>true</span><a id='tabView:ad_j_id77:4:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id58'>EEQEWEQE - QWEQ</span><a id='tabView:ad_j_id77:4:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id62'></span><a id='tabView:ad_j_id77:4:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id66'>123.123.123-12</span><a id='tabView:ad_j_id77:4:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:4:ad_j_id70'>456,70</span><a id='tabView:ad_j_id77:4:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:4:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:4:ad_j_id74'> | </span><a id='tabView:ad_j_id77:4:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:4:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr><tr data-ri='5' data-rk='322' class='ui-widget-content ui-datatable-odd' role='row' aria-selected='false'><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id42'>GABRIELA</span><a id='tabView:ad_j_id77:5:ad_j_id43' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id43',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id46'>GABRIELA</span><a id='tabView:ad_j_id77:5:ad_j_id47' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id47',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id50'>2013/08/20</span><a id='tabView:ad_j_id77:5:ad_j_id51' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id51',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><span id='tabView:ad_j_id77:5:ad_j_id54' class=' ui-icon ui-icon-cancel ' style='display:inline-block;'>false</span><a id='tabView:ad_j_id77:5:ad_j_id55' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id55',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id58'></span><a id='tabView:ad_j_id77:5:ad_j_id59' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id59',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id62'></span><a id='tabView:ad_j_id77:5:ad_j_id63' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id63',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id66'></span><a id='tabView:ad_j_id77:5:ad_j_id67' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id67',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell'><span id='tabView:ad_j_id77:5:ad_j_id70'></span><a id='tabView:ad_j_id77:5:ad_j_id71' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id71',update:'tabView:j_idt233'});return false;'></a></td><td role='gridcell' style='text-align:center;'><a id='tabView:ad_j_id77:5:ad_j_id73' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id73',update:'tabView:j_idt233'});return false;'>Editar</a><span id='tabView:ad_j_id77:5:ad_j_id74'> | </span><a id='tabView:ad_j_id77:5:ad_j_id75' href='#' class='ui-commandlink ui-widget' onclick='PrimeFaces.ab({source:'tabView:ad_j_id77:5:ad_j_id75',update:'tabView:j_idt233'});return false;'>Excluir</a></td></tr>"
                + "</tbody></table></div></div>" + "</body></html>";
        ElementHandler eh = new ElementHandler() {
            public List<Writable> elements = new ArrayList<Writable>();

            @Override
            public void add(Writable wrtbl) {
                if (wrtbl instanceof WritableElement) {
                    elements.add(wrtbl);
                }
                //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
        //worker.parseXHtml(eh, new StringReader(htmltable));
        worker.parseXHtml(pdfWriter, document, new StringReader(htmltable));

        response.setContentLength(baos.size());
        response.getOutputStream().write(baos.toByteArray(), 0, baos.size());
        //System.out.println(str);
        document.close();
        pdfWriter.close();
        baos.close();
        baos2.close();
        file.close();
        FacesContext.getCurrentInstance().responseComplete();
        //writePDFToResponse(context.getExternalContext(), baos, filename);
        //writePDFToResponseNew(context.getExternalContext(), baos, filename);

    } catch (IOException e) {
        throw new IOException(e.getMessage());
    } catch (Exception ex) {
        Logger.getLogger(PDFExporter2.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.algoboss.erp.util.report.PDFExporter2.java

License:Apache License

public void export(FacesContext context, String cssstring, String htmlstring, String filename)
        throws IOException {
    try {//from w  ww  . jav a  2  s. co m
        InputStream is = new ByteArrayInputStream(htmlstring.getBytes());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // step 1
        Document document = new Document();

        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, baos);

        writer.setInitialLeading(12.5f);

        // step 3
        document.open();

        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        String[] cssstringArray = cssstring.split(";");
        FacesContext aFacesContext = FacesContext.getCurrentInstance();
        ServletContext context2 = (ServletContext) aFacesContext.getExternalContext().getContext();
        String realPath = context2.getRealPath("/");
        System.out.println(realPath);
        System.out.println(context2.getContextPath());
        //System.out.println(context2.getResource(""));  
        System.out.println(aFacesContext.getExternalContext().getContextName());
        System.out.println(aFacesContext.getExternalContext().getRequestContextPath()
                + aFacesContext.getExternalContext().getRequestServletPath());
        //System.out.println(aFacesContext.getExternalContext().getRequestServletPath());
        //System.out.println(aFacesContext.getExternalContext().getRequestPathInfo());
        //System.out.println(aFacesContext.getExternalContext().getRequestScheme());
        //System.out.println(aFacesContext.getExternalContext().getRequestScheme());
        //System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
        for (int i = 0; i < cssstringArray.length; i++) {
            String cssHref = cssstringArray[i];
            try {
                URL url = new URL(aFacesContext.getExternalContext().getRequestScheme(),
                        aFacesContext.getExternalContext().getRequestServerName(),
                        aFacesContext.getExternalContext().getRequestServerPort(), cssHref);
                InputStream csspathtest = url.openStream();
                CssFile cssfiletest = XMLWorkerHelper.getCSS(csspathtest);
                cssResolver.addCss(cssfiletest);
            } catch (IOException ex) {
                Logger.getLogger(PDFExporter2.class.getName()).log(Level.SEVERE, null, ex);
            }
            //String realPath2 = context2.getRealPath(cssHref);

            //InputStream csspathtest2 = aFacesContext.getExternalContext().getResourceAsStream(realPath2);
            //System.out.println(cssfiletest);
        }

        Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
                new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser p = new XMLParser(worker);
        p.parse(is, Charset.forName("iso-8859-1"));//new FileInputStream("results/demo2/walden.html"));

        // step
        document.close();

        // post back...
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.setContentType("application/pdf");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Content-Type", "application/pdf");
        response.setHeader("Content-disposition", "inline;filename=\"" + filename + "\";");
        response.setContentLength(baos.size());
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close();
        context.responseComplete();

    } catch (IOException e) {
        throw new IOException(e.getMessage());
    } catch (Exception ex) {
        Logger.getLogger(PDFExporter2.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.alnaser.view.GestionProduit.java

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {
    System.out.println("!!!!!!!");
    try {//w w  w .j  a  v  a2 s.co  m
        OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

        Document document = new Document();
        PdfWriter.getInstance(document, file);

        document.open();
        // a table with three columns
        PdfPTable table = new PdfPTable(7);
        // the cell object
        PdfPCell cell, cel2, cel3, cel4, cel5, cel6, cel7;
        // we add a cell with colspan 3
        cell = new PdfPCell(new Phrase("Code"));
        cel2 = new PdfPCell(new Phrase("Ref"));
        cel3 = new PdfPCell(new Phrase("Desg"));
        cel4 = new PdfPCell(new Phrase("Four"));
        cel5 = new PdfPCell(new Phrase("Remise"));
        cel6 = new PdfPCell(new Phrase("Prix"));
        cel7 = new PdfPCell(new Phrase("Stock"));

        table.addCell(cell);
        table.addCell(cel2);
        table.addCell(cel3);
        table.addCell(cel4);
        table.addCell(cel5);
        table.addCell(cel6);
        table.addCell(cel7);

        for (Produit p : list) {
            table.addCell(p.getCodeProduit());
            table.addCell(p.getReference());
            table.addCell(p.getDeseignation());
            table.addCell(p.getFournisseur());
            table.addCell(p.getRemise() + "");
            table.addCell(p.getPrix() + "");
            table.addCell(p.getStock() + "");
        }
        document.add(table);
        document.add(new Paragraph(new Date().toString()));

        document.close();
        file.close();

    } catch (Exception e) {

        e.printStackTrace();
    }

}

From source file:com.alokomkar.aliensonearth.report.AbstractPdfReport.java

/**
 * /*www .ja  va  2s.c  o m*/
 * @param fileName file to be created (with location)
 * @return created fileName on success, else null
 */
public final String generatePage(String fileName) {

    if (fileName == null)
        throw new NullPointerException(ProjectMessages.EMPTY_FILE_NAME);

    if (fileName.endsWith(".pdf") == false)
        fileName += ".pdf";

    Document document = new Document(PageSize.A4);

    try {

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        HeaderFooter event = new HeaderFooter();

        writer.setPageEvent(event);

        document.open();

        addPageMetaData(document);

        addContent(document);

        document.close();

        return fileName;

    } catch (DocumentException ex) {

        Logger.getLogger(AbstractPdfReport.class.getName()).log(Level.SEVERE, null, ex);
        document.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(AbstractPdfReport.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:com.alokomkar.aliensonearth.report.AbstractPdfReport.java

/**
 * //from w ww.  j  a  va  2 s . com
 * @param fileName file to be created (with location)
 * @return created fileName on success, else null
 */
public final String generateLandScapePage(String fileName) {

    if (fileName == null)
        throw new NullPointerException(ProjectMessages.EMPTY_FILE_NAME);

    if (fileName.endsWith(".pdf") == false)
        fileName += ".pdf";

    Document document = new Document(PageSize.A4.rotate());

    try {

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        HeaderFooter event = new HeaderFooter();

        writer.setPageEvent(event);

        document.open();

        addPageMetaData(document);

        addContent(document);

        document.close();

        return fileName;

    } catch (DocumentException ex) {

        Logger.getLogger(AbstractPdfReport.class.getName()).log(Level.SEVERE, null, ex);
        document.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(AbstractPdfReport.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:com.apcb.utils.utils.PdfCreator.java

public static String createPdf(String fileName, String innerHTML, Document document) throws Exception {

    log.info("Creando HTML nombre: " + fileName);
    File fileHTML = new File(fileName);
    fileHTML.getParentFile().mkdirs();//  www.  j av  a  2s.c o m
    PrintWriter writerHTML = new PrintWriter(fileHTML, "UTF-8");
    writerHTML.println(innerHTML);
    writerHTML.flush();
    writerHTML.close();
    log.info("Creado HTML ruta: " + fileHTML.getAbsolutePath());

    log.info("Creando PDF nombre: " + fileName + ".pdf");
    File filePDF = new File(fileName + ".pdf");

    filePDF.getParentFile().mkdirs();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName + ".pdf"));
    writer.setInitialLeading(12);
    document.open();
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(fileHTML));
    document.close();

    log.info("Creado PDF ruta: " + filePDF.getAbsolutePath());
    log.info("filePDF.getName() " + filePDF.getName());
    return filePDF.getName();
}

From source file:com.apcb.utils.utils.PdfCreator.java

public static String createPdfWStyles(String fileName, String innerHTML, Document document, String CSS,
        String imagesSourcesPath) throws IOException, DocumentException {

    log.info("Creando HTML nombre: " + fileName);
    File fileHTML = new File(fileName);
    fileHTML.getParentFile().mkdirs();/*from w  ww  .j a v  a2s  .  c  o m*/
    PrintWriter writerHTML = new PrintWriter(fileHTML, "UTF-8");
    writerHTML.println(innerHTML);
    writerHTML.flush();
    writerHTML.close();
    log.info("Creado HTML ruta: " + fileHTML.getAbsolutePath());

    log.info("Creando PDF nombre: " + fileName + ".pdf");
    File filePDF = new File(fileName + ".pdf");

    //File file = new File(fileName);
    filePDF.getParentFile().mkdirs();

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName + ".pdf"));
    document.open();

    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    htmlContext.setImageProvider(new AbstractImageProvider() {
        @Override
        public String getImageRootPath() {
            return imagesSourcesPath;
        }
    });

    // Pipelines
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(innerHTML.getBytes()));

    document.close();
    log.info("Creado PDF ruta: " + filePDF.getAbsolutePath());
    log.info("filePDF.getName() " + filePDF.getName());
    return filePDF.getName();
}

From source file:com.app.gpo.pdf.utils.AbstractITextPdfView.java

License:Open Source License

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // IE workaround: write into byte array first.
    ByteArrayOutputStream baos = createTemporaryOutputStream();

    // Apply preferences and build metadata.
    Document document = newDocument();
    PdfWriter writer = newWriter(document, baos);
    prepareWriter(model, writer, request);
    buildPdfMetadata(model, document, request);

    // Build PDF document.
    document.open();
    buildPdfDocument(model, document, writer, request, response);
    document.close();/*w w w.  j  a  va  2  s  .co  m*/

    // Flush to HTTP response.
    writeToResponse(response, baos);
}