Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing an asp.net application where in i need to generate a pdf document of web page Aspx/Html , i was able to generate pdf document but the issue is my stylesheet is not getting applied to this pdf document

please find my effort below

(1) myxyz.css

CSS
body {
    margin:0;
    width: 100%;
    height:100%;

}
.page
{

    width:970px;
    border:1px #E4E3E3 solid;
    display:table;
    height:700px;
    margin:auto;
    font-family:Arial, Helvetica, sans-serif;


}
.header
{
    width:960px;
    margin:auto;
    padding-bottom:15px;
    border-bottom:1px #E4E3E3 solid;
    display:table;
}
.headerLeftPart
{
    padding-left:10px;
    float:left;
}
.headerLeftPart h4
{
 font-size:14pt;

}
.headeraddresspan{
    width:250px;
    word-wrap:break-word;
    display:block;
    text-align:left;
    margin-top: -15px;
    font-size:12pt;
}
.headerRightPart
{
    float:right;
}
.headercontact {

    width:310px;
    margin-top:50px;
    float:right;
}
.headercontactspan {

    width:310px;
    float:right;
    display:block;
}
.footer {

     height:130px;
     width:970px;
     margin:auto;
     padding-top:50px;
     clear:both;
     display:block;

}
.footerleft {
    padding:10px;
    width:350px;
    float:left;
    display:block;

}
.footerleft span
{
    display:block;
    float:left;
    width:350px;
    padding:8px;
    font-weight:bold;
}
.footerright {

    width:350px;
    float:right;
    display:block;

}
.footerright span
{
    display:block;
    float:right;
    width:250px;
    padding:8px;
    font-weight:bold;
}
.content {
    padding:6px;
    padding-left:15px;
    padding-top:15px;
}
.section {

    width:960px;
    margin:auto;
    float:left;
    padding-left:5px;
    padding-right:5px;
    display:block;
}
.sectionhead {
}
.sectionhead h4{
    font-size:14pt;
    font-weight:bold;
    text-decoration:underline;
    display:block;
}
.inlinesectionhead{
    font-size:14pt;
    font-weight:bold;
    text-decoration:underline;
    display:block;
    float:left;

}
.sectioncontent {
    font-size:12pt;
}
.sectioncontentinlinewrap {
    padding-top:15px;

}
.inlinesectionContent {
    font-size:12pt;
    float:left;
    display:block;
    padding-left:15px;
    margin-top: 3px;
    word-wrap:break-word;

}
    .sectioncontent p {

        word-wrap:break-word;
        padding-left:28px;
    }
     .sectioncontent ul {

        padding-left:40px;
    }
    .sectioncontent ul > li {
            word-wrap:break-word;
        }
    .sectiontable {
        width:940px;
        margin:auto;
        border:1px black solid;
    }
     .sectiontable  tr , td , th {
        border: border:1px black solid;;
        border-collapse:separate;
        margin:auto;
        border:1px black solid;
        text-align:center;
    }
.inlinepara {
    text-indent:30px;
    text-align:justify;
     padding-right:30px;
}
.paddresspara {
    padding-left:0px !important;
    margin-top: -14px;
}
.projecthead {
    float:left;
    display:block;
    font-weight:bold;
}
.projectpara {
    padding-left:0px !important;

}

.projectcontent {
    clear:both;
    width:960px;
    padding-top:10px;
    clear:both;
}
.projetcs {
   clear:both;
   width:960px;
   margin-top:-20px;
}
.kutchdialstrip {
    width:960px;
    text-align:center;
    font-size:11px;
    clear:both;
}
/*print settings*/

@page
    {
        size: auto;   /* auto is the initial value */
        margin: 0mm;  /* this affects the margin in the printer settings */
    }

/*print settings ends*/



(2) myxyz.aspx/html

