0

I am practicing HTML to PDF conversion.For This Purpose,Firstly I have tried HtmlWorker but it was not taking my CSS. Then I learnt about XMLWorkerHelper thought it will solve the issue. But it also not getting images and the exact design.

Here I am using 2 pictures 1 as background Image and another as person image

    <div class="col-md-4" id="div1" runat="server" style="background-color: White; height: 204px; width: 324px; border: 1px #DBF0EC solid; border-radius: 5%; color: black; padding: 0px; overflow: hidden;">
        <div class="col-md-12" style="margin: 0px; padding: 0px; width: 100%;">
            <div class="col-md-12" style="margin: 0px; padding: 0px; text-align: center; width: 100%;">
                <h4 style="background-color: #169F85; margin: 0px; padding: 2px; border-radius: 5%; color: white; font-size: 16px;">Vikarunnessa School And College</h4>
            </div>
            <div class="col-md-12" style="background-image: url(Images/IdcardDesign.jpg); background-repeat: no-repeat; background-size: 100%">
                <h2 class="text-center" style="margin: 0px; padding: 0px; padding-bottom: 2px; color: Blue; text-align: center">Student Identity Card</h2>
                <div class="col-md-5" style="margin: 0px; padding: 0px;">
                    <img class="img-preview" style="max-height: 100px; width: 100px; margin: 0px; padding: 0px;" src="Images\img.jpg" />
                </div>
                <div class="col-md-7" style="margin: 0px; padding: 0px;">
                    <h6 style="margin: 0px; padding: 0px; padding-top: 5px; letter-spacing: .04em; width: 100%;">
                    Fredrick Joe 
</h6>   
                </div>
            </div>
        </div>
    </div>

This is the code what is generating PDF

 protected void btnCreatePDF_Click(object sender, EventArgs e)
        {
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    div.RenderControl(hw);
                    StringReader sr = new StringReader(sw.ToString());
                    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                    pdfDoc.Open();
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                    pdfDoc.Close();
                    Response.ContentType = "application/pdf";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Write(pdfDoc);
                    Response.End();

                }
            }
        }

PDF content should be as like as the markup. With 2 images and the exact Design.

Priom
  • 23
  • 8
  • The image url is relative. But you don't inform the XML worker about the base url to assume. Thus, how should it know where to find the image? – mkl Aug 04 '19 at 13:04
  • @mkl But After giving full path it is not taking the exact design. – Priom Aug 05 '19 at 06:50

0 Answers0