Create PDF from HTML using TheArtOfDev.HtmlRenderer.PdfSharp

Michele Ietri picture Michele Ietri · Jan 13, 2017 · Viewed 16.3k times · Source

I need to convert a well-formatted HTML string to a PDF document.

I found this DLL that should do what I need, but it isn't working fine on formatting.

That's the HTML code I'm trying to convert, and viewing it on browser works fine (I've used Bootstrap CSS, that's been correctly referenced as cdn).

enter image description here

But once converted to PDF this is the result: enter image description here

And that's the code I'm using to convert it:

        string html = "";

        if (File.Exists(pathIN))
        {
            html = File.ReadAllText(pathIN);
        }

        PdfDocument pdfDocument = new PdfDocument();
        PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4, 60);
        pdf.Save(pathOUT);

Does anyone have any suggestion?

Answer

Chris Jones picture Chris Jones · Mar 21, 2017

I also had issues with this when using HtmlRenderer/PdfSharp with Bootstrap controlling the layout.

Although it goes against the grain, I resorted to using tables for the layout. Given that the destination (pdf) was a obviously a fixed width, being responsive was not a requirement.