iTextSharp Html到Pdf图像src

使用iTextSharp将html转换为pdf

public static MemoryStream CreatePdfFromHtml( string html, List attachments) { MemoryStream msOutput = new MemoryStream(); using (TextReader reader = new StringReader(html)) using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, msOutput); document.Open(); foreach (var a in attachments) { var image = iTextSharp.text.Image.GetInstance(a.File); document.Add(image); } XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader); writer.CloseStream = false; document.Close(); msOutput.Position = 0; return msOutput; } } 

html以这种方式包含几个嵌入的图像。 此方法是首选,因为使用LinkedResources中的LinkedResources通过电子邮件发送相同的HTML。

 foreach (var a in attachments) { //not production code html += string.Format("", a.Id.ToString()); } 

但是,当生成pdf时,无法将图像ID与img html标记的src部分链接起来。 最终,pdf包含顶部的所有图像,然后是<img src...忽略的HTML。

我已经阅读了使用Paragraphs或ImageAbsolutePosition的几种可能的解决方案,但它们似乎不适合。

试试这种方式:

  PdfWriter writer = PdfWriter.GetInstance(document, msOutput); document.Open(); HTMLWorker worker = new HTMLWorker(document); Dictionary providers = new Dictionary(); //Get url of the application string url = "http://www.url.com/" //url from which images are loaded //Bind image providers for the application providers.Add(HTMLWorker.IMG_BASEURL, url); //Bind the providers to the worker worker.SetProviders(providers); document.Open(); worker.StartDocument(); // Parse the html into the document worker.Parse(reader); worker.EndDocument(); worker.Close(); document.Close(); 

看看这个网站 ,看起来这样可以工作。

编辑:

以下是Referenced Site的代码和文本

使用iTextSharp及其HTMLWorker类将一个HTML页面呈现为PDF的人知道我在说什么:如果HTML包含具有相对路径的图像,您可能会得到“友好”的黄色屏幕!

在此处输入图像描述

这意味着iTextShap尝试使用相对路径“images / screenshot.3.jpg”获取一个图像作为本地文件“C:\ images \ screenshot.3.jpg”,因此,该图像不存在。 在做了大量关于如何向iTextSharp提供正确图像的研究之后,我发现一个人提到了“IImageProvider”界面,它给iTextSharp提供了使用自定义方法查找图像的能力。 好吧,我使用iTextSharp 5.0.2.0做了一个例子。 您可以在这里下载。

首先,你必须创建一个实现IImageProvider接口的类:

 public class CustomItextImageProvider : IImageProvider { #region IImageProvider Members public iTextSharp.text.Image GetImage(string src, Dictionary imageProperties, ChainedProperties cprops, IDocListener doc) { string imageLocation = imageProperties["src"].ToString(); string siteUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, ""); if (siteUrl.EndsWith("/")) siteUrl = siteUrl.Substring(0, siteUrl.LastIndexOf("/")); iTextSharp.text.Image image = null; if (!imageLocation.StartsWith("http:") && !imageLocation.StartsWith("file:") && !imageLocation.StartsWith("https:") && !imageLocation.StartsWith("ftp:")) imageLocation = siteUrl + (imageLocation.StartsWith("/") ? "" : "/") + imageLocation; return iTextSharp.text.Image.GetInstance(imageLocation); } #endregion } 

之后,您必须在呈现HTML内容之前将此图像提供程序指定为HTMLWorker类的“img_provider”接口属性:

 HTMLWorker worker = new HTMLWorker(document); Dictionary interfaceProps = new Dictionary() { {"img_provider", new CustomItextImageProvider()} }; worker.InterfaceProps = interfaceProps; 

现在,当您渲染HTML时,它应该与相对图像一起使用。

虽然这是ASP.Net的一个例子,但主要的想法是如何在呈现HTML时为iTextSharp创建一个自定义图像提供程序,它可以在任何应用程序上使用,这也可以帮助您获取来自相对的图像位置,我已经使用它(显然有更多代码)从SharePoint或需要身份validation的网站获取图像。

或者您可以使用Server.MapPath将虚拟路径转换为物理路径,如下所示:

 imgPhoto.ImageUrl = Server.MapPath(imgPhoto.ImageUrl); 

我之前发现,当您在Itextsharp html pdf生成中使用相对路径时会出现问题,因为您提到可以使用ImageAbsolutePosition,它需要您使用段落来正确定位图像,或者如果您仍然想要使用html,则必须使用给出类似的直接路径

  html += string.Format(""); 
 var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("../Images/mevantage-logo.jpg"));