欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

itext html 生成pdf

程序员文章站 2024-03-24 11:42:46
...
String urlPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath() +"/ssl/test.htm";
			 URL url = new URL(urlPath);  
	         URLConnection rulConnection = url.openConnection();  
	         HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection; 
			
			 // step 1
	        Document document = new Document();
	        // step 2
	        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/TEST.pdf"));
	        // step 3
	        document.open();
	        // step 4
	        XMLWorkerHelper.getInstance().parseXHtml(writer, document,httpUrlConnection.getInputStream(), Charset.forName("UTF-8"));
	        // step 5
	        document.close();

 

Maven:

<dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>itext-xtra</artifactId>
		  <version>5.5.3</version>
		</dependency>
		<dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>itextpdf</artifactId>
		  <version>5.5.3</version>
		</dependency>
		<dependency>
		  <groupId>com.itext</groupId>
		  <artifactId>itext-pdfa</artifactId>
		  <version>5.5.3</version>
		</dependency>
		<dependency>
		  <groupId>com.itext</groupId>
		  <artifactId>itext-asian</artifactId>
		  <version>5.1.1</version>
		</dependency>
		<dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>xmlworker</artifactId>
		  <version>5.5.3</version>
		</dependency>