3

I need to create a PDF containing some tables. When looking on google/stackoverflow the most frequent API seems to be iText but that's under the AGPL licence and thus not desirable for my purposes. I also frequently see apache pdfbox but that does not seem to have native support for tables (although a slightly hacky way was posted at Apache PDFBox Java library - Is there an API for creating tables? )

Does anyone have any recommendations?

Community
  • 1
  • 1
jack
  • 1,902
  • 5
  • 32
  • 43

4 Answers4

3

You could try to use XSL-FO + FOP to create your tables. For example, see http://ashishpatil.blogspot.com/2006/06/creating-pdfs-with-apache-fop.html

Pierre
  • 34,472
  • 31
  • 113
  • 192
3

go for JasperReport

jmj
  • 237,923
  • 42
  • 401
  • 438
1

You may have reached a point where the complexity of your documents is high enough to warrant a domain specific language.

In that case I would suggest you generate DocBook XML which can then be easily transformed to many output formats. See http://wiki.docbook.org/topic/DocBookPublishingTools

I would suggest for small documents the XSL-FO approach.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

JasperReport can be useful if your pdf is not dynamic. But if it is dynamic, and you need to make it on the fly, then I recommend DynamicJasper

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Page;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;

HttpServletResponse response = getContext().getResponse();

            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setCharacterEncoding("ISO-8859-1");

            response.setContentType("application/pdf");     
            response.setHeader("Content-Disposition", "attachment; filename=example.pdf");


DynamicReportBuilder drb = new DynamicReportBuilder();

                Style detailStyle = new Style();
                detailStyle.setBorderTop(Border.THIN);
                detailStyle.setBorderBottom(Border.THIN);
                detailStyle.setBorderLeft(Border.THIN);
                detailStyle.setBorderRight(Border.THIN);

                Style headerStyle = new Style();
                headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
                headerStyle.setBorderBottom(Border.THIN);
                headerStyle.setBackgroundColor(Color.gray);
                headerStyle.setTextColor(Color.white);
                headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
                headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
                headerStyle.setTransparency(Transparency.OPAQUE);

                Style headerVariables = new Style();
                headerVariables.setFont(Font.ARIAL_MEDIUM_BOLD);
                headerVariables.setHorizontalAlign(HorizontalAlign.RIGHT);
                headerVariables.setVerticalAlign(VerticalAlign.MIDDLE);

                Style titleStyle = new Style();
                titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));

                Style importeStyle = new Style();
                importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
                Style oddRowStyle = new Style();
                oddRowStyle.setBorder(Border.NO_BORDER);
                oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
                oddRowStyle.setTransparency(Transparency.OPAQUE);

                drb.addTitle(sessao.getNomeCliente());              
                drb.addTitleStyle(titleStyle);

                drb.addSubtitle("Consulta O.S.");

                drb.addOddRowBackgroundStyle(oddRowStyle);
                drb.addDefaultStyles(titleStyle, null, headerStyle, detailStyle);

                drb.addPageSizeAndOrientation(Page.Page_A4_Landscape());

                int top = 10;
                int bottom = 10;
                int left = 10;
                int right = 10;
drb.addMarginss(top, bottom, left, right);  


if (searchResults.isThereThisField()) {

                    AbstractColumn columnState = ColumnBuilder.getInstance()
                    .addColumnProperty("numeroOs", Integer.class.getName())         
                    .addTitle("This Field")             
                    .addWidth(5)
                    .build();   

                    drb.addColumn(columnState);

                }

Do it to all fields.

drb.addUseFullPageWidth(true);

                DynamicReport dr = drb.build();

JRDataSource ds = new JRBeanCollectionDataSource(lista);

                JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds );

                byte[] b = JasperExportManager.exportReportToPdf(jp);
                response.getOutputStream().write(b);

                response.flushBuffer();
                setPath(null);

I just got this example in my work, i'm just trying to give you a good information. I don't really know how to explain, I just know it works.

Gondim
  • 3,038
  • 8
  • 44
  • 62