Ⅰ java怎么把HTML界面做成pdf格式打印
public boolean convertHtmlToPdf(String inputFile, String outputFile)
throws Exception {
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
String url = new File(inputFile).toURI().toURL().toString();
renderer.setDocument(url);
// 解决中文支持问题
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/SIMSUN.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//解决图片的相对路径问题
renderer.getSharedContext().setBaseURL("file:/D:/");
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
return true;
}
上面这段代码是这样的,输入一个HTML地址URL = inputFile,输入一个要输出的地址,就可以在输出的PDF地址中生成这个PDF。
Ⅱ 怎样用Java把html转为pdf
java中利用第三方jar包iText 5.4.2就可以实现转为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!" );
}
}
转换结果:
Ⅲ java 有html转pdf的三方库吗
可以使用iText库来html转pdf
代码:
Document doc = new Document(PageSize.A4);
PdfWriter.getInstance(doc, out);
doc.open();HTMLWorker hw = new HTMLWorker(doc);
hw.parse(new StringReader(html));
doc.close();
Ⅳ java把html转成pdf文件
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
核心代码如下
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使用什么工具可以把html页面转换成pdf文件,要支持中文的。
用 这个jar 包 iText-5.0.6.jar
.........................................................................................
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
public class PaperManager {
public static void buildPaper(IDao , OutputStream outputstream,
long paperid) {
Document document = new Document();
try {
PdfWriter.getInstance(document, outputstream);
document.open();
/** 开始添加内容 * */
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font1 = new Font(bfChinese, 16, Font.BOLD);
Font font2 = new Font(bfChinese, 14, Font.BOLD);
Font font3 = new Font(bfChinese, 12, Font.NORMAL);
Paragraph par = new Paragraph(“fdfd”, font1);
document.add(par);
par = new Paragraph(“中文”, font2);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
document.close();
}
}
}
Ⅶ java中html字符串如何完美转换成pdf文件
1、首先导入itext的包,
然后直接上代码
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>