抓取网页源码,只获得html文本,所以只适用于同一站点
public static string GetResponse(string url, string method, byte[] postData)
{
try
{
Uri uri = new Uri(url);
WebRequest req = HttpWebRequest.Create(uri);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = method;
if (method.ToUpper() == "POST" && postData != null)
{
Stream postStream = req.GetRequestStream();
postStream.Write(postData, 0, postData.Length);
postStream.Close();
}
WebResponse res = req.GetResponse();
string coder = ((HttpWebResponse)res).CharacterSet;
System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(coder);
StreamReader reader = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
string html = reader.ReadToEnd();
reader.Close();
res.Close();
return html;
}
catch(Exception ex)
{
return ex.Message;
}
}
{
try
{
Uri uri = new Uri(url);
WebRequest req = HttpWebRequest.Create(uri);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = method;
if (method.ToUpper() == "POST" && postData != null)
{
Stream postStream = req.GetRequestStream();
postStream.Write(postData, 0, postData.Length);
postStream.Close();
}
WebResponse res = req.GetResponse();
string coder = ((HttpWebResponse)res).CharacterSet;
System.Text.Encoding resEncoding = System.Text.Encoding.GetEncoding(coder);
StreamReader reader = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
string html = reader.ReadToEnd();
reader.Close();
res.Close();
return html;
}
catch(Exception ex)
{
return ex.Message;
}
}
使用ABCpdf生成pdf
public static void SaveAsFile(string htmlContext, string pdfSavePath)
{
using (Doc pdfDoc = new Doc())
{
pdfDoc.Font = pdfDoc.AddFont("宋体","ChineseS");
string tempFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "temp." + DateTime.Now.Millisecond.ToString() + "." + DateTime.Now.ToString("MM-dd-yyyy-hh") + ".html";
System.IO.StreamWriter strWriter = new System.IO.StreamWriter(tempFile,false,System.Text.Encoding.UTF8);
strWriter.Write(htmlContext);
strWriter.Close();
strWriter = null;
int pdfId = 0;
pdfDoc.Rect.String = "30 50 1470 840";
pdfDoc.Color.String = "192,48,117";
try
{
pdfId = pdfDoc.AddImageUrl("file:///" + tempFile);
while (pdfDoc.Chainable(pdfId))
{
pdfDoc.SetInfo(pdfDoc.Page, "/MediaBox:Rect", "0 0 1500 900");
pdfDoc.Page = pdfDoc.AddPage();
pdfId = pdfDoc.AddImageToChain(pdfId);
}
for (int i = 0; i < pdfDoc.PageCount - 1; i++)
{
pdfDoc.PageNumber = i;
pdfDoc.Flatten();
}
pdfDoc.Encryption.CanChange = false;
pdfDoc.Encryption.CanEdit = false;
pdfDoc.Encryption.CanPrint = true;
pdfDoc.Encryption.CanPrintHi = true;
pdfDoc.Encryption.CanAssemble = false;
pdfDoc.Encryption.CanCopy = false;
pdfDoc.Save(pdfSavePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
System.IO.File.Delete(tempFile);
pdfDoc.Delete(pdfId);
pdfDoc.Clear();
}
}
}
{
using (Doc pdfDoc = new Doc())
{
pdfDoc.Font = pdfDoc.AddFont("宋体","ChineseS");
string tempFile = System.Web.HttpContext.Current.Server.MapPath("~/") + "temp." + DateTime.Now.Millisecond.ToString() + "." + DateTime.Now.ToString("MM-dd-yyyy-hh") + ".html";
System.IO.StreamWriter strWriter = new System.IO.StreamWriter(tempFile,false,System.Text.Encoding.UTF8);
strWriter.Write(htmlContext);
strWriter.Close();
strWriter = null;
int pdfId = 0;
pdfDoc.Rect.String = "30 50 1470 840";
pdfDoc.Color.String = "192,48,117";
try
{
pdfId = pdfDoc.AddImageUrl("file:///" + tempFile);
while (pdfDoc.Chainable(pdfId))
{
pdfDoc.SetInfo(pdfDoc.Page, "/MediaBox:Rect", "0 0 1500 900");
pdfDoc.Page = pdfDoc.AddPage();
pdfId = pdfDoc.AddImageToChain(pdfId);
}
for (int i = 0; i < pdfDoc.PageCount - 1; i++)
{
pdfDoc.PageNumber = i;
pdfDoc.Flatten();
}
pdfDoc.Encryption.CanChange = false;
pdfDoc.Encryption.CanEdit = false;
pdfDoc.Encryption.CanPrint = true;
pdfDoc.Encryption.CanPrintHi = true;
pdfDoc.Encryption.CanAssemble = false;
pdfDoc.Encryption.CanCopy = false;
pdfDoc.Save(pdfSavePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
System.IO.File.Delete(tempFile);
pdfDoc.Delete(pdfId);
pdfDoc.Clear();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string strHTML = WebRequestHelper.GetResponse("http://localhost:2275/ABCpdf.Net/test.aspx", "GET", null);
Tools.PdfHelper.SaveAsFile(strHTML, Server.MapPath("2.pdf"));
}
{
string strHTML = WebRequestHelper.GetResponse("http://localhost:2275/ABCpdf.Net/test.aspx", "GET", null);
Tools.PdfHelper.SaveAsFile(strHTML, Server.MapPath("2.pdf"));
}