
org.sakaiproject.jsf2.spreadsheet.SpreadsheetDataFileWriterPdf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsf2-spreadsheet Show documentation
Show all versions of jsf2-spreadsheet Show documentation
This provides some utility classes to support downloading
of spreadsheet-style data as an XLS or CSV file. It has
been broken out into its own artifact to avoid adding a
POI dependency to the standard Sakai JSF components JAR.
The newest version!
/**
* Copyright (c) 2003-2021 The Apereo Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/ecl2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sakaiproject.jsf2.spreadsheet;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.sakaiproject.component.cover.ServerConfigurationService;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SpreadsheetDataFileWriterPdf implements SpreadsheetDataFileWriter {
private List> studentInfo = new ArrayList>();
private static final int MAX_COLUMNS = 8;
private static Font font, boldFont;
public void writeDataToResponse(List> spreadsheetData, String fileName, HttpServletResponse response) {
response.setContentType("application/pdf");
SpreadsheetUtil.setEscapedAttachmentHeader(response, fileName + ".pdf");
Document document = new Document(new Rectangle(842.0f, 595.0f), 0, 0, 0, 0);
//document.setMargins(0, 0, 0, 0);
OutputStream out = null;
try {
out = response.getOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
if(spreadsheetData != null && spreadsheetData.size() > 0){
int rows = spreadsheetData.size();
int cols = spreadsheetData.get(0).size();
//keeps track of where we left off when switching to a new table
int startIndex = 0;
while(startIndex < cols){
document.add(new Paragraph("\n\n"));
int subCols;
if(startIndex == 0){
subCols = (cols - startIndex > MAX_COLUMNS) ? MAX_COLUMNS : cols - startIndex;
}else{
//need to take into account that we tack on the first two columns (userId and name),
subCols = (cols - startIndex + 2 > MAX_COLUMNS) ? MAX_COLUMNS - 2 : cols - startIndex;
}
Table t = new Table(startIndex == 0 ? subCols : subCols + 2, rows);
t.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
t.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
//t.setBorderColor(new Color(220, 255, 100));
t.setPadding(4);
//t.setSpacing(5);
t.setBorderWidth(1);
for (int rowDataIndex = 0; rowDataIndex < spreadsheetData.size(); rowDataIndex++){
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy