Created
June 27, 2017 09:25
-
-
Save anonymous/aef30750a241b8d1dd4212afc1045f09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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