how to export a list in pdf with java
for a web application I need to implement a function that allowed to export a list in pdf??
How shall i do it? there is a class that allowed it ?
or if you have any suggestions please??
thank you 开发者_JAVA技巧in advance
There's a free library called iText that you can use to generate PDFs (writing it on the fly to the HTTP response, or as a batch process) using Java. For simple things, it's quite easy to use, although the online documentation is virtually non-existant — instead, you support the project by buying the book iText in Action (one of the options is to buy a paper copy including a PDF ebook, so you're not waiting for the post).
You can use iText for that (its latest versions is under GNU Affero GPLv3 license). Have a look in this tutorial regarding what you need.
A simple solution is PDFBox for Java, which has facilities for (among other things) creating PDFs. Since it sounds like your use case is really simply (output a list to a PDF), the prewritten utility TextToPDF might be all you need to use from PDFBox.
I think the best solution for free application reporting is JasperReports.
You do not have anything to do with PDF, just define the data (a list of POJOS if you want), the fields, the layout and create output. PDF is one of the supported formats (this is based on iText).
I use Apache Pdf Box as mentioned in other answers but I am trying to show concrete code examples as none of answers have shown that & a newbie might struggle as per docs available online esp. around the coordinate system & creation of rows.
Below is utility method that I have. valuesToExport
are rows that I wish to write to pdf one by one & filePath
is pdf file to write data to.
I call this method from multiple kind of exports ( i.e. when I have List of different kind of POJOs ). I first convert each pojo fields that are to be written to pdf into a String[]
and put in a List. Each element of String[]
represents a column.
You can change multiple constants as per your need esp. rows per pdf page . I have used 10 rows per page in sample code.
import java.util.List;
import java.nio.file.Path;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
public static void writeToPdf(List<String[]> valuesToExport, Path filePath) {
try (PDDocument doc = new PDDocument()) {
PDFont font = PDType1Font.HELVETICA;
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.setFont(font, 12);
int lines = 1;
float pageHeight = page.getMediaBox().getHeight();
for (String[] row : valuesToExport) {
int startX = 0;
for (String column : row) {
content.beginText();
content.newLineAtOffset(startX, pageHeight - 50 * lines);
startX += startX + 100;
content.showText(column);
content.endText();
}
++lines;
if (lines > 10) {
page = new PDPage();
doc.addPage(page);
content.close();
content = new PDPageContentStream(doc, page);
content.setFont(font, 12);
}
}
content.close();
doc.save(filePath.toFile());
} catch (IOException ex) {
//either log exception or rethrow it
}
}
精彩评论