HTML转PDF


1
2
3
4
5
6
7
8
9
10
11
maven包
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>2.0.2</version>
        </dependency>
package com.shsc.srm.util;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.kernel.colors.Color;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.layout.Document;
import com.shsc.framework.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
* 利用HTML代码片段生成PDF
*
* zkh 2018313日 下午2:05:17
*/
@Slf4j
@Component
public class HtmlToPdf {
private final static String DEST = "./target/desc/备案申请书.pdf";
private final static String SRC = "./src/main/resources/template/备案申请书.html";
public static final String FONT = "/font/SimSun.ttf";

public Map<String, Object> html2Pdf(String htmlStr) throws Exception {
Map<String, Object> map = new HashMap<>();
ByteArrayInputStream inputStream = null;
ByteArrayOutputStream out = null;
byte[] buffer;
try {
ConverterProperties props = new ConverterProperties();
DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
defaultFontProvider.addFont(FONT);
props.setFontProvider(defaultFontProvider);
out = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(out);
PdfDocument pdf = new PdfDocument(writer);
Document document = HtmlConverter.convertToDocument(new ByteArrayInputStream(htmlStr.getBytes()), pdf, props);
int pages = pdf.getNumberOfPages();
document.getRenderer().close();
document.close();
pdf.close();
inputStream = new ByteArrayInputStream(out.toByteArray());
buffer = new byte[inputStream.available()];
inputStream.read(buffer);
map.put("pages", pages);
map.put("byteFile", buffer);
return map;
}catch (Exception e){
log.error("html转pdf失败: "+ e);
throw new BusinessException("html转pdf失败");
}finally {
if(inputStream != null){
inputStream.close();
}
if(out != null){
out.close();
}
}
}

// public void htmlCodeComeFromFile(String filePath, String pdfPath) {
// Document document = new Document();
// try {
// StyleSheet st = new StyleSheet();
// st.loadTagStyle("body", "leading", "16,0");
// PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
// document.open();
// BaseFont bfChinese = BaseFont.createFont("STSong-Light",
// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
// Paragraph t = new Paragraph(filePath, FontChinese);
// document.add(t);
// List p = HTMLWorker.parseToList(new FileReader(filePath), st);
// for (int k = 0; k < p.size(); ++k) {
// document.add((Element) p.get(k));
// }
// document.close();
// System.out.println("文档创建成功");
// } catch (Exception e) {
// e.printStackTrace();
// }
// }

public static void main(String[] args) {
try {
HtmlToPdf html = new HtmlToPdf();
html.html2Pdf(SRC);
} catch (Exception e) {
e.printStackTrace();
}
}

class EndPosition implements ILineDrawer {

/** A Y-position. */
protected float y;

/**
* Gets the Y-position.
*
* @return the Y-position
*/
public float getY() {
return y;
}

/*
* (non-Javadoc)
*
* @see
* com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#draw(com.itextpdf.kernel.pdf.
* canvas.PdfCanvas, com.itextpdf.kernel.geom.Rectangle)
*/
@Override
public void draw(PdfCanvas pdfCanvas, Rectangle rect) {
this.y = rect.getY();
}

/*
* (non-Javadoc)
*
* @see com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#getColor()
*/
@Override
public Color getColor() {
return null;
}

/*
* (non-Javadoc)
*
* @see com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#getLineWidth()
*/
@Override
public float getLineWidth() {
return 0;
}

/*
* (non-Javadoc)
*
* @see
* com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#setColor(com.itextpdf.kernel.
* color.Color)
*/
@Override
public void setColor(Color color) {
}

/*
* (non-Javadoc)
*
* @see com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer#setLineWidth(float)
*/
@Override
public void setLineWidth(float lineWidth) {
}

}
}
ˆ Back To Top