renderer.createPDF( os );捕捉下異常,看是不是跳走廊,在close之前調用os.flush()試試。
String docPath = session.getAttribute("docpath").toString();//獲取文件HTML文件路徑
String inputFile = docPath+"html"; //定義輸入文件全名
String url = new File(inputFile).toURI().toURL().toString();
String outputFile =docPath + "pdf"; //定義輸出文件全名
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer(); 實例化一個ITextRenderer
renderer.setDocument(url);
ITextFontResolver fontResolver = renderer.getFontResolver();
ontResolver.addFont("C:/Windows/fonts/simsun.ttc",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //設置字體
// 解決圖片的相對路徑問題
renderer.getSharedContext().setBaseURL("file:/" + application.getRealPath("UserFiles/Image") + "/");
renderer.layout();
renderer.createPDF(os);
os.close();
❷ java中html字元串如何完美轉換成pdf文件
1、首先導入itext的jar包,
然後直接上代碼
private static void versionsone(String htmlCode, 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);
ArrayList<ArrayList> p = HTMLWorker.parseToList(dome_2(htmlCode), st);
for(int k=0;k<p.size();k++){
for(int m=0;m<p.get(k).size();m++){
Paragraph pCode = new Paragraph(p.get(k).get(m).toString(),FontChinese);
document.add(pCode);
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
document.close();
}
}
private static Reader dome_2(String htmlCode){
if(htmlCode != null && !htmlCode.trim().equals("")){
try{
InputStream tInputStringStream = new ByteArrayInputStream(htmlCode.getBytes());
InputStreamReader isr = new InputStreamReader(tInputStringStream,"utf-8");
BufferedReader reader = new BufferedReader(isr);
return reader;
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
其實此處和別人的最大的不一樣的是,
ArrayList<ArrayList> p = HTMLWorker.parseToList(dome_2(htmlCode), st);
其實剛剛開始我在網上找來的代碼中是這樣寫的「ArrayList p = HTMLWorker.parseToList(dome_2(htmlCode), st);」
但後來我通過debug發現,ArrayList 裡面裝的還是一個集合,然後集合中放得才是真正不含html標簽的字元串。對於普通得文章,一般html標簽一般都是<p></p>
佔大多數。所以我直接用了兩層for循環直接從裡面取得我們想要的字元串。然後直接「Paragraph pCode = new Paragraph(p.get(k).get(m).toString(),FontChinese);」document.add(pCode);這樣就能解決中文問題了
❸ java中html怎麼轉換為可編輯pdf文件
Java代碼
//step1
Documentdocument=newDocument();
//step2
PdfWriterwriter=PdfWriter.getInstance(document,newFileOutputStream("pdf.pdf"));
//step3
document.open();
//step4
XMLWorkerHelper.getInstance().parseXHtml(writer,document,
newFileInputStream("index.html"));
//step5
document.close();
System.out.println("PDFCreated!");
Maven構建對應的版本
Xml代碼
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.4.1</version>
</dependency>
❹ java將html文件轉成pdf
核心代碼如下
package com.hmkcode;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class App {
public static final String HTML = "<h1>Hello</h1>"
+ "<p>This was created using iText</p>"
+ "<a href='hmkcode.com'>hmkcode.com</a>";
public static void main( String[] args ) throws FileNotFoundException, IOException
{
HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
System.out.println( "PDF Created!" );
}
}
❺ java中,用itext把html轉pdf的時候,html中的一個空格在pdf中顯示有2個空格那麼大
可以先在後台直接將html里的空格替換成pdf中正常的空格再輸出。
❻ 怎樣用Java把html轉為pdf
java中利用第三方jar包iText 5.4.2就可以實現html轉為pdf。
比如有如下index.html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>HTML to PDF</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>HTML to PDF</h1>
<p>
<span class="itext">itext</span> 5.4.2 <span class="description"> converting HTML to PDF</span>
</p>
<table>
<tr>
<th class="label">Title</th>
<td>iText - Java HTML to PDF</td>
</tr>
<tr>
<th>URL</th>
<td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
</tr>
</table>
</body>
</html>
樣式文件:
h1 {
color:#ccc;
}
table tr td{
text-align:center;
border:1px solid gray;
padding:4px;
}
table tr th{
background-color:#84C7FD;
color:#fff;
width:100px;
}
.itext{
color:#84C7FD;
font-weight:bold;
}
.description{
color:gray;
}
後台轉換的介面:
package com.hmkcode;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class App
{
public static void main( String[] args )throws DocumentException, IOException
{
// step 1
Document document =new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("pdf.pdf"));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream("index.html"));
//step 5
document.close();
System.out.println("PDF Created!" );
}
}
轉換結果: