Table of Contents
Creating Pdf On The Fly

EO.Pdf supports creating PDF file on the fly, which is often required in a Web application.

Using HTML to PDF

Using PDF Creator (ACM)

Force "Save As" Dialog

Using HTML to PDF

To create PDF file on the fly in a Web page, simply use the HttpResponse's OutputStream as the output stream and call the appropriate ConvertUrl or ConvertHtml function that sends the PDF contents to an output stream. For example:

<asp:Button runat="server" id="Button1" OnClick="Button1_Click" />

Using PDF Creator (ACM)

If you use PdfDocument object directly, you can also calls the PdfDocument's Save method in your web page to save the PDF file to the output stream. The following code uses PDF Creator (ACM) to create PDF contents and send the output directly to the client.

protected void Button1_Click(object sender, EventArgs e)
{
    //Create a new document
    PdfDocument doc = new PdfDocument();

    //Create a new render
    AcmRender render = new AcmRender(doc);

    //Create some contents
    AcmContent content = new AcmContent(new AcmText("Hello!"));

    //Render the content
    render.Render(content);

    //Instead of saving the document to a local file, here it 
    //saves the document to output stream of the current
    //HttpResponse object. It also set the content type as
    //"application/pdf"
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.ClearHeaders();
    response.ContentType = "application/pdf";
    doc.Save(response.OutputStream);
}

The above code creates a "hello!" PDF file and open that file (if the user's browser has a PDF viewer plug-in) in the browser when user clicks the button.

Force "Save As" Dialog

You may wish to force the browser to always display a "Save As" dialog when opening the newly created PDF file. The Downloader control in our EO.Web Controls library can serve that purpose. You can find more details about how to force a "Save As" dialog from the Downloader's documentation. If you already have such code, you can reuse that code but call the following code to save the PdfDocument directly to the output stream:

//Save the document directly to the output stream        
doc.Save(response.OutputStream);