2

I am trying to convert an HTML with CSS in to Pdf and it has been successful. But once I tried to add an image to a div from a CSS the Converted Pdf won't show the image

This is my code

For Pdf Content Type

using System; using System.IO; using 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);
                    }
                }
            }

The Action which Invoke the 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;
                        }

The CSS classes

                .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;
                }

This code will generate a clean HTML to Pdf Conversion Except it won't show the image which applied for the div's background in the converted file

1 Answer 1

2

I am also tried to assign image to div via CSS but not working so when I am tried to add img tag with scr attribute inside div tag then it will work for me

try this

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

hope fully it will work for you

Sign up to request clarification or add additional context in comments.

5 Comments

I cannot use inline styles
I am not saying you applied Inline css you just add your background image via img element rest of css write your css file
can u provide html or aspx page?
<img src="violin-clip-art-violin-clip-art-1.jpg" /> Replace with <img scr="www.yourDomineName.com/violin-clip-art-violin-clip-art-1.jpg"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.