Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create PDF from HTML using TheArtOfDev.HtmlRenderer.PdfSharp

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?

like image 988
Michele Ietri Avatar asked Jan 13 '17 11:01

Michele Ietri


3 Answers

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.

like image 182
Chris Jones Avatar answered Nov 17 '22 15:11

Chris Jones


Try using https://wkhtmltopdf.org, works well for bootstrap pages.

like image 3
Niran Manandhar Avatar answered Nov 17 '22 14:11

Niran Manandhar


I know its a little late but this can help someone The problem with bootstrap is that to align the columns use float: left and pdfsharp cannot read this property instead use display: inline-block and define the width in pixels.

like image 2
Alan Viezcas Avatar answered Nov 17 '22 15:11

Alan Viezcas