While looking to generate HTML to PDF, i came across how to create PDF exactly as HTML using its stylesheet.
After working out for nearly 4 hrs I found something from Stackoverflow which extract inline and external css beautifully.
limitation: Right now this function extracts from current page...I need a dynamic rul where defining the url we can create PDF according to the stytle-sheet... Code is given below..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExtractStyleSheet.aspx.cs" Inherits="Common_UserControls_Lab_HtmltoPDF_ExtractStyleSheet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="../../../_assets/css/addressbook.css" rel="stylesheet" type="text/css" />
<link href="../../../_assets/css/CleanForm.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.grid
{
font-family:Arial;
font-size:12px;
width:800px;
}
/* gridview styles */
.grid .gridview
{
width:100%;
border:solid 2px #5E8CC6;
empty-cells:show;
border-top-width:1px;
}
</style>
<script type="text/javascript" >
function getStyles() {
if (!document.styleSheets) return false; // return false if browser sucks
var rules = new Array();
for (var i = 0; i < document.styleSheets.length; i++) {
var x = 0;
styleSheet = document.styleSheets[i];
if (styleSheet.cssText) { // if this is IE, get the rules directly
rules.push(styleSheet.cssText);
} else {
// otherwise get them individually
do {
cssRule = styleSheet.cssRules[x];
if (cssRule) rules.push(cssRule.cssText);
x++;
} while (cssRule);
}
}
var compositeCSS = '';
debugger;
for (i = 0; i < rules.length ; i++) {
compositeCSS += rules[i];
}
return compositeCSS;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClientClick="getStyles()" Text="Button" />
</div>
</form>
</body>
</html>