XML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../../css/Jobseeker/resumstyle.css" rel="stylesheet" />
</head>
<body>
        <div class="page">
            <div class="header">
                <div class="headerLeftPart">
                    <h4>PRADEEP CHANDRAN</h4>
                    <span class="headeraddresspan">
                        Qtr No: F18, Aramji Society,
                        Evani Avas , Sector-11,
                        Moran- 370001,
                        ABC,India
                    </span>
                </div>
                <div class="headerRightPart">
                    <div class="headercontact">
                        <span class="headercontactspan">Phone : +91 963834000X </span>
                        <span class="headercontactspan">Email: gpxyzmjguj000582@gmail.com</span>
                    </div>
                </div>

            </div>
            <div class="content">
                <div class="section">
                    <div class="sectionhead"><h4>Objective :</h4></div>
                    <div class="sectioncontent">
                        <p>
                            To have a hardcore experience, enlarge my knowledge, maintain long term relationship with the organization and also to have growth.
                        </p>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"><h4>Profile Summary:</h4></div>
                    <div class="sectioncontent">
                        <ul>
                            <li>I was working with JCPL (Franchisee of NIIT) since December, 2007, as Trainer in Microsoft .Net</li>
                            <li>Nov, 2011, Promoted as Group Leader of NIIT, Simultaneously appointed to work with development team as Microsoft .Net developer for Stringz Solutions a sister concern of JCPL.</li>
                            <li>Jan, 2014, left NIIT and working as Microsoft .Net developer for Stringz Solutions a sister concern of JCPL.</li>

                        </ul>
                        <p class="inlinepara">
                            My technological forte is .NET, Microsoft SQL-Server 2005 and also proficient in writing Procedures, Functions and Database Triggers with day to day database maintenance activity. I am also involved in Client Meeting, Requirement Gathering and Analysis.
                        </p>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"><h4>IT / Computer Skills :</h4></div>
                    <div class="sectioncontent">
                        <table class="sectiontable" cellspacing="0">
                            <tr>
                                <th>Skill</th>
                                <th>Experience</th>
                                <th>Last Used</th>

                            </tr>
                            <tr>
                                <td>HTML - Markup Language</td>
                                <td>2.5 years</td>
                                <td>2010</td>

                            </tr>
                            <tr>
                                <td>HTML - Markup Language</td>
                                <td>2.5 years</td>
                                <td>2010</td>
                            </tr>
                            <tr>
                                <td>HTML - Markup Language</td>
                                <td>2.5 years</td>
                                <td>2010</td>
                            </tr>
                            <tr>
                                <td>HTML - Markup Language</td>
                                <td>2.5 years</td>
                                <td>2010</td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"><h4>Educational Qualification :</h4></div>
                    <div class="sectioncontent">
                       <table class="sectiontable" cellspacing="0">
                           <tr>
                               <th>Qualification</th>
                               <th>University/Board</th>
                               <th>Percentage/Grade</th>
                               <th>Year</th>
                           </tr>
                           <tr>
                               <td>Bachelor of Information Technology </td>
                               <td>GSHEB</td>
                               <td>71.57</td>
                               <td>2005</td>
                           </tr>
                           <tr>
                               <td>Bachelor of Information Technology</td>
                               <td>GSHEB</td>
                               <td>71.57</td>
                               <td>2005</td>
                           </tr>
                           <tr>
                               <td>Bachelor of Information Technology</td>
                               <td>GSHEB</td>
                               <td>71.57</td>
                               <td>2005</td>
                           </tr>
                           <tr>
                               <td>Bachelor of Information Technology</td>
                               <td>GSHEB</td>
                               <td>71.57</td>
                               <td>2005</td>
                           </tr>
                       </table>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"><h4>Achivements :</h4></div>
                    <div class="sectioncontent">
                        <ul>
                            <li>Consolidated four accounting offices into one location, resulting in greater efficiency, streamlined operations and savings of over $500K in payroll alone.</li>
                            <li>Implemented new travel policy with effective controls, resulting in greater efficiency and consistency in travel planning and a $100K reduction in costs.</li>
                            <li>Directed an exhaustive search for new office space, negotiated lease agreements and secured sub-tenants in two locations, resulting in savings of $2.5M.</li>

                        </ul>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"><h4>Representative Projects:</h4> </div>
                    <div class="projetcs">
                        <div class="projectcontent">
                            <span class="projecthead">Systems Engineering:</span>
                            <span class="projectpara">Led design and multinational rollout of robust, scalable and secure electronic data interchange (EDI), enterprise resource planning (ERP) and point-of-sale (POS) systems for clients including AmEx, Citigroup and McDonald’s.</span>
                        </div>
                        <div class="projectcontent">
                            <span class="projecthead">Database Developments:</span>
                            <span class="projectpara">Guided teams in the development of relational database management systems (RDBMS) for clients including Coca-Cola, Caterpillar and Allied Waste Industries.</span>
                        </div>
                        <div class="projectcontent">
                            <span class="projecthead">System Integrations/Migrations:</span>
                            <span class="projectpara">
                                Project-managed large-scale initiatives involving the transition of programs to new platforms and the merger of disparate systems from acquired client companies.
                                Results: Achieved seamless migrations and integrations that were transparent to client customers, accomplished with no unscheduled downtime and delivered by as much as $750K under budget.
                            </span>
                        </div>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"><h4> Permanent Address: </h4></div>
                    <div class="sectioncontent">
                        <p class="paddresspara">
                            Qtr No: F18, Aramjinagar Society,
                            Evani Avas , Sector-11,
                            Mora- 370801,
                            ABC,India
                        </p>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"></div>
                    <div class="sectioncontent sectioncontentinlinewrap">
                        <span class="inlinesectionhead">Current Location</span>
                        <span class="inlinesectionContent">Mora ,Gujarat, India</span>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"></div>
                    <div class="sectioncontent sectioncontentinlinewrap">
                        <span class="inlinesectionhead">Current CTC</span>
                        <span class="inlinesectionContent">25000/p.m</span>
                    </div>
                </div>
                <div class="section">
                    <div class="sectionhead"></div>
                    <div class="sectioncontent sectioncontentinlinewrap">
                        <span class="inlinesectionhead">Languages Known</span>
                        <span class="inlinesectionContent">English,Hindi,Gujarati</span>
                    </div>
                </div>

            </div>
            <div class="footer">
                <hr />
                <div class="footerleft">
                    <span>Place: </span>
                    <span>Date: </span>
                </div>
                <div class="footerright">
                    <span>Signature: </span>
                    <span>Name: </span>
                </div>
                <div class="kutchdialstrip">
                    <hr />
                    my copyright text

                </div>
            </div>
        </div>
