14

I have an action who displays a PDF in a new browser tab.

    public ActionResult Print()
    {
        var cd = new ContentDisposition
        {
            FileName ="something.pdf",
            Inline = true 
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(reportResponse.Data.Document, MediaTypeNames.Application.Pdf);

    }

The filename is working fine. When I download the file it has the name I want "something.pdf".

The problem is when google chrome opens the PDF in a new browser tab, it displays the controller action name (Print) as the title of the PDF. That's what I'm trying to change. I attached a picture for clarification.enter image description here

View code: Url.Action("Print", "Controller", new { area = "Area" }), new { @target = "_blank" }

Tomás Escamez
  • 542
  • 1
  • 6
  • 17
  • Browsers (at least Chrome and Firefox) set title of the page from title metadata of pdf document. http://w3.org/TR/WCAG20-TECHS/PDF18.html – Valyok26 Mar 29 '16 at 09:07
  • In my case, the PDF was a partial page to be displayed inline with other content. In MVC the title is set to the name of the action. In my case I just changed the action name to something generic. Still hoping to find a better solution. – 2b77bee6-5445-4c77-b1eb-4df3e5 Apr 04 '19 at 13:16

4 Answers4

5

To the action method pass parameter of the file name and make sure parameter name is 'id'.

View code:

Url.Action("Print", "Controller", new { id = "filename", area = "Area" }), new { @target = "_blank" }

Tawab Wakil
  • 1,737
  • 18
  • 33
  • Any chance to make this work without having the filename as `id` attribute? I have a numeric id (e.g. 1234) and both firefox/chrome show the pdf with those numeric id instead of filename. Altough `Inline = true` is set, your snippet download the pdf file instead of showing them inline. – Lion Dec 11 '18 at 11:52
2

Adding to kodandarami's answer,

When you're passing a PDF file back from ASP.NET MVC, the page title is set to the last url part of the route config used to get to your controller.

Knowing this, you can dynamically name the title by setting up a parameter as part of the route config for your controller eg:

        routes.MapRoute(
            "Print",
            "print/{pdfName}",
            new { controller = "Print", action = "Index", pdfName = UrlParameter.Optional }
        );

Then MVC will use this value instead as it considers it a separate page.

Use it in the url as "/print/this-is-my-pdf-title" not '/print?pdfName=this-is-my-pdf-title".

As a querystring parameter, MVC will just fall back to calling the PDF 'print'.

Note: As mentioned in this related question,

IIS has more strict rules about forbidden characters when the text is before the query string '?' character. You are not allowed <,>,*,%,&,:,\ (See MS Reference)

Any of these characters, even when encoded will give you a 'Path is potentially dangerous' 400 error.

Make sure to remove/replace these forbidden characters.

.

Jono
  • 3,949
  • 4
  • 28
  • 48
0

I solved this problem by using a iframe.

Create an action which fills the title and pdf url.

        public ActionResult ArticleSpecification(int ArticleID)
        {
            using (var context = new myEntities())
            {
                Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);
                ViewData["Title"] = article.Code + " - " + article.Description;
                ViewData["PdfSource"] = "ArticleSpecificationPdf?ArticleID=" + article.ArticleID;
                return View("~/Views/Specification/pdfview.cshtml");
            }
        }

pdfview.cshtml: with a iframe to view the pdf and title.

@{
    Layout = "";
}
<!DOCTYPE html>
<html>
    <head>
        <title>@ViewData["Title"]</title>
        <style>
            html, body, iframe {
                height: 100%;
                width: 100%;
                margin: 0;
                border: 0;
            }
            body {
                overflow-y: hidden;
            }
        </style>
    </head>
    <body>
        <iframe src="@ViewData["PdfSource"]"></iframe>
    </body>
</html>

The action to return the pdf content.

        public FileResult ArticleSpecificationPdf(int ArticleID)
        {
            using (var context = new myEntities())
            {
                PdfFile file = null;
                Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);
                if (article != null && article.SpecificationPdfID != null)
                    file = context.PdfFile.FirstOrDefault(a => a.PdfFileID == article.SpecificationPdfID);
                return new FilePathResult(file.path, "application/pdf");
            }
        }
-1

Try the following return statement:

return File(reportResponse.Data.Document, MediaTypeNames.Application.Pdf, "something.pdf");
diiN__________
  • 7,393
  • 6
  • 42
  • 69