C# 生成pdf文件客户端下载

itextsharp.dll 下载:http://sourceforge.net/projects/itextsharp/

程序需引用:itextsharp.dll,itextsharp.pdfa.dll,PresentationFramework.dll

本人使用的是一般处理程序来写的,废话不多说代码才是硬道理,使用插件定位图片,表格是使用html转的pdf

 1         public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.Clear();
 4             context.Response.AddHeader("content-disposition", "attachment;filename=报价单.pdf");
 5             context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
 6             context.Response.ContentType = "application/pdf";
 7             //文件临时存储路径
 8             string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "/PDF/" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
 9             BaoJiaHtml(filePath);
10             //读取已建好的文件
11             FileStream fs = new FileStream(filePath, FileMode.Open);
12             while (true)
13             {
14                 byte[] buffer = new byte[fs.Length];
15                 //将文件读取成byte字节
16                 int len = fs.Read(buffer, 0, (int)fs.Length);
17                 if (len == 0) break;
18                 if (len == 1024)
19                 {
20                     context.Response.BinaryWrite(buffer);
21                     break;
22                 }
23                 else
24                 {
25                     //读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
26                     byte[] b = new byte[len];
27                     for (int i = 0; i < len; i++)
28                     {
29                         b[i] = buffer[i];
30                     }
31                     context.Response.BinaryWrite(b);
32                     break;
33                 }
34             }
35             fs.Flush();
36             fs.Close();
37             //删除临时文件
38             if (File.Exists(filePath))
39             {
40                 File.Delete(filePath);
41             }
42             context.Response.End();
43         }
 1         #region  html转pdf zhy + void BaoJiaHtml(string filePath)
 2         /// <summary>
 3         /// html转pdf
 4         /// </summary>
 5         /// <param name="filePath">文件存储路径</param>
 6         public static void BaoJiaHtml(string filePath)
 7         {
 8             string imagePath = System.AppDomain.CurrentDomain.BaseDirectory + @"/Content/IMG/BaoJiaDan/yinz.png";
 9             //注册字体(pdf上显示中文必须注册字体)
10             FontFactory.RegisterFamily("宋体", "simsun", @"c:\windows\fonts\SIMSUN.TTC,0");
11             // FontFactory.RegisterFamily("宋体", "simsun bold", @"c:\windows\fonts\SIMSUN_bold.TTC");
12             //获得商品
13             List<商品列表类> product = GetProducts();
14             //获得已拼接好的
15             StringBuilder sb = PdfHtml(product);
16             Document document = new Document();
17             //设置页面大小是A4纸大小
18             document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
19             //创建文档
20             PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
21             document.Open();
22
23             HTMLWorker html = new HTMLWorker(document);
24             //将html转为pdf
25             html.Parse(new StringReader(sb.ToString()));
26
27             //添加图片
28             iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
29             //计算图片y轴的位置
30             int imgY = CountYCoordinate(product.Count);
31
32             //设置图片的X,Y轴
33             img.SetAbsolutePosition(400, imgY);
34             //将图片添加到文档中
35             document.Add(img);
36             document.Close();
37         }
38         #endregion
 1  #region 根据商品数量设置图标位置 zhy + int CountYCoordinate(int productCount)
 2         /// <summary>
 3         /// 根据商品数量设置图标位置
 4         /// </summary>
 5         /// <param name="productCount">商品数量</param>
 6         /// <returns>公司公章图片的Y轴坐标</returns>
 7         public static int CountYCoordinate(int productCount)
 8         {
 9             int imgY = 230;
10             //只有一页商品
11             if (productCount > 1 && productCount < 10)
12             {
13                 imgY = imgY - (productCount - 1) * 20;
14             }
15             //公章位置固定在顶部(一满页有22件商品)
16             else if ((productCount >= 10 && productCount <= 12) || ((productCount - 12) % 22 == 19) || ((productCount - 12) % 22 == 20) || ((productCount - 12) % 22 == 21) || ((productCount - 12) % 22 == 0))
17             {
18                 imgY = 475;
19             }
20             //商品数量超过12件并不满足固定公章位置的条件,计算y轴坐标
21             else if (productCount > 12)
22             {
23                 imgY = 475;
24                 //商品数量减去第一页的半页商品数量在余满页商品数量计算Y轴坐标
25                 imgY = imgY - ((productCount - 12) % 22) * 22;
26             }
27             return imgY;
28         }
29         #endregion

