Convert HTML to PDF in. NET

I want to generate PDF by passing HTML content to the function. I've used iTextSharp for this, but it doesn't perform well when it encounters tables and the layout gets messy.

Is there a better way?

#1 building

Try using this PDF duo. Net conversion component to HTML from ASP.NET Application transformation For PDF, No other dll is required.

You can pass HTML strings or files, or streams to generate PDF. Use the following code (example C):

string file_html = @"K:\hdoc.html";   
string file_pdf = @"K:\new.pdf";   
try   
{   
    DuoDimension.HtmlToPdf conv = new DuoDimension.HtmlToPdf();   
    conv.OpenHTML(file_html);   
    conv.SavePDF(file_pdf);   
    textBox4.Text = "C# Example: Converting succeeded";   
}   

You can find the Info + C / VB example at: http : //www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx

#2 building

OK, use this technology.

src can from Here Download, requires nant

#3 building

Update: now I recommend wkhtmltopdf Use pupeersharp on.

attempt wkhtmtopdf . This is the best tool I have found so far.

For. NET, you can use this Small library Make it easy to call the wkhtmtopdf command line utility.

#4 building

You can create a bitmap of an HTML page and then use for example iTextSharp Instead of parsing HTML directly to PDF, insert bitmaps into PDF.

This is a code of how to get the URL bitmap. I found it somewhere in SO, and if I found the source, I linked it.

public System.Drawing.Bitmap HTMLToImage(String strHTML)
{
    System.Drawing.Bitmap myBitmap = null;

    System.Threading.Thread myThread = new System.Threading.Thread(delegate()
    {
        // create a hidden web browser, which will navigate to the page
        System.Windows.Forms.WebBrowser myWebBrowser = new System.Windows.Forms.WebBrowser();
        // we don't want scrollbars on our image
        myWebBrowser.ScrollBarsEnabled = false;
        // don't let any errors shine through
        myWebBrowser.ScriptErrorsSuppressed = true;
        // let's load up that page!    
        myWebBrowser.Navigate("about:blank");

        // wait until the page is fully loaded
        while (myWebBrowser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
            System.Windows.Forms.Application.DoEvents();

        myWebBrowser.Document.Body.InnerHtml = strHTML;

        // set the size of our web browser to be the same size as the page
        int intScrollPadding = 20;
        int intDocumentWidth = myWebBrowser.Document.Body.ScrollRectangle.Width + intScrollPadding;
        int intDocumentHeight = myWebBrowser.Document.Body.ScrollRectangle.Height + intScrollPadding;
        myWebBrowser.Width = intDocumentWidth;
        myWebBrowser.Height = intDocumentHeight;
        // a bitmap that we will draw to
        myBitmap = new System.Drawing.Bitmap(intDocumentWidth - intScrollPadding, intDocumentHeight - intScrollPadding);
        // draw the web browser to the bitmap
        myWebBrowser.DrawToBitmap(myBitmap, new System.Drawing.Rectangle(0, 0, intDocumentWidth - intScrollPadding, intDocumentHeight - intScrollPadding));
    });
    myThread.SetApartmentState(System.Threading.ApartmentState.STA);
    myThread.Start();
    myThread.Join();

    return myBitmap;
}

#5 building

Use Winnovative HTML to PDF Converter, you can convert HTML strings in one line

byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlString, baseUrl);

The base URL is used to parse images referenced by relative URLs in HTML strings. In addition, you can use the full URL in HTML or use src = "data: image / png" to embed an image as an image tag.

In order to answer "fubaar" user comments about the innovative converter, corrections must be made. The converter does not use IE as the rendering engine. It doesn't actually depend on any installed software, and rendering is compatible with the WebKit engine.

Tags: Windows IE

Posted on Wed, 05 Feb 2020 08:45:26 -0500 by TeNDoLLA