this is ConvetPDF Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
///
/// Summary description for clsPdfConversion
///
public class clsPdfConversion
{
public clsPdfConversion()
{
//
// TODO: Add constructor logic here
//
}
public static string strPdfContent = string.Empty;
public static string HTMLToPdf(string HTML, string fileName)
{
Document document = new Document();
string appRootDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
PdfWriter.GetInstance(document, new FileStream(appRootDir + "\\" + fileName + ".pdf", FileMode.Create));
document.Open();
//Image pdfImage = Image.GetInstance(Server.MapPath('logo.png'));
//pdfImage.ScaleToFit(100, 50);
//pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);
//document.Add(pdfImage);
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(HTML));
document.Close();
return (appRootDir + "\\" + fileName + ".pdf");
}
}
javaScript Code
function exportSalesToPdf(sellingId) {
PageMethods.exportSalesToPdf_wm(sellingId, function () {
window.open("Export.aspx", "mYwindow");
});
}
this is my webmethod
[WebMethod]
public static void exportSalesToPdf_wm(int iSellingId)
{
BAL.clsSellingAnalytics objSellingAnalytics = new BAL.clsSellingAnalytics();
DataTable success_user_dt = new DataTable();
DataTable sale_dt = new DataTable();
StringBuilder HTML = new StringBuilder();
string msg = string.Empty;
string strProfit = string.Empty;
objSellingAnalytics.GetSellingIdOfUser = iSellingId;
DataSet ds = objSellingAnalytics.SelectSuccessfulSalesOfUser_AllRecords();
sale_dt = ds.Tables[0];
success_user_dt = ds.Tables[1];
string strCurrencySymbol = "$";
if (Convert.ToString(sale_dt.Rows[0]["temp_currency_symbol"]) == "Rs.")
{
strCurrencySymbol = "Rs.";
}
if (strCurrencySymbol == "$")
{
strProfit = objSellingAnalytics.calculateProfit("NON-INR");
}
else if (strCurrencySymbol == "Rs.")
{
strProfit = objSellingAnalytics.calculateProfit("INR");
}
if (sale_dt.Rows.Count > 0)
{
string view_and_sell_count = objSellingAnalytics.Count_views(ref msg);
HTML.Append("<div style='font-family:'Helvetica Neue', Arial;background: #fafafa; padding: 10px; margin-top: 2px'>" +
"<h3 style='text-align: center; background: #7be4a9; padding: 6px; color: #fff; font-size: 22px'>" + sale_dt.Rows[0]["temp_creation_title"] + "</h3>" +
"<div style='text-align: center'>" +
" <span style='font-size: 28px; color: #999; font-weight: 700'>" + view_and_sell_count.Split('/').First() + " views</span>" +
" <span style='font-size: 28px; margin-left: 20px; color: #97dda8; font-weight: 700'>" + view_and_sell_count.Split('/').Last() + " sales</span>" +
" <span style='font-size: 28px; margin-left: 20px; color: #ff9f81; font-weight: 700'>" + strCurrencySymbol + strProfit + " profit</span>" +
"</div>");
HTML.Append("<div style='margin-top: 20px'>" +
" <h4 style='color: #999; font-size: 22px; margin-top: 10px; text-align: center'>" +
" Successful sales</h4>");
foreach (DataRow dr in sale_dt.Rows)
{
string strImageLink = string.Empty;
string strFullName = string.Empty;
double fsingleTicketPrice = 0.0;
int iTicketQuantity = 0;
if (dr["buyr_id"].ToString() != "0")
{
DataRow[] row_from = success_user_dt.Select("user_id ='" + dr["buyr_id"].ToString() + "'");
strImageLink = Convert.ToString(row_from[0]["image_link"]);
strFullName = Convert.ToString(row_from[0]["full_name"]);
}
else
{
strImageLink = "https://d297tiqatb0yra.cloudfront.net/images/solos/default_image_male.jpg";
strFullName = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToString(dr["mail"]));
}
HTML.Append(
"<div style='padding: 10px; background: #fff'>" +
"<img style='width: 32px;height: 32px;border-radius: 100px;' src='" + strImageLink + "' >" +
" <span style='color: #ffa775'>" + strFullName + "</span>");
if (Convert.ToInt32(dr["temp_shipping_status"]) == 1)
{
HTML.Append("<span style='color: #bdbdbd;margin-left: 20px; '>");
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_street_address"])))
{
HTML.Append(Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToString(dr["temp_street_address"])));
}
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_city"])))
{
HTML.Append(Microsoft.Security.Application.Encoder.HtmlEncode(", " + dr["temp_city"]));
}
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_state"])))
{
HTML.Append(Microsoft.Security.Application.Encoder.HtmlEncode(", " + dr["temp_state"]));
}
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_country_name"])))
{
HTML.Append(Microsoft.Security.Application.Encoder.HtmlEncode(", " + dr["temp_country_name"]));
}
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_pin_code"])))
{
HTML.Append(Microsoft.Security.Application.Encoder.HtmlEncode(" - " + dr["temp_pin_code"]));
}
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_phone_no"])))
{
HTML.Append(Microsoft.Security.Application.Encoder.HtmlEncode(". Ph: " + dr["temp_phone_no"]));
}
HTML.Append("</span>");
}
HTML.Append("<div style='float: right'>" );
string amount = Convert.ToString(dr["amount"]);
double multipliedAmount = 0.0;
string[] amouns = amount.Split('.');
double dAmount = Convert.ToDouble(amount);
double dQuantity = Convert.ToDouble(dr["temp_quantity"]);
if (amouns[1] == "00")
{
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_quantity"])))
{
if (dQuantity > 1.0)
{
iTicketQuantity = Convert.ToInt32(dr["temp_quantity"]);
fsingleTicketPrice = Math.Round(Convert.ToDouble(amouns[0]) / Convert.ToDouble(dr["temp_quantity"]), 2);
HTML.Append("<span style='color: #bdbdbd; '>" + strCurrencySymbol + fsingleTicketPrice + " x " + dr["temp_quantity"] + "</span>");
multipliedAmount = fsingleTicketPrice * dQuantity;
}
else
{
HTML.Append("<span style='color: #bdbdbd; '>" + strCurrencySymbol + amouns[0] + "</span>");
multipliedAmount = dAmount;
}
}
else
{
HTML.Append("<span style='color: #bdbdbd; '>" + strCurrencySymbol + amouns[0] + "</span>");
multipliedAmount = dAmount;
}
}
else
{
if (!string.IsNullOrEmpty(Convert.ToString(dr["temp_quantity"])))
{
if (dQuantity > 1.0)
{
iTicketQuantity = Convert.ToInt32(dr["temp_quantity"]);
fsingleTicketPrice = Math.Round(Convert.ToDouble(dr["amount"]) / Convert.ToDouble(dr["temp_quantity"]), 2);
HTML.Append("<span style='color: #bdbdbd;'>" + strCurrencySymbol + fsingleTicketPrice + " x " + dr["temp_quantity"] + "</span>");
multipliedAmount = fsingleTicketPrice * dQuantity;
}
else
{
HTML.Append("<span style='color: #bdbdbd; '>" + strCurrencySymbol + dr["amount"] + "</span>");
multipliedAmount = dAmount;
}
}
else
{
HTML.Append("<span style='color: #bdbdbd; '>" + strCurrencySymbol + Convert.ToString(dr["amount"]) + "</span>");
multipliedAmount = dAmount;
}
}
if (Convert.ToInt32(dr["temp_refund_status"]) == 1)
{
HTML.Append("<span style='color: #bdbdbd; '> (Refunded " + strCurrencySymbol + dr["temp_refund_amount"] + ")</span>");
}
else if (Convert.ToInt32(dr["temp_refund_status"]) == 2)
{
HTML.Append("<span style='color: #bdbdbd; '>(Charged back)</span>");
}
//if (Convert.ToInt32(dr["temp_failed_status"]) == 1)
//{
// HTML.Append("<span style='color: #bdbdbd; '>Failed</span>");
//}
HTML.Append("</div>" +
" </div>");
}
HTML.Append(" </div>" +
" </div>");
}
clsPdfConversion.strPdfContent = Convert.ToString(HTML);
ClsCSVConversion.strCsvContent = Convert.ToString(HTML);
}