由于html转pdf有很多css属性在转换时都不支持所以使用纯表格进行布局,支持的属性此博客写的比较全,大家可以看看 http://blog.163.com/wangsongtao_82/blog/static/54480071201351921328227/

  1  #region 拼接html内容 zhy + StringBuilder PdfHtml(List<us_ShoppingTrolley> products, int pagecount)
  2         /// <summary>
  3         /// 拼接html内容
  4         /// </summary>
  5         /// <param name="products">购物车商品列表</param>=
  6         /// <returns></returns>
  7         public static StringBuilder PdfHtml(List<us_ShoppingTrolley> products)
  8         {
  9             StringBuilder sb = new StringBuilder();
 10             //标题
 11             sb.Append("<table border=\"0\" width=\"80%\" style=\"margin:0 auto;\"><tr>");
 12             sb.Append("<td><img src=\"" + System.AppDomain.CurrentDomain.BaseDirectory + "/Content/IMG/BaoJiaDan/logo.jpg\" width=\"150\" height=\"60\" /></td>");
 13             sb.Append("<td style=\"font-family: 宋体; font-size: 18pt; height: 50px; line-height: 22px;\" encoding=\"Identity-H\" >PDF生成</td></tr></table>");
 14             //中间部分的表格
 15             sb.Append("<table border=\"0.5\" width=\"100%\" style=\"margin:0 auto;\"><tr>");
 16             sb.Append("<td width=\"100%\" height=\"0.1px\" bgcolor=\"rgb(0,0,0)\"></td></tr></table>");
 17             sb.Append("<table border=\"0\" width=\"100%\" style=\"float:left;\">"); 19             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11:222</td>");
 20             sb.Append("<td style=\"font-family: 宋体; font-size: 18pt;width:45%; \" encoding=\"Identity-H\">12354348</td></tr>");
 21             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11:0000</td>");
 22             sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">25487587</td></tr>");
 23             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">1252:1244</td>");
 24             sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">电话:400-855-3618</td></tr>");
 25             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;\" encoding=\"Identity-H\">11111:23254235325</td>");
 26             sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">网址:www.dagou100.com</td></tr>");
 27             sb.Append("<tr><td><table border=\"0.5\" width=\"80%\"><tr><td style=\"font-family: 宋体; font-size: 14pt;width:45%;line-height:20px;height:20px; \" encoding=\"Identity-H\">123452:</td><td colspan=\"3\" style=\"font-family: 宋体; font-size: 12pt;width:45%;line-height:20px;height:20px; \" encoding=\"Identity-H\">" + Guid.NewGuid().ToString() + "</td></tr></table></td>");
 28             sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;width:45%; \" encoding=\"Identity-H\">12452:122527</td></tr></table>");
 29             //商品表格
 30             //表格标题
 31             sb.Append("<table width=\"100%\" border=\"0.5\" style=\"margin:0 auto;\">");
 32             sb.Append("<tr><th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">序号</th>");
 33             sb.Append("<th colspan=\"2\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">商品编号</th>");
 34             sb.Append("<th colspan=\"10\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">商品名称</th>");
 35             sb.Append("<th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">货期</th>");
 36             sb.Append("<th colspan=\"2\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">价格</th>");
 37             sb.Append("<th style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">数量</th>");
 38             sb.Append("<th colspan=\"3\" style=\"font-family: 宋体; font-size: 14pt;text-align:center\" encoding=\"Identity-H\">小计</th></tr>");
 39             //表格内容
 40             int productcount = 0;
 41             int no_price = 0;
 42             decimal productPriceCount = 0;
 43             if (products != null && products.Count > 0)
 44             {
 45                 foreach (商品列表类 item in products)
 46                 {
 47                     productcount++; 52                     sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + productcount + "</td>");
 53                     sb.Append("<td colspan=\"2\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + item.productId + "</td>");
 54                     sb.Append("<td colspan=\"10\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">" + item.productName + "</td>");
 55                     sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">暂无</td>");
 56                     sb.Append("<td colspan=\"2\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">12457</td>");
 57                     sb.Append("<td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">1</th>");
 58                     sb.Append("<td colspan=\"3\" style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">12457</td></tr>"); 60                 }
 61                 sb.Append("</table>");
 62                 int count = products.Count - 12;
 63                 //判断商品数量插入空白展位行方便定位公司公章  (由于我的公章必须与商品总计在一起所以图片需要灵活定位,也方便分页时定位)
 64                 if (((count % 22 == 19)) && products.Count != 12)
 65                 {
 66                     sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
 67                     sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr>");
 68                     sb.Append("</table>");
 69                 }
 70                 else if (products.Count == 10 || (count % 22 == 20))
 71                 {
 72                     sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
 73                     sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr><tr><td style=\"width:100px;height:100px;\">.</td></tr>");
 74                     sb.Append("</table>");
 75                 }
 76                 else if (products.Count == 11 || (count % 22 == 21))
 77                 {
 78                     sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">");
 79                     sb.Append("<tr><td style=\"width:100px;height:100px;\">.</td></tr></tr>");
 80                     sb.Append("</table>");
 81                 }
 82             }
 83             else
 84             {
 85                 sb.Append("</table>");
 86             }
 87             //底部
 88             sb.Append("<table width=\"100%\" border=\"0\" style=\"margin:0 auto;\">"); 92             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 14pt;text-align:right\" encoding=\"Identity-H\">商品总计:来电咨询</td></tr>"); 98             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:right\" encoding=\"Identity-H\">注:以上金额含17%的增值税,部分商品不含运费</td></tr>");
 99             sb.Append("<tr><td></td></tr>");
100             sb.Append("<tr><td></td></tr>");
101             sb.Append("<tr><td style=\"font-family: 宋体; font-size: 12pt;text-align:center\" encoding=\"Identity-H\">※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※</td></tr>");
102             sb.Append("</table>");
103             return sb;
104         }
105         #endregion

此数是获取商品列表代码就不贴了,同志们灵活替换吧

1         #region 获取购物车商品列表 zhy +  List<商品列表类> GetProducts()
2         /// <summary>
3         /// 获取购物车商品列表
4         /// </summary>
5         /// <returns>List<us_ShoppingTrolley></returns>
6         public static List<商品列表类> GetProducts()
7         {                 return new List<商品列表类>();          }

ok,帮助到大家的话请给点个推荐吧,自己写的哦

时间: 2024-08-04 02:08:27

C# 生成pdf文件客户端下载的相关文章

.NET生成PDF文件

C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strContent = "ddd3232342434d"; new PDFGenerator.PDFGenerator(path, strContent).Create(); 下载地址: http://pan.baidu.com/s/1kTIchev?

ThinkPHP3.2.3扩展之生成PDF文件(MPDF)

目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendor('mpdf.mpdf'); //设置中文编码 $mpdf=new \mPDF('zh-cn','A4', 0, '宋体', 0, 0); //html内容 $html='<h1><a name="top"></a>一个PDF文件</h1>

MFC使用Haru free pdf lib生成pdf文件

MFC使用Haru free pdf lib生成pdf文件 我们先创建一个项目MFCPDFDemo,在上面添加一个编辑框和一个按钮: haru free pdf类库下载地址 这里直接下载dll,如果想以静态库的方式使用,得下载源码自己编译.然后把这个dll添加到项目中,怎么添加就不说了吧,还是说一下吧,万一有新手看不懂呢.解压后我们要用到三个东西:libhpdf.dlllibhpdf.libinclude整个文件夹解压后复制libhpdf.dll到项目的release和debug目录下:libh

怎么用PHP在HTML中生成PDF文件

原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PHP并借助FPDF来生成PDF文件.但是如今,已经有很多函数库可以使用了,并且能够从你提供的HTML文件生成PDF文档.这让原先耗时的工作变得非常简单了. FPDF是很早就被使用的,其特点如下: FPDF FPDF是一个允许使用纯PHP生成PDF文档的PHP类,换句话说,没有使用PDFlib 函数库.

史上最强php生成pdf文件,html转pdf文件方法

之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有达到我要的求. pdflib,FPDF 这两个方法是需要编写程序去生成pdf的,就也是讲不支持直接把html页面转换成pdf:html2pdf这个虽然可以把html页面转换成pdf文 件,但是它只能转换一般简单的html代码,如果你的html内容要的是通过后台新闻编辑器排版的那肯定不行的. 纠结了半

使用flying-saucer,利用HTML来生成PDF文件(裴东辉)

1.导入maven依赖 <flyingSaucer.version>9.1.0</flyingSaucer.version> <!-- flying-saucer --> <dependency>    <groupId>org.xhtmlrenderer</groupId>     <artifactId>flying-saucer-pdf</artifactId>     <version>${

[轉載]史上最强php生成pdf文件,html转pdf文件方法

之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有达到我要的求. pdflib,FPDF 这两个方法是需要编写程序去生成pdf的,就也是讲不支持直接把html页面转换成pdf:html2pdf这个虽然可以把html页面转换成pdf文 件,但是它只能转换一般简单的html代码,如果你的html内容要的是通过后台新闻编辑器排版的那肯定不行的. 纠结了半

Freemarker+IText生成pdf文件

最近项目中遇到要用html生成pdf文件的需求,一开始研究了下前端插件jspdf,使用h5 canvas绘图生成图片,再把图片生成pdf文件,遇到了各种各样的问题,生成的pdf文件达到20多M,height超过5000浏览器就崩溃,有兴趣的童鞋可以尝试一下,该方案LZ最终放弃了. 接着开始尝试服务端生成,使用freemaker模板生成静态html文件,通过iext生成pdf,网上很多关于renderer.setDocument(dom,null)的用法,LZ尝试后发现效率奇低,最后放弃了,直接使

在C#.NET中,如何生成PDF文件?主要有以下几个途径

1.使用.NET文件流技术:若通过.NET的文件流技术生成PDF文件,必须对PDF文件的语法很清楚,例如BT表示实体内容开始:ET表示实体内容结束:TD表示换行等等.我们可以从Adobe的官方网站上下载PDF的语法说明文档,但文档几乎有1000页.如果熟悉好语法以后再编程,花费的时间肯定很长,而且生成的PDF文档不一定能满足要求.有关C# 代码参见附件.有关PDF文件的语法详见:http://partners.adobe.com/public/developer/en/pdf/PDFRefere