6

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

fero
  • 6,050
  • 1
  • 33
  • 56
user1400915
  • 1,933
  • 6
  • 29
  • 55
  • ..so you're converting an existing html page to pdf and then you want to display that in a web page browser? – Seph Dec 05 '12 at 11:03
  • @Seph:Yes ,thats exactly what my requirement is – user1400915 Dec 05 '12 at 11:07
  • I am not familiar with MVC but if it has a mime type and there is a viewer registered for the mime type it should load. If you could leave it as HTML it sure would be easier. – paparazzo Dec 05 '12 at 14:12
  • The mime type is "application/octet stream" and this stream mostly I need to pass to view ,but is there any pdf viewer available? – user1400915 Dec 05 '12 at 16:06
  • Then that is the problem. Mime type application/octet stream is always saved as as a file. You need the mime type to be application/pdf. – paparazzo Dec 05 '12 at 20:06

2 Answers2

12

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)

William R.
  • 145
  • 1
  • 11
retslig
  • 888
  • 5
  • 22
  • Thanks for the comment.Will start from here and post the working code after doing some R & D. – user1400915 Dec 05 '12 at 16:19
  • my assumption was that after submitting the form you would only display the pdf in another div on the screen and keep the form available... may not be what you wanted but I use this a lot in my applications as I want the user to able to see a preview of the pdf before actually creating it or what ever. – retslig Dec 05 '12 at 16:33
  • @restlig:What is "RenderPdf" ?Is that an action in Home controller? – user1400915 Dec 06 '12 at 05:21
  • sorry missed that code it is the partial. edited the code above to include this. – retslig Dec 06 '12 at 15:05
  • Hi. What type did you use for the model? I do not want to add confusion and add a field into my Parent ViewModel. Im hoping you have an idea around this? – Vishav Premlall Jun 28 '18 at 12:30
  • The 2 methods: GeneratePDF and RenderPDF. What arguments do i send to them? – Vishav Premlall Jun 28 '18 at 12:44
  • What du you have in these two models( LabelViewModel and ViewModel )? – Mohammad Hassani Aug 05 '18 at 08:05
  • GeneratePDF gave me some issues, and this https://stackoverflow.com/questions/6439634/view-pdf-as-part-of-the-page helped – Adam Cox Mar 28 '19 at 19:16
1

How a PDF file is displayed depends on the user's browser. I don't think that there is a real "solution" for your problem.

When I had this problem I rendered the PDF file to multiple PNG images on the server using the GhostScript library and then referenced the PNG files in my view.

fero
  • 6,050
  • 1
  • 33
  • 56