java URL转PDF文件

1、java将URL网页博客转化为pdf文件


2、测试博客网页地址为:http://blog.csdn.net/u014520797/article/details/50944998


3、测试结果为




4、工程代码结构为:



5、部分代码展示:

public static String[] extractBlogInfo(String blogURL) throws Exception {
		String[] info = new String[4];
		//报错:Exception in thread "main" org.jsoup.HttpStatusException:HTTP error fetching URL. Status=403, URL=http://blog.csdn.net/u014520797/article/details/50944998/
//		org.jsoup.nodes.Document doc = Jsoup.connect(blogURL).get();
//		爬取某个网站太快,会被封。于是要模拟像人一样的取爬取某个网站,那样的话估计几秒爬取一个网页
//		参考http://blog.sina.com.cn/s/blog_664fdc7e0102vesz.html
		org.jsoup.nodes.Document doc = Jsoup.connect(blogURL).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").timeout(10000).get();
		org.jsoup.nodes.Element e_title = doc.select("span.link_title").first();
		info[0] = e_title.text();

		org.jsoup.nodes.Element category_r = doc.select("div.category_r").first();
		info[1] = category_r.after("label").after("span").text().replace("作者同类文章X", "");

		org.jsoup.nodes.Element e_date = doc.select("span.link_postdate").first();
		info[2] = e_date.text();
		org.jsoup.nodes.Element entry = doc.select("div.article_content").first();
		info[3] = formatContentTag(entry);
		info[3]="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"  
				+"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"  
				+"<html xmlns=\"http://www.w3.org/1999/xhtml\">  "
				+"<head>  "
				+"<style>  "
				+"body{  "
				+"font-family:SimSun;  "
				+"font-size:14px;  "
				+"}  "
				+"</style>  "
				+"<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"></meta></head><body>"+info[3]+"</body></html>";
      
			System.out.println("info.toString():"+info[0]+",\n"+info[1]+",\n"+info[2]+",\n"+info[3]+",\n");
		return info;
	}


6、不能使用org.jsoup.nodes.Document doc = Jsoup.connect(blogURL).get();,因为爬取某个网站太快,会被封。于是要模拟像人一样的取爬取某个网站,那样的话估计几秒爬取一个网页。


7、需要在网页部分添加,避免无法显示中文。

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
				<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">  
				<html xmlns=\"http://www.w3.org/1999/xhtml\"> 
				<head> 
				<style> 
				body{  
				font-family:SimSun;  
				font-size:14px;  
				}  
				</style>  
				<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"></meta></head><body>"+info[3]</body></html>


8、代码下载地址:http://download.csdn.net/detail/u014520797/9469285


要将Java中的URL换为PDF,可以使用iText库。首先,您需要使用JavaURL类来打开并读取URL中的内容。然后,您可以使用iText库中的PdfWriter类将URL内容写入PDF文件中。以下是一个简单的示例代码: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class URLtoPDFConverter { public static void main(String[] args) { try { // 创建一个Document对象 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 读取URL内容并添加到PDF文档中 URL url = new URL("http://www.example.com/content"); InputStream is = url.openStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { document.add(new Paragraph(new String(buffer, 0, bytesRead))); } document.close(); is.close(); System.out.println("PDF文件已生成"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们首先创建一个Document对象,然后使用PdfWriter将其实例化,并指定输出的PDF文件名。接着,我们使用URL类打开和读取指定URL的内容,并将其添加到PDF文档中。最后,我们关闭文档并打印出一个成功的消息。 使用iText库能够很方便地实现将Java中的URL换为PDF文件,同时也能够对生成的PDF文件进行更多的定制和处理。希望对您有所帮助!
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值