HTML转成pdf:
<!-- pdf 相关jar包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
栾城网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
public class PdfUtil2 {
//根据html文件生成pdf
public static void parseHtml2PdfByFilePath(String pdfFilePath, String htmlFilePath, String fontPath) {
Document document = new Document();
PdfWriter writer = null;
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
try {
fileOutputStream = new FileOutputStream(pdfFilePath);
writer = PdfWriter.getInstance(document, fileOutputStream);
// 设置底部距离60,解决重叠问题
document.setPageSize(PageSize.A4);
document.setMargins(50, 45, 50, 60);
document.setMarginMirroring(false);
document.open();
StringBuffer sb = new StringBuffer();
fileInputStream = new FileInputStream(htmlFilePath);
BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
String readStr = "";
while ((readStr = br.readLine()) != null) {
sb.append(readStr);
}
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(sb.toString().getBytes("Utf-8")), null, Charset.forName("UTF-8"), new MyFontProvider(fontPath));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != document) {
document.close();
}
if (null != writer) {
writer.close();
}
if (null != fileInputStream) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 根据html内容生成pdf
*
* @param pdfFilePath pdf文件存储位置
* @param htmlcontent html内容
* @param fontPath 字体路径
* @throws DocumentException
* @throws IOException
*/
public static void parseHtml2PdfByString(String pdfFilePath, String htmlcontent, String fontPath) {
Document document = new Document();
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
// 设置底部距离60,解决重叠问题
document.setPageSize(PageSize.A4);
document.setMargins(50, 45, 50, 60);
document.setMarginMirroring(false);
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlcontent.getBytes("Utf-8")), null, Charset.forName("UTF-8"), new MyFontProvider(fontPath));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != document) {
document.close();
}
if (null != writer) {
writer.close();
}
}
}
public static void main(String[] args) {
try {
// 本地
String htmlFile = "D:\\1.html";
String pdfFile = "D:\\test2.pdf";
String fontPath = "D:\\simsun.ttf";
String htmlContent = "<html><body style=\"font-size:12.0pt; font-family:宋体\">" + "<h2>Test</h2><p>测试中文Hello World</p></body></html>";
//parseHtml2PdfByString(pdfFile,htmlContent,fontPath);
parseHtml2PdfByFilePath(pdfFile,htmlFile,fontPath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
@Description
*/
public class MyFontProvider extends XMLWorkerFontProvider {
private String fontPath;
public MyFontProvider(String filePath) {
this.fontPath = filePath;
}
@Override
public Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) {
BaseFont bf = null;
try {
bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
}
分享文章:HTML转成pdf
当前链接:https://www.cdcxhl.com/article42/pdpjhc.html
成都网站建设公司_创新互联,为您提供小程序开发、全网营销推广、网站设计、网站改版、网站内链、建站公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联