【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)
前言:因项目需求,需要将HTML代码转成PDF。大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径。
在百度上找了一个上午,有一点关联的解决方案都没有。最后去谷歌求助,终于找到了。
这是原文:http://www.am22tech.com/html-to-pdf/(可能需要翻墙)
这是我总结后做的一个例子(使用的是第二个解决方法):http://files.cnblogs.com/files/zuochengsi-9/HTML%E8%BD%ACPDF.zip
不懂的也可以参考我的这篇博客:http://www.cnblogs.com/zuochengsi-9/p/5483808.html
------------------------------------------------------------------
正文:
我正在到处寻找完美的例子,可是没有一个能完美的解决我的需求。我的需求非常简单,如下:
创建一个PDF文档从一个HTML页面。这个HTML里的代码包含的了img标签,同时用的相对路径。
我找到了有价值的信息从这几个地方http://kuujinbo.info/iTextSharp/tableWithImageToPdf.aspx
和http://hamang.net/2008/08/14/html-to-pdf-in-net/
最终,可以用下面的asp.net代码解决了我的问题。我希望这也能帮助到你!
必要条件:
Download and copy iTextSharp.dll 我的版本是5.1.1
问题和解决方案:
这个新的ITextSharp库,对于HTML代码转PDF已经做的很好了。可是有个主要的缺陷,图片的URL映射只能是绝对路径。
不然HTMLworlker类就会抛异常,如果你用的相对路径。
这里有两个解决方法对于这个问题:
1、用IImageProvider 接口取出所有的图片从HTML代码中,然后再"paste"PDF中。
但是HTML代码中对img修饰的style,例如height和width都不会保留下来。
2、解析HTML代码,同时用绝对的URL替换相对的URL在写入PDF文件之前。
这个方法会保存HTML代码中对与<img>设置的height和width。当然这个方法更好。
不过我还是提供两种解决方案让你自己去选择。
基本的准备:
添加一个新的page在你的代码中
PostToPDF_AM22.aspx
PostToPDF_AM22.aspx.cs
方法1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; //HTML to PDF 的引用
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.util;
using System.Text.RegularExpressions;
//For converting HTML TO PDF- END public partial class PostToPDF_AM22 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get the HTML code from your database or whereever you have stored it and store
//it in HTMLCode variable.
string HTMLCode = string.Empty;
ConvertHTMLToPDF(HTMLCode);
}
protected void ConvertHTMLToPDF(string HTMLCode)
{
HttpContext context = HttpContext.Current; //Render PlaceHolder to temporary stream
System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringReader reader = new StringReader(HTMLCode); //Create PDF document
Document doc = new Document(PageSize.A4);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~") + "/App_Data/HTMLToPDF.pdf", FileMode.Create));
doc.Open(); /********************************************************************************/
var interfaceProps = new Dictionary<string, Object>();
var ih = new ImageHander() { BaseUri = Request.Url.ToString() }; interfaceProps.Add(HTMLWorker.IMG_PROVIDER, ih); foreach (IElement element in HTMLWorker.ParseToList(
new StringReader(HTMLCode), null))
{
doc.Add(element);
}
doc.Close();
Response.End(); /********************************************************************************/ } //handle Image relative and absolute URL's
public class ImageHander : IImageProvider
{
public string BaseUri;
public iTextSharp.text.Image GetImage(string src,
IDictionary<string, string> h,
ChainedProperties cprops,
IDocListener doc)
{
string imgPath = string.Empty; if (src.ToLower().Contains("http://") == false)
{
imgPath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + src;
}
else
{
imgPath = src;
} return iTextSharp.text.Image.GetInstance(imgPath);
}
}
}
方法2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; //For converting HTML TO PDF- START
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.util;
using System.Text.RegularExpressions;
//For converting HTML TO PDF- END public partial class PostToPDF_AM22 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get the HTML code from your database or whereever you have stored it and store
//it in HTMLCode variable.
string HTMLCode = string.Empty;
ConvertHTMLToPDF(HTMLCode);
}
protected void ConvertHTMLToPDF(string HTMLCode)
{
HttpContext context = HttpContext.Current; //Render PlaceHolder to temporary stream
System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); /********************************************************************************/
//Try adding source strings for each image in content
string tempPostContent = getImage(HTMLCode);
/*********************************************************************************/ StringReader reader = new StringReader(tempPostContent); //Create PDF document
Document doc = new Document(PageSize.A4);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~") + "/App_Data/HTMLToPDF.pdf", FileMode.Create));
doc.Open(); try
{
//Parse Html and dump the result in PDF file
parser.Parse(reader);
}
catch (Exception ex)
{
//Display parser errors in PDF.
Paragraph paragraph = new Paragraph("Error!" + ex.Message);
Chunk text = paragraph.Chunks[] as Chunk;
if (text != null)
{
text.Font.Color = BaseColor.RED;
}
doc.Add(paragraph);
}
finally
{
doc.Close();
}
} public string getImage(string input)
{
if (input == null)
return string.Empty;
string tempInput = input;
string pattern = @"<img(.|\n)+?>";
string src = string.Empty;
HttpContext context = HttpContext.Current; //Change the relative URL's to absolute URL's for an image, if any in the HTML code.
foreach (Match m in Regex.Matches(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.RightToLeft))
{
if (m.Success)
{
string tempM = m.Value;
string pattern1 = "src=[\'|\"](.+?)[\'|\"]";
Regex reImg = new Regex(pattern1, RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match mImg = reImg.Match(m.Value); if (mImg.Success)
{
src = mImg.Value.ToLower().Replace("src=", "").Replace("\"", ""); if (src.ToLower().Contains("http://") == false)
{
//Insert new URL in img tag
src = "src=\"" + context.Request.Url.Scheme + "://" +
context.Request.Url.Authority + src + "\"";
try
{
tempM = tempM.Remove(mImg.Index, mImg.Length);
tempM = tempM.Insert(mImg.Index, src); //insert new url img tag in whole html code
tempInput = tempInput.Remove(m.Index, m.Length);
tempInput = tempInput.Insert(m.Index, tempM);
}
catch (Exception e)
{ }
}
}
}
}
return tempInput;
} string getSrc(string input)
{
string pattern = "src=[\'|\"](.+?)[\'|\"]";
System.Text.RegularExpressions.Regex reImg = new System.Text.RegularExpressions.Regex(pattern,
System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.Match mImg = reImg.Match(input);
if (mImg.Success)
{
return mImg.Value.Replace("src=", "").Replace("\"", ""); ;
} return string.Empty;
}
}
说明:
上面的两种方案,都有一个方法ConvertHTMLToPDF,对于得到的HTML代码的格式是有要求的,具体可以去ITextSharp官网看看。
最后结果存储的一个PDF文档的名字叫HTMLToPDF.pdf在你的web站点的App_Data文件夹里
记得,你需要写代码去拿到HTML代码从你的数据库中或者其他文件里在上面的Page_Load事件中。
通过HTML代码转换函数,它将为您创建PDF文件。
如果你面临任何问题,写在评论中,我会尽力帮助你 。
——————————————————————————————————————
初次翻译,就直接原样翻译了。但通过这次就感觉看英文资料没有以前那种抗拒感了。果然还是有尝试,就会有收获!
【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)的更多相关文章
- [译]Asp.net MVC 之 Contorllers(二)
URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...
- [转][译]ASP.NET MVC 4 移动特性
此教程将讨论ASP.NET MVC 4 Web应用程序里的移动特性.对于此教程,可以使用 Visual Studio Express 2012 或者 Visual Web Developer 2010 ...
- [译]Asp.net MVC 之 Contorllers(一)
Asp.net MVC contorllers 在Ajax全面开花的时代,ASP.NET Web Forms 开始慢慢变得落后.有人说,Ajax已经给了Asp.net致命一击.Ajax使越来越多的控制 ...
- IIS+Asp.Net Mvc必须知道的事(解决启动/重启/自动回收站点后第一次访问慢问题)
问题现象: Asp.net Mvc站点部署在IIS上后,第一个用户第一次访问站点,都会比较慢,确切的说是访问站点的Action页面(即非静态页面,因为静态页面直接由IIS处理返回给用户即完成请求,而A ...
- ASP.NET MVC AJAX 请求中加入 antiforgerytoken 解决“所需的防伪表单字段“__RequestVerificationToken”不存在”问题
在ASP.NET mvc中如果在表中使用了@Html.AntiForgeryToken(),ajax post不会请求成功 解决方法是在ajax中加入__RequestVerificationToke ...
- asp.net mvc 控制器中操作方法重载问题 解决
Controllers: public ActionResult Index() { return View(db.GuestBooks.ToList()); } // // GET: /Guest2 ...
- ASP.Net MVC——使用 ITextSharp 完美解决HTML转PDF(中文也可以)
前言: 最近在做老师交代的一个在线写实验报告的小项目中,有这么个需求:把学生提交的实验报告(HTML形式)直接转成PDF,方便下载和打印. 以前都是直接用rdlc报表实现的,可这次牵扯到图片,并且更为 ...
- [译] ASP.NET MVC 6 attribute routing – the [controller] and [action] tokens
原文:http://www.strathweb.com/2015/01/asp-net-mvc-6-attribute-routing-controller-action-tokens/ 当在Web ...
- [转]ASP.NET MVC Json()处理大数据异常解决方法 json maxjsonlength
本文转自:http://blog.csdn.net/blacksource/article/details/18797055 先对项目做个简单介绍: 整个项目采用微软的ASP.NET MVC3进行开发 ...
随机推荐
- Web安全相关(五):SQL注入(SQL Injection)
简介 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据 ...
- TFS 测试用例步骤数据统计
TFS系统集成了一套BI系统,基于SQL Server的Analysis Service进行实现的.通过这几年的深入使用,能够感触到这个数据数据仓库模型是多么的优秀,和微软官方提供的数据仓库示例Adv ...
- javascript动画系列第一篇——模拟拖拽
× 目录 [1]原理介绍 [2]代码实现 [3]代码优化[4]拖拽冲突[5]IE兼容 前面的话 从本文开始,介绍javascript动画系列.javascript本身是具有原生拖放功能的,但是由于兼容 ...
- spring无法读取properties文件数据
只讲述异常点,关于怎么配置文件,这里不做说明. 1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...
- 【Java每日一题】20170104
20170103问题解析请点击今日问题下方的"[Java每日一题]20170104"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...
- 跟着老男孩教育学Python开发【第五篇】:模块
递归的案例:阶乘 1*2*3*4*5*6*7- def func(num): if num == 1: return 1 return num * func(num - ...
- 豪情-CSS解构系列之-新浪页面解构-02
前言 一.开发工具 - 前端四大利器 1. WebStorm 1). 岂今为止,业界公认的前端开发利器.优点: 2). 缺点 3). 相关资源 4). 后续展望 2. Photoshop 1).基本信 ...
- Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询
问题 你想用LINQ写一个搜索查询,能被转换成更有效率的SQL.另外,你想用EF的CodeFirst方式实现. 解决方案 假设你有如下Figure 13-6所示的模型 Figure 13-6. A s ...
- Spring的前期配置
1创建一个java项目,鼠标单击项目右键新建一个名为lib的文件夹 2在lib文件夹中考入Spring需要的配置文件(俗称jar包) 3 按Shift选中这些jar右键添加至构建路径 4选中src目录 ...
- 严重: Null component localEngine:type=JspMonitor,name=jsp,WebModule=//localhost/SpringMVC01,J2EEApplication=none,J2EEServer=none
检查了 Java Build Path, Java Compiler,Project Facts 都确定了版本一致 包括 maven run as →run config 里面的 jdk 和maven ...