Skip to content

Instantly share code, notes, and snippets.

@ugurhasar
Last active January 5, 2018 07:51
Show Gist options
  • Save ugurhasar/566fe1fb52d581dfb81c2b40b07ec3b5 to your computer and use it in GitHub Desktop.
Save ugurhasar/566fe1fb52d581dfb81c2b40b07ec3b5 to your computer and use it in GitHub Desktop.
C#'ta iTextSharp ile Html'i Pdf'e Dönüştürme
<!DOCTYPE HTML>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
public byte[] Write()
{
byte[] bytes = null;
string htmlStr = File.ReadAllText("html dosya yolu", Encoding.UTF8);
string cssStr = File.ReadAllText("css dosya yolu", Encoding.UTF8);
using (var memoryStream = new MemoryStream())
{
using (Document document = new Document(PageSize.A4, 0, 0, 0, 0))
{
using (var pdfWriter = PdfWriter.GetInstance(document, memoryStream))
{
document.Open();
using (var css = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssStr)))
{
using (var html = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlStr)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, html, css);
}
}
document.Close();
}
}
bytes = memoryStream.ToArray();
}
return bytes;
}
<style>
h1 { color: red; }
</style>
using System.Web.UI.HtmlControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Text.RegularExpressions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment