iTextSharp ile türkçe karakter sorunu olmayan, turkce PDF üretmek:
var example_css = @"@font-face {
src: url('arialuni.ttf');
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
body {font-family:arial unicode ms;encoding:Identity-H;font-size:11pt;}";
string strHtml = @"<html><head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html;charset=iso-8859-9'/>
<META HTTP-EQUIV='Content-language' CONTENT='tr'/>
</head>
<body style='font-family:arial unicode ms;'>
<div style='height:225px;'></div>
<div style='width:100%;text-align:center;font-size:25px;font-weight:bolder;'>" + baslik + @"</div>
<div style='height:120px;'></div>
<div style='height:400px;margin-left:100px;padding-left:100px;margin-right:100px;padding-right:100px;'>" + metin+@"</div>
<div style='margin-left:100px;text-align:center;float:center;'><img style='width:370px;' src='"+resim+@"' /></div>
</body></html>";
MemoryStream memStream = new MemoryStream();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string strFileShortName = ad + "-"+toTRK2(baslik).Replace(" ","_")+"-"+DateTime.Now.ToString("yyMMddHHmmss") + ".pdf";
string strFileName = HttpContext.Current.Server.MapPath("uploads\\" + strFileShortName);
float lm = 10, rm = 10, tm = 10, bm = 10;
iTextSharp.text.Document docWorkingDocument = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, lm, rm, tm, bm);
StringReader srdDocToString = null;
//sayfa zeminine bir foto ekle
string imageFilePath = "http://www.xxxxxxxxxxxxx.com/gelisim-kart-arka.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
var pageWidth = docWorkingDocument.PageSize.Width - (lm + rm);
var pageHeight = docWorkingDocument.PageSize.Height - (bm + tm);
jpg.SetAbsolutePosition(lm, bm);
jpg.ScaleToFit(pageWidth, pageHeight);
MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css));
MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strHtml));
try
{
PdfWriter pdfWrite = default(PdfWriter);
pdfWrite = PdfWriter.GetInstance(docWorkingDocument, new FileStream(strFileName, FileMode.Create));
docWorkingDocument.Open();
docWorkingDocument.Add(jpg);
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, msHtml, msCss);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if ((docWorkingDocument != null))
{
docWorkingDocument.Close();
docWorkingDocument.Dispose();
}
if ((srdDocToString != null))
{
srdDocToString.Close();
srdDocToString.Dispose();
}
}
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "window.open('uploads/" + strFileShortName + "','_blank','location=0,menubar=0,status=0,titlebar=0,toolbar=0');", true);
veya farklı birşekilde DB den degilde sayfadaki bir kontrol yapmak için :
public void DownloadAsPDF(string h)
{
try
{
string strHtml = string.Empty;
string pdfFileName = Request.PhysicalApplicationPath + "\\uploads\\" + "Raporunuz.pdf";
//kontroledn almak icin
//StringWriter sw = new StringWriter();
//HtmlTextWriter hw = new HtmlTextWriter(sw);
//StringReader sr = new StringReader(sw.ToString());
//strHtml = sr.ReadToEnd();
//sr.Close();
//strHtml
CreatePDFFromHTMLFile( h, pdfFileName);
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", "Raporunuz.pdf"));
Response.ContentEncoding = Encoding.UTF8;
Response.WriteFile(pdfFileName);
Response.HeaderEncoding = Encoding.UTF8;
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}