Java API for creating pdf with tables: any recommendations?

I need to create a PDF file containing some tables. When you browse google / stackoverflow, the most common API seems to be iText, but this is under the AGPL license and therefore not desirable for my purposes. I also often see apache pdfbox, but it doesn't seem to have built-in table support (although a slightly hacked way was posted in the Apache PDFBox Java library - is there an API for creating tables? )

Does anyone have any recommendations?

+3
source share
4 answers

JasperReport may be useful if your pdf is not dynamic. But if it's dynamic and you need to do it on the fly, 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 good information. I really donโ€™t know how to explain, I just know that this works.

0
source

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

+3
source
+3

, , , , .

DocBook XML, . . http://wiki.docbook.org/topic/DocBookPublishingTools

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

+1
source

Source: https://habr.com/ru/post/1782410/


All Articles