使用itextsharp将带图像的HTML转换为PDF时出错 [英] Error in converting HTML with images to PDF using itextsharp

查看:264
本文介绍了使用itextsharp将带图像的HTML转换为PDF时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,首先允许用户使用CKEDITOR创建html文档,用户可以在其中创建html文档并可以插入图像,表单字段等。生成的HTML文档将转换为PDF。
如果HTML文档包含纯文本而不是PDF文件成功创建但如果用户在其中插入图像而不是给出错误。
用于创建PDF文档的代码。

In my application first am allowing the user to create html document using CKEDITOR where user can can create html document and can insert image, form fields etc. the generated HTML document is than converted into PDF. If HTML document contains plain text than PDF file gets created successfully but if user inserts image in it than gives error. code for creating PDF document.

public ActionResult CreateFile(FormCollection data)
    {
        var filename = data["filename"];
        var htmlContent = data["content"];
        string sFilePath = Server.MapPath(_createdPDF + filename + ".html");
        htmlContent = htmlContent.Trim();
        if (!System.IO.File.Exists(sFilePath))
        {
            using (FileStream fs = new FileStream(sFilePath, FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.Write(htmlContent);
                }
            }
            createPDF(sFilePath);
        }

        return View();
    }

    private MemoryStream createPDF(string sFilePath)
    {
        string filename = Path.GetFileNameWithoutExtension(sFilePath);
        string name = Server.MapPath(_createdPDF + filename + ".pdf");
        MemoryStream ms = new MemoryStream();
        TextReader tr = new StringReader(sFilePath);
        Document document = new Document(PageSize.A4, 30, 30, 30, 30);
        string urldir = Request.Url.GetLeftPart(UriPartial.Path);
        urldir = urldir.Substring(0, urldir.LastIndexOf("/") + 1);
        Response.Write(urldir);
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name, FileMode.Create));
        document.Open();
        string htmlText = "";
        StreamReader sr;
        sr = System.IO.File.OpenText(sFilePath);
        htmlText = sr.ReadToEnd();
        sr.Close();
        WebClient wc = new WebClient();
        Response.Write(htmlText);
        var props = new Dictionary<string, Object>();
        props["img_baseurl"] = @"C:\Documents and Settings\shubham\My Documents\visdatemplatemanger\visdatemplatemanger\";
        List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null,props);
        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            document.Add((IElement)htmlarraylist[k]);
        }
        document.Close();
        System.IO.File.Delete(sFilePath);
        UploadURL(name);
        return ms;
    }

如果图像包含在HTML文档中,我得到的错误是:

The error that i get if image is included in HTML document is:

Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\PDFimages\rectangle-shape.png'.


推荐答案

iTextSharp将尝试解析基于HTTP的相对图像文档,但从文件系统提供的文档,您需要提供绝对路径或提供搜索的基础。

iTextSharp will try to resolve relative images for HTTP-based documents but ones served from the filesystem you need to either provide absolute paths or provide a base for it to search from.

//Image search base, path will be concatenated directly so make sure it contains a trailing slash
var props = new Dictionary<string, Object>();
props["img_baseurl"] = @"c:\images\";

//Include the props from above
htmlarraylist = HTMLWorker.ParseToList(sr, null, props);

这篇关于使用itextsharp将带图像的HTML转换为PDF时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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