无法使用C#在asp.net中使用iTextSharp API将HTML标记导出为PDF?

我有一个HTML标记,其中包含HTML TableImages 。 我正在使用iTextSharp APIHTML markup to PDF转换HTML markup to PDF 。 但是,遗憾的是iTextSharp无法将包含Images&Table的HTML标记导出为PDF。

错误:找不到网络路径。

结果必须是: 在此处输入图像描述

码:

 using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html; using System.Data.SqlClient; using System.Text; using iTextSharp.text.html.simpleparser; public string strSelectUserListBuilder = @" 

Packing slip

Place this slip inside the box with your device.

ITEM OFFER
iPhone 5 32GB (AT&T) $205.00

Total Offer:  


$215.00

You have until 01/29/2014 to ship your device.

If you send your device after the expiration date we cannot honor your initial offer. We will not accept devices that have been reported lost or stolen.


barcode

'Find my iPhone' must be turned off

This feature locks your device and will delay or reduce payment.
How to deactivate:
1. Tap the “settings” icon on your homescreen.
2. Tap iCloud from the settings menu.
3. If 'Find My iPhone' is on, tap the slider to turn it off.
"; protected void HTMLToPDF() { String htmlText = strSelectUserListBuilder.ToString(); Document document = new Document(); PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "MySamplePDF.pdf", FileMode.Create)); document.Open(); iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document); hw.Parse(new StringReader(htmlText)); document.Close(); } protected void Page_Load(object sender, EventArgs e) { HTMLToPDF(); }

我知道这个错误是由于Image Path。 但是,无法解决。

任何方案?

帮助感谢!

看一下: 使用包含数据库中图像的itextsharp创建pdf

你需要在每个img元素的src属性中提供完整的物理路径(例如 ),或者有可能告诉iTextSharp物理所有图像的基本文件夹。

我能够通过useiung特殊提供商正确呈现我的HTML

 var baseUrl = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority); var html = ...; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentEncoding = Encoding.UTF8; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.pdf", fileName)); Response.ContentType = "application/pdf"; using (var ms = new MemoryStream()) { using (var doc = new Document()) { using (var writer = PdfWriter.GetInstance(doc, ms)) { doc.Open(); var rootProvider = new CustomRootProvider(baseUrl, Request.PhysicalApplicationPath); //Server.MapPath(Request.ApplicationPath) FontFactory.RegisterDirectories(); var htmlContext = new HtmlPipelineContext(null); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); htmlContext.SetImageProvider(rootProvider); htmlContext.SetLinkProvider(rootProvider); htmlContext.CharSet(Encoding.UTF8); var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true); var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer))); var worker = new XMLWorker(pipeline, true); var p = new XMLParser(worker); using(var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html))) { p.Parse(htmlStream, Encoding.UTF8); } writer.CloseStream = false; } doc.Close(); } ms.Position = 0; Response.BinaryWrite(ms.ToArray()); } Response.Flush(); Response.End(); ... public class CustomRootProvider : AbstractImageProvider, ILinkProvider { private string _baseUrl; private string _basePath; public CustomRootProvider(string baseUrl, string basePath) { _baseUrl = baseUrl; } public override Image Retrieve(string src) { var siteUrl = string.IsNullOrEmpty(_baseUrl) ? HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, string.Empty) : _baseUrl; siteUrl = VirtualPathUtility.RemoveTrailingSlash(siteUrl); if (Uri.IsWellFormedUriString(src, UriKind.Relative)) { src = new Uri(new Uri(siteUrl), src).ToString(); } try { return Image.GetInstance(src); } catch (Exception) { return Image.GetInstance(new Uri(new Uri(siteUrl), "/Content/Images/noimage.png").ToString()); } } public override string GetImageRootPath() { return string.IsNullOrEmpty(_basePath) ? HttpContext.Current.Request.PhysicalApplicationPath : _basePath; //HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath); } public string GetLinkRoot() { return string.IsNullOrEmpty(_baseUrl) ? HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, string.Empty) : _baseUrl; } } 

但是在某些主机上有html unicode符号的奇怪问题(在德国机器上确定但在俄语上呈现不正确)。 我试图用iTextSharp fontfactory注册不同的字体,以使用不同的标记(css规则,字体 – 面等)。 什么都没有帮助我。 最后使用wkhtmltopdf和包装器Pechkin (互联网上提供了不同的包装器)。 wkhtmltopdf开箱即用!:

 var config = new GlobalConfig().SetMargins(0, 0, 0, 0).SetPaperSize(PaperKind.A4); var pdfWriter = new SynchronizedPechkin(config); var bytes = pdfWriter.Convert(new Uri(dataUrl)); Response.BinaryWrite(bytes); 

极力推荐!