Created
July 29, 2013 15:19
-
-
Save kad1r/6105088 to your computer and use it in GitHub Desktop.
You can convert your html to pdf with itextsharp dll. Just make sure your html tags closed well.
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
// Namespaces | |
using System.IO; | |
using iTextSharp.text; | |
using iTextSharp.text.html.simpleparser; | |
using iTextSharp.text.pdf; | |
using iTextSharp.tool.xml; | |
// Lets consider your html looks like HTML. | |
string HTML = "<table style=\"width:800px;\"><tr><td style=\"width:100px;\">FOO</td><td style=\"width:10px\">:</td><td>BLABLABLA ...</td></tr></table>"; | |
try | |
{ | |
//string filename = "\\pdf\\x_pdf___" + Request.QueryString["id"] + ".pdf"; | |
string filepath = HttpContext.Current.Server.MapPath("\\pdf\\x_pdf___" + Request.QueryString["id"] + ".pdf"); | |
Document document = new Document(PageSize.A4); | |
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create)); | |
document.Open(); | |
HTMLWorker hw = new HTMLWorker(document); | |
StringReader sr = new StringReader(HTML); | |
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr); | |
//hw.Parse(new StringReader(HTML)); | |
document.Close(); | |
ShowPdf(filename, filepath); | |
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG); | |
} | |
catch (Exception ex) | |
{ | |
string error = ex.Message; | |
} | |
// function to show on page | |
private void ShowPdf(string filename, string filePath) | |
{ | |
Response.ClearContent(); | |
Response.ClearHeaders(); | |
Response.ContentType = "application/pdf"; | |
Response.BufferOutput = true; | |
string FilePath = HttpContext.Current.Server.MapPath(filename); | |
Response.WriteFile(FilePath); | |
Response.End(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
iTextSharp.text.Image addLogo = default(iTextSharp.text.Image);
addLogo = iTextSharp.text.Image.GetInstance(Server.MapPath("Images") + "/Content/images/asia-cloud-logo.png");
addLogo.SetAbsolutePosition(30f, 800f);
addLogo.ScaleToFit(128, 37);
your other code
document.Open();
HTMLWorker hw = new HTMLWorker(document);