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
當前鏈接:http://vcdvsql.cn/article42/pdpjhc.html
成都網站建設公司_創新互聯,為您提供小程序開發、全網營銷推廣、網站設計、網站改版、網站內鏈、建站公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