Java代码网页转pdf java将html转pdf

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);
ArrayListArrayList p = HTMLWorker.parseToList(dome_2(htmlCode), st);
for(int k=0;kp.size();k){
for(int m=0;mp.get(k).size();m){
Paragraph pCode = new Paragraph(p.get(k).get(m).toString(),FontChinese);
document.add(pCode);
【Java代码网页转pdf java将html转pdf】}
}
}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;
}
其实此处和别人的最大的不一样的是,
ArrayListArrayList 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怎样将网页上面的数据以pdf的格式导出,求代码需要用到一个pdf的jar包 , 去网上下载一个itextpdf.jar 。导出pdf的格式都是需要自己用代码实现的 , 每一行,每一个列是什么格式,都需要自己写出来,是不能够自动生成的,反正很麻烦 。我做的一个导出成pdf特定格式的东西,写了好几千行代码 。相当头痛 。
package com.dw.mqs.export;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dw.file.WebHelper;
import com.dw.mqs.MqsManager;
import com.dw.mqs.MqsUtilNew;
import com.dw.mqs.ProductItem;
import com.dw.mqs.ProjectBasicItem;
import com.dw.mqs.ProjectConfDetailItem;
import com.dw.mqs.ProjectConfVerItem;
import com.dw.mqs.ProjectConfig;
import com.dw.mqs.ProjectService;
import com.dw.mqs.Util;
import com.dw.system.Convert;
import com.dw.system.gdb.DBResult;
import com.dw.system.gdb.DataRow;
import com.dw.system.gdb.GDB;
import com.dw.user.User;
import com.dw.user.UserManager;
import com.dw.user.UserProfile;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PdfService
{
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter pdf = PdfWriter.getInstance(document, os);
Rectangle rect = new Rectangle(36, 54, 559, 788);
pdf.setBoxSize("art", rect);
pdf.setPageEvent(new TableHeader());
document.open();
BaseFont baseArialuni = BaseFont.createFont("res/ARIALUNI.TTF",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // Arial
// unicode字体
Font fontCN = new Font(baseArialuni, 9, Font.NORMAL, Color.BLACK);
Font fontCN8b = new Font(baseArialuni, 9, Font.BOLD, Color.BLACK);
Font fontCN12b = new Font(baseArialuni, 12, Font.BOLD, Color.BLACK);
Font fontCN9b = new Font(baseArialuni, 10, Font.BOLD, Color.BLACK);
Font fontCN9 = new Font(baseArialuni, 10, Font.NORMAL, Color.BLACK);
Font fontCN9b_blue = new Font(baseArialuni, 10, Font.BOLD, Color.BLUE);
PdfPTable table = null;
PdfPCell cell = null;
table = new PdfPTable(relativeWidths);
table.setWidthPercentage(100);
cell = new PdfPCell(new Paragraph(tit"标题", fontCN12b));
cell.setColspan(root ? 12 : 10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 垂直居中
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
}
怎样用Java把html转为pdfjava中利用第三方jar包iText 5.4.2就可以实现html转为pdf 。
比如有如下index.html文件:
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""
html
head
titleHTML to PDF/title
link href="https://www.04ip.com/post/style.css" rel="stylesheet" type="text/css" /
/head
body
h1HTML 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
tdiText - Java HTML to PDF/td
/tr
tr
thURL/th
td;/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 怎样将当前的页面保存为pdf可以用下struts2框架,它的配置文件里通过添加一个属性就可以把网页转成pdf的
application/pdf;
charset=gb2312
pdfStream
attachment;
filename="contract.pdf"
2048
关于Java代码网页转pdf和java将html转pdf的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读