Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PDF based on HTML code (iTextSharp, PDFSharp?)

Tags:

Does the library PDFSharp can - like iTextSharp - generate PDF files *take into account HTML formatting *? (bold (strong), spacing (br), etc.)

Previously I used iTextSharp and roughly handled in such a way (code below):

 string encodingMetaTag = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
 string htmlCode = "text <div> <b> bold </ b> or <u> underlined </ u> <div/>";

 var sr = new StringReader (encodingMetaTag + htmlCode);
 var pdfDoc = new Document (PageSize.A4, 10f, 10f, 10f, 0f);
 var = new HTMLWorker htmlparser (pdfDoc);
 PdfWriter.GetInstance (pdfDoc, HttpContext.Current.Response.OutputStream);
 pdfDoc.Open ();
 htmlparser.Parse (sr);
 pdfDoc.Close ();

incorporated into the appropriate HTML form to a PDF document dealt with the class object HTMLWorker.. so what with PDFSharp? Has PDFSharp similar solution?

like image 941
TomashUfx Avatar asked Sep 29 '11 12:09

TomashUfx


3 Answers

I know this question is old, but here's a clean way to do it...

You can use HtmlRenderer combined with PDFSharp to accomplish this:

Bitmap bitmap = new Bitmap(1200, 1800); Graphics g = Graphics.FromImage(bitmap); HtmlRenderer.HtmlContainer c = new HtmlRenderer.HtmlContainer(); c.SetHtml("<html><body style='font-size:20px'>Whatever</body></html>"); c.PerformPaint(g); PdfDocument doc = new PdfDocument(); PdfPage page = new PdfPage(); XImage img = XImage.FromGdiPlusImage(bitmap); doc.Pages.Add(page); XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]); xgr.DrawImage(img, 0, 0); doc.Save(@"C:\test.pdf"); doc.Close();          

Some people report that the final image looks a bit blurry, apparently due to automatic anti-aliasing. Here's a post message on how to fix that: http://forum.pdfsharp.com/viewtopic.php?f=2&t=1811&start=0

like image 106
Diego Avatar answered Oct 05 '22 16:10

Diego


No, PDFsharp does not currently include code to parse HTML files.

like image 42
I liked the old Stack Overflow Avatar answered Oct 05 '22 17:10

I liked the old Stack Overflow


Old question but none of above worked for me. Then i tried generatepdf method of HtmlRenderer in combination of pdfsharp. Hope it helps: You must install a nuget named HtmlRenderer.pdfsharp.

var doc = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf("Your html in a string",PageSize.A4);
  PdfPage page = new PdfPage();
  XImage img = XImage.FromGdiPlusImage(bitmap);
  doc.Pages.Add(page);
  XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
  xgr.DrawImage(img, 0, 0);
  doc.Save(Server.MapPath("test.pdf"));
  doc.Close();
like image 39
Rohit Arora Avatar answered Oct 05 '22 15:10

Rohit Arora