import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.nio.charset.Charset;
/***
* html
* 转pdf文档
*/
public class jsopu {
private static final String HTML_PATH = "D:\\sdf.html";
private static final String PDF_PATH = "d:\\2222222.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File f = new File(HTML_PATH);
//转换html以jsoup格式
org.jsoup.nodes.Document jsdocument = Jsoup.parse(f, "UTF-8");
String html = jsdocument.html();
//个人业务代码开始----------------------------str
//本段落的作用是获取html里面所有标签为table的内容。
//循环当thead 内容为空时 移除该table标签
Elements elements = jsdocument.getElementsByTag("table");
for (Element d:elements){
Element d1 = d.getElementById("romveiss");
if(d!=d1){
if (d.getElementsByTag("thead").text().isEmpty()){
d.remove();
}
}
}
//个人业务代码end------------------------------
System.out.println(jsdocument.html());
File file1 = new File(HTML_PATH);
FileOutputStream fos = new FileOutputStream(file1);
BufferedWriter bw = null;
bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
bw.write(jsdocument.html());
bw.close();
fos.close();
Document document = new Document(PageSize.A4, 20, 20, 20, 20);//左右上下的margin
document.setMargins(20, 20, 20, 20);
PdfWriter pdfwriter;
pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(PDF_PATH));
document.open();
XMLWorkerHelper wh = XMLWorkerHelper.getInstance();
wh.parseXHtml(pdfwriter, document, new FileInputStream(HTML_PATH), Charset.forName("UTF-8"), new DefineFontProvider());
document.close();
System.out.println("succeed");
}
}
最近下载更多
最近浏览更多
datang123
6月3日
暂无贡献等级
Jacko01 LV8
3月20日
liuyinwan
1月14日
暂无贡献等级
源蝈蝈
2024年12月28日
暂无贡献等级
3334004690 LV11
2024年6月24日
19950118
2024年4月19日
暂无贡献等级
zhos0212 LV19
2024年3月29日
467277 LV13
2024年3月26日
studengaaa
2024年3月17日
暂无贡献等级
a1325922140 LV1
2024年2月19日

