我需要创建一个包含一些表的PDF。 在google / stackoverflow上查看时,最常见的API似乎是iText,但这是在AGPL许可下,因此不适合我的目的。 我也经常看到apache pdfbox,但似乎没有对表的原生支持(尽管在Apache PDFBox Java library - Is there an API for creating tables?发布了一种略带hacky的方式)
有人有任何建议吗?
答案 0 :(得分:3)
您可以尝试使用XSL-FO + FOP来创建表格。例如,请参阅http://ashishpatil.blogspot.com/2006/06/creating-pdfs-with-apache-fop.html
答案 1 :(得分:3)
答案 2 :(得分:1)
您可能已达到文档复杂程度足以保证特定于域的语言的程度。
在这种情况下,我建议您生成DocBook XML,然后可以轻松地将其转换为多种输出格式。见http://wiki.docbook.org/topic/DocBookPublishingTools
我建议小文档使用XSL-FO方法。
答案 3 :(得分:0)
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);
}
对所有领域都这样做。
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);
我刚刚在工作中得到了这个例子,我只想给你一个很好的信息。我真的不知道如何解释,我只知道它有效。