【译】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进行开发 ...
随机推荐
- SDWebImage源码解读 之 UIImage+GIF
第二篇 前言 本篇是和GIF相关的一个UIImage的分类.主要提供了三个方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name ----- 根据名 ...
- 【腾讯Bugly干货分享】Android Linker 与 SO 加壳技术
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57e3a3bc42eb88da6d4be143 作者:王赛 1. 前言 Andr ...
- ASP.NET Core 中间件详解及项目实战
前言 在上篇文章主要介绍了DotNetCore项目状况,本篇文章是我们在开发自己的项目中实际使用的,比较贴合实际应用,算是对中间件的一个深入使用了,不是简单的Hello World,如果你觉得本篇文章 ...
- K-近邻算法(KNN)
简介 k近邻算法是数据分类一种常用的算法,属于监督学习算法的一类,它采用不同特征值之的距离进行分类.K近邻算法具有精度高.对异常值不敏感.无数据输入假定的优点,缺点是计算复杂度高.空间复杂度高.适用于 ...
- 页面元素坐标和偏移(clientX/pageX/screenX/layerX/offsetWidth/scrollWidth/clientWidth等)相关整理
鼠标事件都是在特定位置发生的,我们可以通过event事件对象的各种属性来获得事件发生的坐标位置,有相对于视口的,有相对于整个文档的,同样页面元素的位置也有相对视口的,也有滚动后的,这些都比较容易混淆, ...
- Linux简单指令操作
Linux CentOS运维中,常用的操作和命令记录下: 1.DNS设置 在Linux服务器上,当我们ping出现这个错误时:ping: unknown host,很大可能是系统的DNS没有设置或者设 ...
- Thread.Sleep(0) vs Sleep(1) vs Yeild
本文将要提到的线程及其相关内容,均是指 Windows 操作系统中的线程,不涉及其它操作系统. 文章索引 核心概念 Thread.Yeild Thread.Sleep(0) Thread. ...
- Entity Framework 6 Recipes 2nd Edition(13-2)译 -> 用实体键获取一个单独的实体
问题 不管你用DBFirst,ModelFirst或是CodeFirst的方式,你想用实体键获取一个单独的实体.在本例中,我们用CodeFirst的方式. 解决方案 假设你有一个模型表示一个Paint ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- xamarin android,UWP 网络类型和IP地址
App开发经常要判断网络连通情况,并判断网络类型,获取网络IP.xamarin中可以使用Dependencies提供各平台下的方法,现把各平台代码记录如下: using System; using S ...