Browse Source

还不知道如何写的pdf

4.1.3.A
jueyue 9 years ago
parent
commit
d3cd1b9eff
3 changed files with 212 additions and 0 deletions
  1. +68
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java
  2. +123
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java
  3. +21
    -0
      easypoi-base/src/main/java/org/jeecgframework/poi/pdf/package-info.java

+ 68
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/PdfExportUtil.java View File

@@ -0,0 +1,68 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.jeecgframework.poi.pdf;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.pdf.export.PdfExportServer;
import com.itextpdf.text.Document;
/**
* PDF 导出工具类
*
* @author JueYue
* @date 2015年10月6日 下午8:14:01
* @version 1.0
*/
public class PdfExportUtil {
/**
* 根据注解导出数据
* @param entity
* 表格标题属性
* @param pojoClass
* PDF对象Class
* @param dataSet
* PDF对象数据List
*/
public static Document exportPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet,
OutputStream outStream) {
return new PdfExportServer(outStream).createPdf(entity, pojoClass, dataSet);
}
/**
* 根据Map创建对应的PDF
* @param entity
* 表格标题属性
* @param pojoClass
* PDF对象Class
* @param dataSet
* PDF对象数据List
*/
public static Document exportPdf(ExportParams entity, List<ExcelExportEntity> entityList,
Collection<? extends Map<?, ?>> dataSet,
OutputStream outStream) {
return new PdfExportServer(outStream).createPdfForMap(entity, entityList, dataSet);
}
}

+ 123
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/export/PdfExportServer.java View File

@@ -0,0 +1,123 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.jeecgframework.poi.pdf.export;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jeecgframework.poi.excel.annotation.ExcelTarget;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.export.base.ExportBase;
import org.jeecgframework.poi.util.PoiPublicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
* PDF导出服务,基于Excel基础的导出
* @author JueYue
* @date 2015年10月6日 下午8:21:08
*/
public class PdfExportServer extends ExportBase {
private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class);
private Document document;
public PdfExportServer(OutputStream outStream) {
try {
document = new Document();
PdfWriter.getInstance(document, outStream);
document.open();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
public Document createPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
try {
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
if (entity.isAddIndex()) {
excelParams.add(indexExcelEntity(entity));
}
// 得到所有字段
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
String targetId = etarget == null ? null : etarget.value();
getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass,
null);
sortAllParams(excelParams);
int index = entity.isCreateHeadRows() ? createHeaderAndTitle(entity, excelParams) : 0;
int titleHeight = index;
//setCellWith(excelParams, sheet);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
} finally {
document.close();
}
return document;
}
private int createHeaderAndTitle(ExportParams entity,
List<ExcelExportEntity> excelParams) throws DocumentException {
PdfPTable table = new PdfPTable(excelParams.size());
for (int i = 0; i < excelParams.size(); i++) {
table.addCell(new Phrase(excelParams.get(i).getName(), getFont()));
}
for (int i = 0; i < excelParams.size(); i++) {
table.addCell(new Phrase("test", getFont()));
}
document.add(table);
return 0;
}
private Font getFont() {
try {
//用以支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese);
return font;
} catch (DocumentException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
Font font = new Font(FontFamily.UNDEFINED);
return font;
}
public Document createPdfForMap(ExportParams entity, List<ExcelExportEntity> entityList,
Collection<? extends Map<?, ?>> dataSet) {
return document;
}
}

+ 21
- 0
easypoi-base/src/main/java/org/jeecgframework/poi/pdf/package-info.java View File

@@ -0,0 +1,21 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
/**
* EasyPOI的PDF模块
* @author JueYue
* @date 2015年10月6日 下午8:13:19
*/
package org.jeecgframework.poi.pdf;

Loading…
Cancel
Save