</body>
</html>


(3) C# Code
C#
  try
       {
           string strHtml = string.Empty;
           string pdfFileName = HttpContext.Current.Request.PhysicalApplicationPath + "Media\\files\\xyz\\" + "GenerateHTMLTOPDF.pdf";

           StringWriter sw = new StringWriter();
           HtmlTextWriter hw = new HtmlTextWriter(sw);
           dvHtml.RenderControl(hw);
           StringReader sr = new StringReader(sw.ToString());
           strHtml = sr.ReadToEnd();
           sr.Close();

           CreatePDFFromHTMLFile(strHtml, pdfFileName);

           HttpContext.Current.Response.ContentType = "application/x-download";
           HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "GenerateHTMLTOPDF.pdf"));
           HttpContext.Current.Response.WriteFile(pdfFileName);
           HttpContext.Current.Response.Flush();
           HttpContext.Current.Response.End();
       }
       catch (Exception ex)
       {
           HttpContext.Current.Response.Write(ex.Message);
       }


public static void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
   {
       try
       {
           object TargetFile = FileName;
           string ModifiedFileName = string.Empty;
           string FinalFileName = string.Empty;

          // HtmlStream = HtmlStream.Replace("\r\n", "");

           GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);

           GeneratePDF.HtmlPdfPage first = builder.AddPage();
           first.AppendHtml(HtmlStream);
           builder.ImportStylesheet(HttpContext.Current.Request.PhysicalApplicationPath + "CSS\\myxyz.css");
           byte[] file = builder.RenderPdf();
           File.WriteAllBytes(TargetFile.ToString(), file);

           iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
           ModifiedFileName = TargetFile.ToString();
           ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

           iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
           reader.Close();
           if (File.Exists(TargetFile.ToString()))
               File.Delete(TargetFile.ToString());
           FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
           File.Copy(ModifiedFileName, FinalFileName);
           if (File.Exists(ModifiedFileName))
               File.Delete(ModifiedFileName);

       }
       catch (Exception ex)
       {
           throw ex;
       }
   }
Posted
Updated 2-Oct-15 19:38pm
v3
Comments
Whisortates 26-Oct-15 6:18am    
I had a similar issue, one easy trick that you can use is to transform the external CSS files into an inline HTML styling.
On the following link you can find quite a few options on how to do this:
http://stackoverflow.com/questions/5066667/net-convert-external-css-to-inline-css
Another thing that you can try is the solution from the following article:
Convert HTML to PDF in C# and VB.NET
The code from that article uses this C# library for word documents processing.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900