2012-11-27 73 views
5

轉換一個HTML到PDF。由於使用LinkedResourcesAlternateView中通過電子郵件發送相同的HTML,因此此方法是首選。iTextSharp的HTML到PDF圖片src

foreach (var a in attachments) 
{ 
    //not production code 
    html += string.Format("<img src=\"cid:{0}\"></img>", a.Id.ToString()); 
} 

然而,當被生成的PDF,是沒有辦法的圖片ID與img HTML標籤的src部分聯繫起來。 最終,pdf包含頂部的所有圖像,然後忽略<img src...的HTML。

我已經使用如第或ImageAbsolutePosition閱讀幾種可能的解決方案,但他們似乎並不格格不入。

回答

1

我發現之前,有一個問題,當你使用相對路徑iTextSharp的HTML PDF一代你提到你可以使用這需要您使用段落正確,或者如果你仍然想使用HTML定位您的圖像ImageAbsolutePosition 你將不得不給直接路徑類似

​​
1

,或者你可以將虛擬路徑到Server.MapPath的物理路徑是這樣的:

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

試試這個方法:

  PdfWriter writer = PdfWriter.GetInstance(document, msOutput); 
      document.Open(); 
      HTMLWorker worker = new HTMLWorker(document); 

      Dictionary<string, object> providers = new Dictionary<string, object>(); 
      //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(); 
2

this site,看起來像這樣可以工作。

編輯:

下面是引用的網站

它一直在與iTextSharp的和HTMLWorker類呈現一個HTML頁面PDF人民的代碼和文本知道我是什麼談論:如果HTML包含具有相對路徑的圖像,您可能會獲得「友好」的黃色屏幕!

enter image description here

這意味着iTextShap試圖讓一個圖像與相對路徑「圖像/ screenshot.3.jpg」作爲本地文件「C:\影像\ 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<string,string> 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 
} 

它之後,你有渲染之前,這個圖像提供商作爲HTMLWorker類的「img_provider」的接口屬性分配HTML內容:

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

現在,當您呈現HTML時,它應該可以處理相對圖像。

雖然這是ASP.Net的一個例子,但主要思想是如何在呈現HTML時爲iTextSharp創建一個自定義圖像提供程序,並且它可以在任何應用程序上使用,而且,這不僅可以幫助您獲取來自相對位置的圖像,我已經使用它(顯然有更多的代碼)用於從SharePoint或需要驗證的網站獲取圖像。

+0

@kleopatra我已經加入從引用的網站上的代碼和文本。 – Pierre

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

OP明確指出圖像是最終通過XmlWorker運行的HTML的一部分。因此,你的代碼行不匹配。 – mkl