我有一个相当简单的HTML页面通过asp.net呈现.通过HtmlRenderer.PdfSharp运行后,它在PDF中看起来很漂亮,除了图像没有出现.只是PDF中缺少图像的红色X,即使网页本身确实正确显示图像.
这是我的HtmlRenderer.PdfSharp代码:
public void BuildPDF( string url, string pdfPath ) { string html = GetHTML(url); Byte[] res = null; using( MemoryStream ms = new MemoryStream() ) { using( FileStream file = new FileStream(pdfPath, FileMode.Create, FileAccess.Write) ) { byte[] bytes = new byte[ms.Length]; var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4); pdf.Save(ms); res = ms.ToArray(); file.Write(res, 0, res.Length); ms.Close(); } } } private string GetHTML(string url) { string html = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.AutomaticDecompression = DecompressionMethods.GZip; using( HttpWebResponse response = (HttpWebResponse)request.GetResponse() ) using( Stream stream = response.GetResponseStream() ) using( StreamReader reader = new StreamReader(stream) ) { html = reader.ReadToEnd(); } return html; }
以下是PDF中不呈现的img HTML:
我怎么解决这个问题?
使用图像的绝对路径.
您可以解析html并在将其传递给pdf转换器之前先进行字符串替换.