package test.byd.com.dcc.unittest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpHost;
import org.apache.commons.httpclient.methods.GetMethod;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class TestPdf12 {
private static int _COUNT=0;
private static ITextRenderer reader;
public static void main(String []args) throws Exception{
long time=System.currentTimeMillis();
URL url=new URL("http://localhost:8080/DCC/version/getpdf");
HttpClient client=new HttpClient();
HttpHost host=new HttpHost("localhost");
client.getHostConfiguration().setHost(host);
GetMethod method=new GetMethod(url.toString());
client.executeMethod(method);
//获取响应的html 文本5624 5327
String contents=method.getResponseBodyAsString();
String outputFile = "D:/firstdoc1012.pdf";
String path="WebContent/views/test/pdf/testHttpClient.html";
contents=contents.replace(" ", " ");
//写入文件
writeHTML(contents,path);
createPdf(path,outputFile);
System.out.println(_COUNT);
contents=readerHTML(path);
String str="<span id=\"_PAGE\"></span>";
String pages="<span id=\"_PAGE\">1/"+_COUNT+"</span>";
int number=contents.indexOf(str);
contents= contents.substring(0,number)+pages+contents.substring(number+str.length());
writeHTML(contents,path);
createPdf(path,outputFile);
System.out.println(System.currentTimeMillis()-time);
}
private static void createPdf(String path,String outputFile)throws Exception{
path=new File(path).toURI().toURL().toString();
OutputStream os = new FileOutputStream(outputFile);
//创建pdf处理类
ITextRenderer renderer = getItextRenderer();
// 解决中文支持问题 simsun.ttc
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.setDocument(path);
renderer.layout();
if(_COUNT==0){
List list=renderer.getRootBox().getLayer().getPages();
list.get(0);
System.out.println(list.size());
_COUNT=list.size();
}else if(_COUNT==-1){
PdfReader pdfreader=new PdfReader(outputFile);
_COUNT=pdfreader.getNumberOfPages();
}else{
renderer.createPDF(os);
os.close();
}
}
private static ITextRenderer getItextRenderer(){
if(null==reader){
reader=new ITextRenderer();
}
return reader;
}
/**
* 写文件
* @param contents
* @param path
* @throws MalformedURLException
*/
private static void writeHTML(String contents,String path) {
try {
path=new File(path).toURI().toURL().toString();
FileOutputStream fos = new FileOutputStream(path.substring(path.indexOf("/")));
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8" );
osw.write(contents);
osw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
private static String readerHTML(String path){
StringBuffer contents=new StringBuffer();
try {
path=new File(path).toURI().toURL().toString();
FileInputStream fis = new FileInputStream(path.substring(path.indexOf("/")));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8" );
BufferedReader br = new BufferedReader(isr);
String line = null ;
while ((line = br.readLine()) != null ) {
contents.append(line);
contents.append("\r\n "); // 补上换行符
}
} catch (Exception e) {
e.printStackTrace();
}
return contents.toString();
}
}