0

I wrote Asp.net application in which web browser can download pdf file from a server and open the pdf file in new tab. The program code is ok functionally but the height of the window size of the pdf is very small.

Does enyone have any idea? It would be really helpful if anyone comments on this problem or have any idea........

The code is written below. Asp.Net Controller

[HttpPost]
public IActionResult DownloadPdf(){
    var stream = new FileStream( pdfPath, FileMode.Open );
    return ControllerBase.File( stream, "application/pdf", pdfName);
}

Asp.Net _Layout.cshtml

<head>
    <link rel="stylesheet" href="~/css/common.css" />
    .........another codes......
</head>
<body>
    <main>@RenderBody()</main>
</body>

Asp.Net DownloadPdf.cshtml

<button id="download">Download</button>

Javascript

$("#downloadPdf").click(function(){
    let response = await fetch( DownloadPdf",{
        method: "POST",
    }
    let blob = await response.blob();
    let url = (window.URL || window.webkitURL).createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.target = "_blank";
    a.rel = "noopener noreferrer";
    a.id = 1;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
}

Result enter image description here

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
Lemonade
  • 21
  • 4
  • you can embed it within an iframe on your webpage instead of new tab, because you can easily initial height and width of the iframe. – Xinran Shen Aug 10 '23 at 06:30
  • Thank you for your comment. I am now trying this code: "Content-Security-Policy", "default-src 'self'; object-src 'self' blob:; child-src 'self' blob:; – Lemonade Aug 10 '23 at 08:21
  • Maybe you can refer to this [link](https://stackoverflow.com/questions/29425989/load-generated-pdf-data-into-iframe-on-asp-net-mvc). – Xinran Shen Aug 10 '23 at 08:51

0 Answers0