views:

122

answers:

1

Background:

I couldn't find any decent free HTML to PDF conversion utilities in C#. There are 100s of them for PHP with extensive documentation, support, and CSS support. So I am using html2ps and html2pdf (php).

I have PHP 5.2 installed on IIS7 and its working beautifully to create PDFs.

I have the following in getPDF.aspx

<!-- Output the header -->
<DM:header runat="server" ID="header" />

<asp:Placeholder id="content" runat="server" />

<!-- Output the footer -->
<DM:footer runat="server" ID="footer" />

and in getPDF.aspx.cs:

protected void Page_Load(object sender, EventArgs e){
    // AddContentControl simples adds a controls to the content Placeholder.

    AddContentControl("controls/page1.ascx");
    AddContentControl("controls/page2.ascx");
    AddContentControl("controls/page3.ascx");
}

and in generatePDF.php:

<?php
    /* ... includes and stuff here ... */

    $data = "THE HTML GOES HERE!";
    // creates the PDF from the $data and Outputs the created file.
    convert_to_pdf($data);
?>

-- getPDF.aspx works perfectly...except the output is HTML.

So how can I get getPDF.aspx to output its HTML as PDF generated by generatePDF.php?

+1  A: 

I'd suggest looking into iTextSharp a free .NET port of iText (Java-Based PDF Lib) Then you can cut php right out of the equation.

For converting HTML using iTextSharp Please See This Post (Found using google)

Update

Rendering Partials in ASP.NET Forms (i.e. rendering a single control, or a page with controls) You create a System.Web.Page to drive the event structure.

Here's a code-sample I adapted for a project of mine:

    public static string Render<T>(string controlPath, Action<T> initControlCallback) where T : Control
    {
        Page renderPage = new Page();

        // Load the control & add to page
        T control = (T) renderPage.LoadControl(controlPath);
        renderPage.Controls.Add(control);

        // Initialize the control
        initControlCallback.Invoke(control);
        renderPage.DataBind();

        StringWriter result = new StringWriter();
        HttpContext.Current.Server.Execute(renderPage, result, false); // Render Process
        return result.ToString();
    }

It's called like this:

MyHelper.Render<MyControlBase>("~/SomePath/SomeControl.ascx", p => { p.SomeProperty = "Initializer" });

This code may not be what you need, but as you can see you can render a result using the Server / Page objects, this may be the route you should take.

Aren
I've actually used iTextSharp before...but I didn't know it works with HTML. the string "HTML" is nowhere on their home page.
David Murdoch
I've updated my answer with a link to an example.
Aren
I'm working on it now. Now my problem is that I'm trying to render controls and then write their contents into a PDF. The controls rely on their own Page_Load events which don't get fired when using control.RenderControl(HtmlTextWriter);
David Murdoch
More updates for you :)
Aren
now my problem is that iTextSharp chokes on my HTML.
David Murdoch
I ended up going with a .net solution. thanks!
David Murdoch