iTextSharp的HTML到PDF图片src [英] iTextSharp Html to Pdf image src

查看:408
本文介绍了iTextSharp的HTML到PDF图片src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用转换一个HTML到PDF iTextSharp的

Convert a html to pdf using iTextSharp

public static MemoryStream CreatePdfFromHtml(
        string html, List<Attachment> 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包含几个嵌入图像这种方式。是首选这种方法相同的HTML是通过电子邮件在 AlternateView 使用 LinkedResources 发送。

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



然而,当被生成的PDF,是没有办法的图像链接ID与的src IMG HTML标签的一部分。
最终,pdf文档包含的所有图像往上顶,然后用<的HTML; IMG SRC ... 忽略

However, when the pdf gets generated, there is no way to link the image id with the src part of the img html tag. Ultimately, the pdf contains all the images up top and then the HTML with the <img src... ignored.

我已经使用或者段落或ImageAbsolutePosition读了几可能的解决方案,但他们似乎并不适合在

I've read several possible solutions using either Paragraphs or the ImageAbsolutePosition but they don't seem to fit in.

推荐答案

本网站,貌似这样可以正常工作。

Look at this site, looks like this can work.

编辑:

下面是引用的网站的代码和文本

Here is the code and text from the Referenced Site

这一直与iTextSharp的和HTMLWorker类呈现1 HTML页面PDF的人知道我在说什么:如果HTML包含相对路径图像,你可能会得到友好的黄屏!

The people which have been working with iTextSharp and its HTMLWorker class for rendering one HTML page to PDF knows what I'm talking about: if the HTML contains images with relative path you'll probably get the "friendly" yellow screen!

这意味着iTextShap试图得到一个图像与相对路径图像/ screenshot.3.jpg作为本地文件C:\images\ screenshot.3.jpg,所以,该图像不存在。
做了关于如何提供iTextSharp的正确的图像,我发现一个人提到的IImageProvider界面,给予iTextSharp的找到使用自定义的方法图像的能力,大量的研究后。嗯,我已经做了使用iTextSharp的5.0.2.0一个例子。你可以在这里下载。

It means that iTextShap tried to get one image with the relative path "images/screenshot.3.jpg" as the local file "C:\images\screenshot.3.jpg", so, that image doesn't exist. After doing a lot of research about how to provide to iTextSharp the correct image I found that one guy mentioned the "IImageProvider" interface that gives to iTextSharp the ability of find the image using custom methods. Well, I have done one example using iTextSharp 5.0.2.0. you can download it here.

首先,在所有你需要创建一个实现IImageProvider接口一个类:

First at all you have to create one class that implements the IImageProvider Interface:

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
}



之后,你有渲染HTML内容之前,这一形象提供商作为HTMLWorker类的img_provider界面属性分配:

After it, you have to assign this image provider as the "img_provider" interface property of the HTMLWorker class before rendering the HTML Content:

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

现在,当你渲染你的HTML应该相对图像工作。

Now, when you render your HTML it should work with relative images.

Althought这是ASP.Net做了一个例子,其主要思想是如何创建iTextSharp的一个自定义图像提供者在生成HTML,它可以在任何应用程序中使用,也能帮助您不仅从相对位置获取图像,我已经用它(有更多的代码,很明显)获取从SharePoint或需要身份验证的网站的图片。

Althought this is one example made for ASP.Net, the main idea is how to create one custom Image Provider for iTextSharp when rendering HTML and it could be used on any application, also, this could help you not only for getting images from a relative location, I have used it (with more code, obviously) for getting images from SharePoint or Sites that require authentication.

这篇关于iTextSharp的HTML到PDF图片src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