標籤:style blog http color 使用 width
1 public class PdfHelper 2 { 3 4 static string RootPath 5 { 6 get 7 { 8 string AppPath = ""; 9 HttpContext HttpCurrent = HttpContext.Current;10 if (HttpCurrent != null)11 {12 AppPath = HttpCurrent.Server.MapPath("~");13 }14 15 return AppPath.Trim(new char[]{‘\\‘});16 }17 }18 19 20 public static void GetPdf(HttpResponseBase response,string pdfUrl)21 {22 string fileName = Guid.NewGuid().ToString("N") + ".pdf";23 string filePath = RootPath + "\\pdfs\\" + fileName;24 string exePath=RootPath + "\\wkhtmltopdf\\wkhtmltopdf.exe";25 string args=pdfUrl + " --zoom 1.25 \"" + filePath+"\"";26 27 28 Process p =new Process();29 p.StartInfo.FileName = exePath;30 p.StartInfo.Arguments = args;31 p.StartInfo.UseShellExecute = false;32 p.StartInfo.RedirectStandardInput = true;33 p.StartInfo.RedirectStandardOutput = true;34 p.StartInfo.RedirectStandardError = true;35 p.StartInfo.CreateNoWindow = false;36 37 try38 {39 p.Start();40 p.WaitForExit(); 41 p.StandardOutput.ReadToEnd();42 p.Close();43 44 FileStream fs = new FileStream(filePath, FileMode.Open);45 byte[] file = new byte[fs.Length];46 fs.Read(file, 0, file.Length);47 fs.Close();48 49 50 response.Clear();51 response.AddHeader("content-disposition", "attachment; filename=" + fileName);52 response.ContentType = "application/octet-stream";53 response.BinaryWrite(file);54 response.Flush();55 56 }57 catch58 {59 60 }61 62 }63 }
對於要列印成pdf的html要求:
1. dom元素的 height/width/margin/padding以及定位,盡量使用百分比的形式。
2. 圖片的像素品質盡量高一些,因為產生pdf的過程會有縮放。另外加入html的時候設定顯示的高度跟寬度。
3. 對於pdf的分頁,可以直接使用一些屬性來實現:
<header page-break-before="always"></header>
{page-break-before:always;}//添加到header的dom元素實現換頁
<footer page-break-after="always"></footer>
{page-break-after:always;} //添加到footer的dom元素實現換頁
{page-break-inside:avoid;}