Sai Avinash
Sai Avinash

Reputation: 4753

Converting a html page with jquery into pdf

I am trying to find an html parser that can parse an html page even with jquery and convert it to pdf.

previously, i have been using the following code to export an html file to pdf:

using System;using System.Web;
using System.Web.UI;
using System.Data;
using System.IO;using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser; 

protected void btnPDF_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Page.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

But by using iTextSharp dll , will i be able to convert an html page that has Jquery UI in it into a pdf?

I ran out of ideas. can any one please give inputs or sugesstions how can we achieve this?

First of all, i would like to know whether this can be achieved at all? is there any dll that has capability to convert an html page(Jquery UI) to pdf?

Upvotes: 0

Views: 3747

Answers (2)

Alok
Alok

Reputation: 522

i used jspdf.debug.js to solve this ..

  function pdfFromHtml(element,gridConfig) { 
                getHtmlForExport(element,gridConfig,"pdf")
                var source='<table>'+$('#exportTable').html()+'</table>'; 
                var pdf = new jsPDF('l', 'pt', 'a0');  
               var margins = {
                    top: 60,
                    bottom: 10,
                    left: 10,
                    width: 720
                };
                pdf.fromHTML(
                    source, // HTML string or DOM elem ref.
                    margins.left, // x coord
                    margins.top, {// y coord
                        'width': margins.width, // max width of content on PDF 
                    },
                    function (obj) { 
                        var elementClicked = element;
                        var headerText =  $(elementClicked).text();
                        var fileName;
                        if (elementClicked == undefined || headerText == undefined) {
                            fileName = "pdfExport"
                        } else {
                            fileName = headerText.toString().trim()
                        } 
                            pdf.setFontSize(22);
                            pdf.setFontType("bold");
                            pdf.text(40, 40, fileName+" grid");
                            pdf.save(fileName + '.pdf');
                    }, margins);
    } 

getHtmlForExport(element,gridConfig,"pdf") is the function to get plain html from the table which you want to export to pdf ....

Note CSS won't be supported as far as i know.

Upvotes: 1

Andre Lombaard
Andre Lombaard

Reputation: 7105

Evo PDF is a commercial software library that can handle this for you.

If you do not mind to use a command line tool, you can have a look at wkhtmltopdf

Upvotes: 1

Related Questions