【问题标题】:ITextSharp HTML to PDF Conversion CSS Images are not showing after convesionITextSharp HTML 到 PDF 转换 CSS 图像在转换后不显示
【发布时间】:2015-10-26 15:06:24
【问题描述】:

我正在尝试将带有 CSS 的 HTML 转换为 Pdf,并且已经成功。但是,一旦我尝试将图像从 CSS 添加到 div,转换后的 Pdf 将不会显示图像

这是我的代码

对于 Pdf 内容类型

使用系统; 使用 System.IO; 使用 System.Web.Mvc;

            namespace MvcApplication1.Helpers
            {
                public class PdfContent : ActionResult
                {
                    public MemoryStream MemoryStream { get; set; }
                    public string FileName { get; set; }

                    public override void ExecuteResult(ControllerContext context)
                    {
                        if (context == null)
                        {
                            throw new ArgumentNullException("context");
                        }

                        var response = context.HttpContext.Response;
                        response.ContentType = "pdf/application";
                        response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".pdf");
                        response.OutputStream.Write(MemoryStream.GetBuffer(), 0, MemoryStream.GetBuffer().Length);
                    }
                }
            }

调用 Pdf 的动作

  public ActionResult GeneratePdf()
                        {
                            var cssText = System.IO.File.ReadAllText(@"C:\Users\sansa\Desktop\Ground Operations\HTMLResources\grace.css");
                            var htmlText = System.IO.File.ReadAllText(@"C:\Users\sansa\Desktop\Ground Operations\HTMLResources\grace.html");

                            var cssArray = cssText.Trim().Split('}');

                            var cssClassesString = string.Join("} ", cssArray);

                            var memoryStream = new MemoryStream();
                            var document = new Document();
                            var writer = PdfWriter.GetInstance(document, memoryStream);
                            document.Open();

                            using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssClassesString)))
                            {
                                using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText)))
                                {
                                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
                                }
                            }

                            document.Close();

                            var pdfContent = new PdfContent
                            {
                                MemoryStream = memoryStream,
                                FileName = "SomeName"
                            };
                            return pdfContent;
                        }

CSS 类

                .main_div{background-color:#F8F8F8; vertical-align:center; z-index:1000000; border-width:1px; border-style:solid ;border-radius: 2px;border-color: #E0E0E0; width:400px; height:200px}
                .text_one{color: #003300;}
                .text_two{color: #0000ca;}
                .text_three{color: #e40000;}
                .tbl_page{
                    margin-top: 5px;
                    color: black;
                    background-color: cornsilk;
                    display: block;
                    border-style: solid;
                    border-color: wheat;
                    border-width: 1px;;
                    border-radius: 5px;
                    font-family: "Times New Roman", Times, serif;
                    font-size: 12px;
                    white-space: normal;
                    line-height: normal;
                }

                .tbl_cell{
                    background-color: honeydew;
                }

                td, th {
                    display: table-cell;
                }

                .tbr_raw{
                    background-color: #66CCFF;
                    text-align: center;
                }

                .image_div_background{
                    background-image: url('violin-clip-art-violin-clip-art-1.jpg');
                    height: 400px; width: 400px;
                }

此代码将生成一个干净的 HTML 到 Pdf 的转换 除了它不会在转换后的文件中显示申请 div 的背景的图像

【问题讨论】:

    标签: c# css pdf pdf-generation itext


    【解决方案1】:

    我也尝试通过 CSS 将图像分配给 div,但无法正常工作,因此当我尝试在 div 标签内添加带有 scr 属性的 img 标签时,它会为我工作

    试试这个

    <div class="image_div_background">
    <img scr="www.yourDomineName.com/violin-clip-art-violin-clip-art-1.jpg"/>
    <div>
    

    希望它对你有用

    【讨论】:

    • 我不能使用内联样式
    • 我不是说你应用了内联 css 你只是通过 img 元素添加你的背景图片其余 css 写你的 css 文件
    • 可以提供html或aspx页面吗?
    • 替换为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2018-02-14
    • 2015-04-19
    • 1970-01-01
    • 2013-11-08
    • 2013-06-28
    • 1970-01-01
    相关资源
    最近更新 更多