iTEXT将html⽂档转PDF,spire.doc包html转word(包括样式修改和添。。。项⽬需求
使⽤velocity模板⽣成html内容,将含有标题 / 段落 / 表格 / 图⽚ 的html⽂件转化成PDF和word格式,并保留良好样式。
iText
起初只想实现word功能,但经过尝试和查询⼀些例如poi等⼯具,都⽆法较好的保留原有的复杂样式,后更换pdf需求进⾏尝试,itext⽣成pdf已经⾮常完美了。
⽣成word样式没有尝试,感觉⽐较⿇烦。
后来采⽤spire的free包将pdf转word样式保持很好,但是项⽬环境中遇到问题,转战html转word。
注意:不要将spire的pdf和word包同时引⽤,如果需要可以直接使⽤office包,会产⽣⼀些例如注册许可⽅⾯的冲突。
spire.pdf / spire.doc / spire.office
很好的第三⽅⼯具,能完美实现html转pdf和word等格式⽂件,最关键的是有free版本,虽然有10页的限制,结合需求页可能够⽤和付费版功能上⽆差别。
付费版本也⼀样可以⽤,只是会在左上⾓有红⾊警告信息,其实word可以⼿动删除,pdf可以通过添加图⽚的⽅式遮盖掉。
注意:我们的项⽬环境是Linux / jdk1.7 / weblogic12.1.3.0.0(pdf转word–使⽤doc包就会报错UnkownSource【原因不明】,最好直接html转word最终解决问题),html转word表头的css样式可能会⽆法解析,使⽤基本样式就好。
html转pdf
引⼊jar包
引⼊itext仓库
<repository>
<id>itext</id>
<name>iText Repository - releases</name>
<url>repo.itextsupport/releases</url>
</repository>
在l引⼊maven(不包括spire.pdf)
<!-- itextpdf -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!-- pdfHTML -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.0</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.0.8</version>
</dependency>
</dependencies>
可以下载spire.pdf.free以及各种实⽤jar包的maven仓库
⽣成pdf
package com.sy.util;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory; import com.itextpdf.dia.MediaDeviceDescription; import com.itextpdf.dia.MediaType;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.font.FontProvider;
import org.apachemons.io.FileUtils;
import org.apache.poi.util.IOUtils;
import java.io.*;
import static com.PageSize.A4;
import static com.PageSize.A3;
public class PdfService {
//private static final Logger LOGGER = Logger(PdfFromHTML.class);
public static final String FONT_NAME = "f";
public void copyFont(String targetDir,String fontPath) {
FileOutputStream fout = null;
FileInputStream fileInputStream = null;
try {
//InputStream inputStream = Class().getClassLoader().getResourceAsStream(FONT_CLASS_PATH); fileInputStream = new FileInputStream(fontPath+"/WEB-INF/f");
File dir = new File(targetDir);
FileUtils.forceMkdir(dir);
fout = new FileOutputStream(targetDir + "f");
if (fileInputStream != null && fout != null) {
org.apachemons.py(fileInputStream, fout);
}
} catch (Exception e) {
//("failed to copy font: ", e);
e.printStackTrace();
} finally {
if(null!=fileInputStream){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=fout){
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void createPdfFromHtml(String pdfFileName, InputStream htmlInputStream, String resourcePrefix){
PdfDocument pdfDoc = null;
FileOutputStream outputStream = null;
PdfWriter pdfWriter = null;
try {
outputStream = new FileOutputStream(resourcePrefix + pdfFileName);
WriterProperties writerProperties = new WriterProperties();
writerProperties.addXmpMetadata();
pdfWriter = new PdfWriter(outputStream, writerProperties);
pdfDoc = createPdfDoc(pdfWriter);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
pdfDocument.setDefaultPageSize(A3);
//转换器
ConverterProperties props = createConverterProperties(resourcePrefix);
props.setBaseUri(resourcePrefix);
//设置媒体的描述⽅式
MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
props.setMediaDeviceDescription(mediaDeviceDescription);
pdfDocument.close();
} catch (Exception e) {
//("failed to create pdf from html exception: ", e);
e.printStackTrace();
} finally {
try {
outputStream.close();
pdfWriter.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
outputStream = null;
pdfWriter = null;
}
IOUtils.closeQuietly(pdfDoc);
}
}
private PdfDocument createPdfDoc(PdfWriter pdfWriter) {
PdfDocument pdfDoc;
pdfDoc = new PdfDocument(pdfWriter);
pdfDoc.setTagged();
}
private ConverterProperties createConverterProperties(String resourcePrefix) {
ConverterProperties props = new ConverterProperties();
props.setFontProvider(createFontProvider(resourcePrefix));
props.setBaseUri(resourcePrefix);
props.setCharset("UTF-8");
DefaultTagWorkerFactory tagWorkerFactory = new DefaultTagWorkerFactory();
props.setTagWorkerFactory(tagWorkerFactory);
return props;
}
private FontProvider createFontProvider(String resourcePrefix) {
FontProvider fp = new FontProvider();
fp.addStandardPdfFonts();
fp.addDirectory(resourcePrefix);
return fp;
}
}
下⾯为main⽅法
//pdf⽂件加载和⽣成统⼀路径
String pdfPath = localFilePath + name + ".pdf";
File temFile = new File(localFilePath);
if (!ists()) {
temFile.mkdirs();
}
// 添加字体,这⼀步很关键,否则中⽂会显⽰不出来
PdfService pdfService = new PdfService();
File file = new File(localFilePath + PdfService.FONT_NAME);
if (!ists()) {
//加载字体库
}
//---------------------
FileInputStream inputStream = new FileInputStream(url);
File pdfFile = new File(pdfPath);
//TODO html To PDF
pdf布局设置(以下内容通过spire.doc/pdf包处理)
package com.sy.util;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
java修改html文件import com.spire.doc.fields.TextRange;
import com.spire.pdf.*;
import com.spire.doc.*;
import com.spire.pdf.annotations.PdfRubberStampAnnotation;
import com.spire.pdf.annotations.appearance.PdfAppearance;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.aphics.*;
import java.awt.*;
import Dimension2D;
import Point2D;
import Rectangle2D;
import com.spire.doc.FileFormat.*;
public class PdfPageSetting {
public static final int FLAG = 2;
public void pdfPageSetting(String url,int flag){
//创建PdfDocument对象
PdfDocument originalDoc = new PdfDocument();
//加载PDF⽂件
originalDoc.loadFromFile(url);
//第⼀页插⼊空⽩页(⽬的是删除spirefree的内置红⾊警告语,主要是为了⽣成word使⽤,
pdf可以⽤添加图⽚⽅法进⾏遮盖)
//创建⼀个新的PdfDocument实例
PdfDocument newDoc = new PdfDocument();
//遍历所有PDF 页⾯
Dimension2D dimension2D = new Dimension();
for (int i = 0; i < Pages().getCount(); i++) {
PdfPageBase page = Pages().get(i);
if (flag==1){
//设置新⽂档页⾯宽、⾼为原来的1倍
float scale = 1.0f;
float width = (float) Size().getWidth() * scale;
float height = (float) Size().getHeight() * scale;
dimension2D.setSize(width, height);
/
/设置新⽂档第⼀页的页边距为左右
PdfMargins margins = new PdfMargins(0, 20);
PdfPageBase newPage = Pages().add(dimension2D, margins);
//复制原⽂档的内容到新⽂档
if (flag==2){
//设置新⽂档页边距为左右0、上下20(不能随意更改,会影响主标题位置)
PdfMargins margins = new PdfMargins(0,20,0,0);
//设置新⽂档页⾯⼤⼩为A3
PdfPageBase newPage = Pages().add(PdfPageSize.A3, margins);
//调整画布,设置内容也根据页⾯的⼤⼩进⾏缩放
double wScale = (Width() - 10) / Width();
double hScale = (Height() - 10) / Height();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论