Skip to content

Instantly share code, notes, and snippets.

Created June 27, 2017 09:25
Show Gist options
  • Save anonymous/aef30750a241b8d1dd4212afc1045f09 to your computer and use it in GitHub Desktop.
Save anonymous/aef30750a241b8d1dd4212afc1045f09 to your computer and use it in GitHub Desktop.
public MemoryStream ConvertToPDF(string url)
{
if (!string.IsNullOrWhiteSpace(url))
{
HtmlWeb hw = new HtmlWeb();
HtmlDocument hd = hw.Load(url);
HtmlNode node = hd.DocumentNode.SelectSingleNode("html");
string html = node.InnerText;
MemoryStream ms = new MemoryStream();
TextReader reader = new StringReader(html);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
File.WriteAllBytes(@"C:\temp\output.pdf", ms.ToArray());
ms.Dispose();
return ms;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment