【问题标题】:Error in converting HTML with images to PDF using itextsharp使用 itextsharp 将带图像的 HTML 转换为 PDF 时出错
【发布时间】:2013-04-17 09:11:34
【问题描述】:

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

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 文档中,我得到的错误是:

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

【问题讨论】:

    标签: asp.net-mvc itextsharp


    【解决方案1】:

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

    //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);
    

    【讨论】:

    • 在这个应用程序中允许用户上传图片。路径可以是任何东西。我想要的是将 HTML 文件(可以包含图像、表格、表单字段等)转换为 PDF 文档。如果用户创建的文件仅包含文本而不包含任何图像,则上面的代码可以正常工作。请为此提供解决方案
    • 如果用户可以上传图片,那么您只需将图片存储在服务器上的文件夹中,并将上述代码指向该文件夹即可。
    • 我按照你说的做了,但仍然收到错误为“找不到路径的一部分 'C:\Documents and Settings\shubham\My Documents\visdatemplatemanger\PDFimages\rectangle-shape. .png'。我允许用户包含在 PDF 中的所有图像都存储在 C:\Documents and Settings\shubham\My Documents\visdatemplatemanger\PDFimages\
    • 那么您的 HTML 是否指向 /PDFimages/rectangle-shape.png?如果是这样,您需要将img_baseurl 指向一个文件夹。
    • 嗨,克里斯,当我做 props["img_baseurl"] = "C:\Source\NinjaWeb\Development\Source.Reporting\Ninja.Web\Content\Images";该程序将路径为 C:\Source\NinjaWeb\Development\Source.Reporting\Ninja.Web\Content\Images\Content\Images 如果我只执行“C:\Source\NinjaWeb\Development\Source.Reporting\Ninja. Web”它给出 NullRef.Exception。解决办法是什么?
    猜你喜欢
    • 2013-11-08
    • 2022-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多