Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pdf Viewer in MVC to show the pdf contents in View

I have a View called ShowDocument.cshtml.

I want to show the pdf document in a view. First I am converting the html page to .pdf which contains the information as :

The code in the controller is:

Stream stream = HtmlToPdfBuilder.GetHtmlForm(model.Type, 16);

If I give return File(stream, "application/pdf", "Authorization.pdf"), I will be getting as save, save as dialog box.

I do not want this dialog box ,I want to show the pdf content inside the page only .

So Is there any pdf viewer in MVC so that I can show the content in a View only using some control

like image 713
user1400915 Avatar asked Dec 05 '12 10:12

user1400915


People also ask

How do I display the contents of a PDF in HTML?

The easiest way to put PDF in an HTML document is using the <a> tag with its href attribute. You need to add the URL or the reference link of your PDF file to the element.

How do you visualize a PDF?

The most common way to view a PDF file on your computer or mobile device is by installing free PDF readers such as Adobe Reader. Adobe Reader is by far the most popular PDF reader in the market today for a good reason, they invented the PDF!

What is the best way to display a PDF in a web app?

Using Google Docs' Viewer.


1 Answers

This may not be exactly what you want but might meet your need. You can embed the PDF in a partial view then update the partial view via ajax with the PDF on the form submit button.

Example code: Partial view

    @model Test.Models.ViewModel

<style type="text/css">

#pdfbox
{
    width:600px;
    height:400px;
    border: 5px solid #ccc;
}

</style>

<object id='pdfbox' type="application/pdf" data="@Url.Action("GeneratePDF", "Home", Model)">
    Click @Html.ActionLink("here", "GeneratePDF", "Home") to view the file.
</object>    

Controller call:

    public ActionResult GeneratePDF(ViewModel model)
    {

        byte[] bytes = OpenPDFAndGetBytes("Thepdfname");
        return File(bytes, "application/pdf");
    }

    public ActionResult RenderPDF(LabelViewModel model)
    {
        return PartialView(model);
    }

main view:

@using (Ajax.BeginForm("RenderPDF", "Home", new AjaxOptions { UpdateTargetId = "pdf" }))
{
    <table>
        <tr>
            <td>
                <fieldset>
                    <legend>Fill the form:</legend>
                        Some form junk can go here
                    <br />
                    <input type="submit" value="Display PDF" />
                </fieldset>
            </td>
            <td>
                <div id='pdf'>
                    @{
                        Html.RenderPartial("RenderPDF", Model);
                    }
                </div>
            </td>
        </tr>
    </table>
}

(Edit: Changed "main view" to a title ish)

like image 85
retslig Avatar answered Sep 18 '22 01:09

retslig