HTML to PDFPublished: Apr 12, 2015 01:15:55
Physical Link: HTML to PDF
Have you ever wanted a function to convert HTML to PDF? It is really easy. A good use of this is when you have a resume on your website and you want to create a download of it as a PDF. Mind you; you will have to properly format your resume so it looks good in both cases. Not the easiest job, but totally worth while to have a resume download created on demand.
I just created a genPDF.aspx file that takes a URL and makes it to a PDF. Put a button on the page and have it call the genPDF.aspx page. Your download will automatically be created and pushed through to the browser.
Some things you will need first is a download of some zip files. You can get them from my website or you can seek them out on sourceforge ( iTextSharp / xmlworker ).
There is a lot of examples of how to do this; however, most examples do not support linked CSS files. My example does support linked CSS files.
The code is very simple:
Document document = new Document();
var oStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, oStream);
document.AddHeader("Content-Disposition", "attachment; filename=resume.pdf");
WebClient wc = new WebClient();
string htmlText = wc.DownloadString("http://ldndeveloper.com/resume/resume.aspx");
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(htmlText));
Response.OutputStream.Write(oStream.GetBuffer(), 0, oStream.GetBuffer().Length);
The post HTML to PDF appeared first on Software Developer In London.
Author: Andrew Pallant
Categories: C#, C-Sharp, Developement, PDF, Web